简体   繁体   English

使用iText5向PDF添加水印

[英]Adding watermark to PDF using iText5

I want to add a watermark to the existing pdf using PdfPageEvent but when I try to do that, it's not working as I want.我想使用PdfPageEvent向现有的 pdf 添加水印,但是当我尝试这样做时,它无法正常工作。

here is my code,这是我的代码,

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 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));
    }
}

You can add watermarks even without using PdfPageEvent in iText 5.5.X.即使不使用 iText 5.5.X 中的 PdfPageEvent,您也可以添加水印。 Please refer to the following working example.请参考以下工作示例。

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();
}

} }

Please refer to the example here at iText knowledge base on how to add watermarks with events https://kb.itextpdf.com/home/it5kb/examples/page-events-for-watermarking请参考 iText 知识库中的示例,了解如何使用事件添加水印https://kb.itextpdf.com/home/it5kb/examples/page-events-for-watermarking

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM