package cn.ezhizao.common.utils.pdf;

import com.itextpdf.io.font.FontProgramFactory;
import com.itextpdf.kernel.events.Event;
import com.itextpdf.kernel.events.IEventHandler;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Canvas;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.properties.VerticalAlignment;

/**
 * pdf 添加图片和文字水印
 */
public class PdfWaterMarker implements IEventHandler {
    private String textWaterMarker;
    private Image imageWaterMarker;

    private PdfFont font;

    public PdfWaterMarker(String textWaterMarker) {
        this.textWaterMarker = textWaterMarker;
    }

    public PdfWaterMarker(String textWaterMarker, PdfFont font) {
        this.textWaterMarker = textWaterMarker;
        this.font = font;
    }

    public PdfWaterMarker(Image imageWaterMarker) {
        this.imageWaterMarker = imageWaterMarker;
    }

    public String getTextWaterMarker() {
        return textWaterMarker;
    }

    public void setTextWaterMarker(String textWaterMarker) {
        this.textWaterMarker = textWaterMarker;
    }

    @Override
    public void handleEvent(Event event) {
        PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
        PdfDocument pdf = docEvent.getDocument();
        PdfPage page = docEvent.getPage();
        Rectangle pageSize = page.getPageSize();
        PdfCanvas pdfCanvas = new PdfCanvas(
                page.getLastContentStream(), page.getResources(), pdf);
        Canvas canvas = new Canvas(pdfCanvas, pageSize);
        if (textWaterMarker != null && !textWaterMarker.isEmpty()) {
            Paragraph waterMarker = new Paragraph(textWaterMarker)
                    .setOpacity(0.05f)
                    .setFontSize(40);
            if (font != null) {
                waterMarker.setFont(font);
            }
            for (int i = 0; i < 5; i++) {
                for (int j = 0; j < 5; j++) {
                    canvas.showTextAligned(waterMarker, (150 + i * 300), (160 + j * 150), pdf.getNumberOfPages(), TextAlignment.CENTER, VerticalAlignment.BOTTOM, .6f);
                }
            }
            canvas.close();
        } else if (imageWaterMarker != null) {
            Paragraph waterMarker = new Paragraph().add(imageWaterMarker).setOpacity(0.05f);
            imageWaterMarker.setHeight(30);
            for (int i = 0; i < 5; i++) {
                for (int j = 0; j < 5; j++) {
                    canvas.showTextAligned(waterMarker, (150 + i * 300), (160 + j * 150), pdf.getNumberOfPages(), TextAlignment.CENTER, VerticalAlignment.BOTTOM, .6f);
                }
            }
        }


    }
}
