I am trying to add a watermark to a PDF specifically with PDFBox. I've been able to get the image to appear on each page, but it loses the background transparency because it appears as though PDJpeg converts it to a JPG. Perhaps there's a way to do it using PDXObjectImage.
Here is what I have written thus far:
public static void watermarkPDF(PDDocument pdf) throws IOException
{
// Load watermark
BufferedImage buffered = ImageIO.read(new File("C:\\PDF_Test\\watermark.png"));
PDJpeg watermark = new PDJpeg(pdf, buffered);
// Loop through pages in PDF
List pages = pdf.getDocumentCatalog().getAllPages();
Iterator iter = pages.iterator();
while(iter.hasNext())
{
PDPage page = (PDPage)iter.next();
// Add watermark to individual page
PDPageContentStream stream = new PDPageContentStream(pdf, page, true, false);
stream.drawImage(watermark, 100, 0);
stream.close();
}
try
{
pdf.save("C:\\PDF_Test\\watermarktest.pdf");
}
catch (COSVisitorException e)
{
e.printStackTrace();
}
}
Just made this piece of code to add (transparent) images (jpg, png, gif) to a pdf page with pdfbox:
Example:
This worked for me with jdk 1.7 and bcmail-jdk16-140.jar, bcprov-jdk16-140.jar, commons-logging-1.1.3.jar, fontbox-1.8.3.jar, jempbox-1.8.3.jar and pdfbox-1.8.3.jar.
@Androidman : Addition to https://stackoverflow.com/a/9382212/7802973
It seems like many methods are removed with each version of PDFBox. So that code will not work on PDFBox 2.0.7.
Instead, use
void org.apache.pdfbox.pdmodel.PDDocument.save(String fileName)
, I think:Edit: I am using
org.apache.pdfbox.tools.OverlayPDF
for overlays now and it works just fine. The code looks like this:As I had not enough reputation to add a comment, I thought it'd be appropiate to add this in a new answer.
UPDATED ANSWER (Better version with easy way to watermark, thanks to the commentators below and @okok who provided input with his answer)
If you are using PDFBox 1.8.10 or above, you can add watermark to your PDF document easily with better control over what pages needs to be watermarked. Assuming you have a one page PDF document that has the watermark image, you can overlay this on the document you want to watermark as follows.
Sample Code using 1.8.10
Sample using PDFBox 2.0.0 Release candidate
If you want to use the new package org.apache.pdfbox.tools.OverlayPDF for overlays you can do this way. (Thanks the poster below)
OLD ANSWER Inefficient way, not recommended.
Well, OP asked how to do it in PDFBox, the first answer looks like an example using iText. Creating a watermark in PDFBox is really simple. The trick is, you should have an empty PDF document with the watermark image. Then all you have to do is Overlay this watermark document on the document that you want to add the watermark to.
Caution: You should make sure you match the number of pages in both document..Otherwise, you would end up with a document with number of pages matching the one which has least number of pages. You can manipulate the watermark document and duplicate the pages to match your document.
Hope this helps.!
Look at this method, whitch add a watermark image in pdf sources using PDFBOX library
There is another Overlay class within util package, that saves you from creating a pdf with same number of pages as the source document and then doing the overlay.
To understand its usage, take a look at pdfbox source code, specifically the OverlayPDF class.