Home > Enterprise >  Adding watermark to PDF using iText5
Adding watermark to PDF using iText5

Time:03-19

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.

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

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

CodePudding user response:

You can add watermarks even without using PdfPageEvent in iText 5.5.X. 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

  • Related