0

I am not getting any tutorial for adding a text watermark in a PDF file? Can you all please guide me, I am very new to PDFBOX.

Its not duplicate, the link in the comment didn't help me. I want to add text, not an image to the pdf.

Tilman Hausherr
  • 17,731
  • 7
  • 58
  • 97
Piku
  • 11
  • 1
  • 2
  • 1
    Possible duplicate of [Watermarking with PDFBox](http://stackoverflow.com/questions/8929954/watermarking-with-pdfbox) – Boris Schegolev Sep 21 '16 at 07:12
  • *I was trying iText before this,but iText is not free. I want a open source...* - Such claims are inappropriate here. If you want to do a task using library XXX, there is no need to explain why you are not using library YYY. Instead you should have explained what you have tried and where you got stuck. Concerning the claim itself, iText *is* open source and you can use it without paying royalties as long as you follow the AGPL rules. PDFBox likewise is open source and you can use it as long as you follow the Apache License v2.0 rules. – mkl Sep 21 '16 at 08:42
  • 1
    Please have a look at the AddMessageToEachPage.java example in the source code download. Then edit your question for whatever problem is remaining. – Tilman Hausherr Sep 21 '16 at 08:51
  • thanks Tilman, I am able to add watermark,but how to add it to background using overlay, transparency. – Piku Sep 22 '16 at 04:05

1 Answers1

1

Here is an example using PDFBox 2.0.2. This will load a PDF and write some text in the bottom right corner in a red transparent font. If it is a multiple page PDF the watermark will appear on every page. It might not be production ready, as I am not sure if there are some additional null conditions that need to be checked, but it should get you running in the right direction.

Keep in mind that this particular block of code will not modify the original PDF, but will create a new PDF using the Tmp_(filename) as the output.

        private static void watermarkPDF (File fileStored) {
    File tmpPDF;
    PDDocument doc;
    tmpPDF = new File(fileStored.getParent() + System.getProperty("file.separator") +"Tmp_"+fileStored.getName());
    doc = PDDocument.load(fileStored);
    for(PDPage page:doc.getPages()){
        PDPageContentStream cs = new PDPageContentStream(doc, page, AppendMode.APPEND, true, true);
        String ts = "Some sample text";
        PDFont font = PDType1Font.HELVETICA_BOLD;
        float fontSize = 14.0f;
        PDResources resources = page.getResources();
        PDExtendedGraphicsState r0 = new PDExtendedGraphicsState();
        r0.setNonStrokingAlphaConstant(0.5f);
        cs.setGraphicsStateParameters(r0);
        cs.setNonStrokingColor(255,0,0);//Red
        cs.beginText();
        cs.setFont(font, fontSize);
        cs.setTextMatrix(Matrix.getTranslateInstance(0f,0f));
        cs.showText(ts);
        cs.endText();
        }
        cs.close();
    }
    doc.save(tmpPDF);
}
MDig
  • 649
  • 1
  • 6
  • 10
  • 1
    *It might not be production ready* - to make it more so, **A** update the position calculations to consider that the coordinate system origin need not be the lower left, **B** use the `PDPageContentStream` constructor with the `resetContext` parameter and set it to `true`, and **C** simplify the `PDExtendedGraphicsState` handling, in particular stop explicitly meddling with the resources, `setGraphicsStateParameters` already adds the state to the resources if necessary. And as the OP *wants to add text, not an image to the pdf*, also drop the image adding. – mkl Sep 28 '16 at 07:09
  • Hi MKL - I just patched this together the same day I posted it from other examples online. But, from what I gather from you, I should be able to change this by removing the entire IF section and only keep the r0.setNonStrokingAlphaConstant. Is that correct? – MDig Sep 29 '16 at 13:57
  • Yes, that should fix item **C**. – mkl Sep 29 '16 at 14:09