使用 iText5 为 PDF 添加水印

Adding watermark to PDF using iText5

我想使用 PdfPageEvent 向现有 pdf 添加水印,但是当我尝试这样做时,它无法正常工作。

这是我的代码,

private void addWatermark(Uri fileUri) {

    InputStream inputStream = getContentResolver().openInputStream(fileUri);
    PdfReader reader = new PdfReader(inputStream);

    Document document = new Document(PageSize.A4);

    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(expPDF));
    writer.setBoxSize("page", document.getPageSize());
    writer.setPageEvent(watermarkPageEvent);

    document.open();
    PdfContentByte cb = writer.getDirectContent();

    for (int i = 1; i <= reader.getNumberOfPages(); i++) {

        PdfImportedPage page = writer.getImportedPage(reader, i);
        cb.addTemplate(page, 0, 0);
        document.newPage();
    }

    document.close();
}

PDFPageEventHelper

static class WatermarkPageEvent extends PdfPageEventHelper {

    private static final String TAG = "WatermarkPageEvent__";
    String text = "Testing";
    int textSize = 20;
    BaseColor textColor = BaseColor.BLACK;
    Font.FontFamily fontFamily;
    int fontStyle = Font.BOLD;
    int rotate = 0;
    Font font;
    Phrase phrase;
    boolean isW;

    public WatermarkPageEvent() {
        fontFamily = Font.FontFamily.HELVETICA;
        font = new Font(fontFamily, textSize, fontStyle, textColor);
        phrase = new Phrase(new Anchor(text));
        phrase.setFont(font);

    }

    @Override
    public void onOpenDocument(PdfWriter writer, Document document) {
        super.onOpenDocument(writer, document);

    }

    @Override
    public void onEndPage(PdfWriter writer, Document document) {

        Rectangle rectangle = writer.getBoxSize("page");

        ColumnText.showTextAligned(writer.getDirectContentUnder(),//
                Element.ALIGN_CENTER,//
                phrase,//
                (rectangle.getWidth() / 2f), // want to add watermark in center
                (rectangle.getHeight() / 2f), //
                rotate);//


    }

    public void addText(String toString) {
        text = toString;
        phrase.add(new Anchor(text));
    }
}

即使不使用 iText 5 中的 PdfPageEvent,您也可以添加水印。5.X。请参考以下工作示例。

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfGState;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class TransparentWatermark {

public static final String SRC = "src/main/resources/nanobi/input_watermark.pdf";
public static final String DEST = "src/main/resources/nanobi/output_watermark.pdf";

public static void main(String[] args) throws IOException, DocumentException {
    File file = new File(DEST);
    file.getParentFile().mkdirs();
    new TransparentWatermark().manipulatePdf(SRC, DEST);
}

public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    PdfContentByte under = stamper.getUnderContent(1);
    Font f = new Font(FontFamily.HELVETICA, 15);
    Phrase p = new Phrase("This watermark is added UNDER the existing content", f);
    ColumnText.showTextAligned(under, Element.ALIGN_CENTER, p, 297, 550, 0);
    PdfContentByte over = stamper.getOverContent(1);
    p = new Phrase("This watermark is added ON TOP OF the existing content", f);
    ColumnText.showTextAligned(over, Element.ALIGN_CENTER, p, 297, 500, 0);
    p = new Phrase("This TRANSPARENT watermark is added ON TOP OF the existing content", f);
    over.saveState();
    PdfGState gs1 = new PdfGState();
    gs1.setFillOpacity(0.5f);
    over.setGState(gs1);
    ColumnText.showTextAligned(over, Element.ALIGN_CENTER, p, 297, 450, 0);
    over.restoreState();
    stamper.close();
    reader.close();
}

}

请参考此处iText知识库中的示例,了解如何使用事件添加水印https://kb.itextpdf.com/home/it5kb/examples/page-events-for-watermarking