Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7528911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:30:52+00:00 2026-05-30T04:30:52+00:00

I am trying to add a watermark to a PDF specifically with PDFBox. I’ve

  • 0

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();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:30:53+00:00Added an answer on May 30, 2026 at 4:30 am

    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

    import java.util.HashMap;
    import org.apache.pdfbox.pdmodel.PDDocument;
    import org.apache.pdfbox.util.Overlay;
    
    public class TestPDF {
        public static void main(String[] args) throws Exception{
                PDDocument realDoc = PDDocument.load("originaldocument.pdf"); 
                //the above is the document you want to watermark                   
    
                //for all the pages, you can add overlay guide, indicating watermark the original pages with the watermark document.
                HashMap<Integer, String> overlayGuide = new HashMap<Integer, String>();
                for(int i=0; i<realDoc.getPageCount(); i++){
                    overlayGuide.put(i+1, "watermark.pdf");
                    //watermark.pdf is the document which is a one page PDF with your watermark image in it. Notice here that you can skip pages from being watermarked.
                }
                Overlay overlay = new Overlay();
                overlay.setInputPDF(realDoc);
                overlay.setOutputFile("final.pdf");
                overlay.setOverlayPosition(Overlay.Position.BACKGROUND);
                overlay.overlay(overlayGuide,false);
               //final.pdf will have the original PDF with watermarks.
    

    Sample using PDFBox 2.0.0 Release candidate

    import java.io.File;
    import java.util.HashMap;
    import org.apache.pdfbox.multipdf.Overlay;
    import org.apache.pdfbox.pdmodel.PDDocument;
    
    public class TestPDF {
    
        public static void main(String[] args) throws Exception{        
            PDDocument realDoc = PDDocument.load(new File("originaldocument.pdf"));
            //the above is the document you want to watermark
            //for all the pages, you can add overlay guide, indicating watermark the original pages with the watermark document.
    
            HashMap<Integer, String> overlayGuide = new HashMap<Integer, String>();
            for(int i=0; i<realDoc.getNumberOfPages(); i++){
                overlayGuide.put(i+1, "watermark.pdf");
                //watermark.pdf is the document which is a one page PDF with your watermark image in it. 
                //Notice here, you can skip pages from being watermarked.
            }
            Overlay overlay = new Overlay();
            overlay.setInputPDF(realDoc);
            overlay.setOutputFile("final.pdf");
            overlay.setOverlayPosition(Overlay.Position.BACKGROUND);
            overlay.overlay(overlayGuide);      
        }
    }
    

    If you want to use the new package org.apache.pdfbox.tools.OverlayPDF for overlays you can do this way. (Thanks the poster below)

    String[] overlayArgs = {"C:/Examples/foreground.pdf", "C:/Examples/background.pdf", "C:/Examples/resulting.pdf"};
    OverlayPDF.main(overlayArgs);
    System.out.println("Overlay finished.");
    

    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.

    PDDocument watermarkDoc = PDDocument.load("watermark.pdf");
    //Assuming your empty document with watermark image in it.
    
    PDDocument realDoc = PDDocument.load("document-to-be-watermarked.pdf");
    //Let's say this is your document that you want to watermark. For example sake, I am opening a new one, you would already have a reference to PDDocument if you are creating one
    
    Overlay overlay = new Overlay();
    overlay.overlay(realDoc,watermarkDoc);
    watermarkDoc.save("document-now-watermarked.pdf");
    

    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.!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to add a watermark to an image and then save the
I have been trying add facebook login to my page. I have facebook login
I'm trying to add an image watermark to images (image sizes can go up
Friends, i m trying add image to my Jbutton using seticon method but it
Trying to add a new element to the DOM but I get all sorts
I am trying to add watermark with transparent background on OSX with ffmpeg. I
I'm trying add data triggers to the default combobox style so each text item
Im new to asp.net mvc. I'm trying add new model class but it got
Trying to add a blank sample app for a rails tutorial to GitHub, but
Trying to add a class object into a List using reflection, but when invoking

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.