Watermarking with PDFBox

前端 未结 6 1627
说谎
说谎 2020-11-28 09:12

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 i

6条回答
  •  攒了一身酷
    2020-11-28 09:37

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

    Overlay overlay = new Overlay();
    overlay.setInputPDF(realDoc);
    // ** The method setOutputFile(String) is undefined for the type Overlay ** 
    overlay.setOutputFile("final.pdf")
    

    Instead, use void org.apache.pdfbox.pdmodel.PDDocument.save(String fileName), I think:

    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 overlayGuide = new HashMap();
        for(int i=0; i

    Edit: I am using org.apache.pdfbox.tools.OverlayPDF for overlays now and it works just fine. The code looks like this:

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

    As I had not enough reputation to add a comment, I thought it'd be appropiate to add this in a new answer.

提交回复
热议问题