Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace PDF page using PDFBox

I have two PDF files (named : A1.pdf and B1.pdf). Now I want to replace the some pages of the second PDF file (B1.pdf) with the first one (A1.pdf) programatically. In this case I am using PDFBox library.

Here is my sample code:

try {
        File file = new File("/Users/test/Desktop/A1.pdf");
        PDDocument pdDoc = PDDocument.load(file);

        PDDocument document = PDDocument.load(new File("/Users/test/Desktop/B1.pdf"));
        document.removePage(3);
        document.addPage((PDPage) pdDoc.getDocumentCatalog().getAllPages().get(0));
        document.save("/Users/test/Desktop/"+"generatedPDFBox"+".pdf");
        document.close();
     }catch(Exception e){}

The idea is to replace the 3rd page. In this implementation the page is appending to the last page of the output pdf. Can anyone help me to implement this? If not with PDFBOX. Could you please suggest some other libraries in java?

like image 742
Rivu Avatar asked Sep 29 '22 07:09

Rivu


People also ask

What is PDFBox used for?

The Apache PDFBox™ library is an open source Java tool for working with PDF documents. This project allows creation of new PDF documents, manipulation of existing documents and the ability to extract content from documents. Apache PDFBox also includes several command line utilities.

Is PDFBox free for commercial use?

Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative ...

How to remove page from PDF Java?

print(noOfPages); Remove a page from the PDF document using the removePage() method of the PDDocument class. To this method, pass the index of the page that is to be deleted. While specifying the index for the pages in a PDF document, keep in mind that indexing of these pages starts from zero.


2 Answers

This solution creates a third PDF file with the contents like you asked for. Note that pages are zerobased, so the "3" in your question must be a "2".

    PDDocument a1doc = PDDocument.load(file1);
    PDDocument b1doc = PDDocument.load(file2);
    PDDocument resDoc = new PDDocument();

    List<PDPage> a1Pages = a1doc.getDocumentCatalog().getAllPages();
    List<PDPage> b1Pages = b1doc.getDocumentCatalog().getAllPages();

    // replace the 3rd page of the 2nd file with the 1st page of the 1st one
    for (int p = 0; p < b1Pages.size(); ++p)
    {
        if (p == 2)
            resDoc.addPage(a1Pages.get(0));
        else
            resDoc.addPage(b1Pages.get(p));
    }

    resDoc.save(file3);
    a1doc.close();
    b1doc.close();
    resDoc.close();

If you want to work from the command line instead, look here: https://pdfbox.apache.org/commandline/

Then use PDFSplit and PDFMerge.

like image 197
Tilman Hausherr Avatar answered Oct 04 '22 04:10

Tilman Hausherr


I am not too familiar with how PDFBox works, but to answer your follow up I know you can accomplish what you want to do in a fairly simple manner with the Datalogics APDFL SDK. A free trial exists in case you want to look into it. Here is a code snippet so you can see how it would be done:

Document Doc1 = new Document("/Users/test/Desktop/A1.pdf");
Document Doc2 = new Document("/Users/test/Desktop/B1.pdf");

/* Delete pages on the page range 3-3*/
Doc2.deletePages(3, 3)

/* LastPage is where in Doc2 you want to insert the page, Doc1 the document from which the page is coming from, 0 is the page number in Doc1 that will be inserted first, 1 is the number of pages that will be inserted (beginning from the page number specified in the previous parameter), and PageInsertFlags which would let you customize what gets / doesn't get copied */
Doc2.insertPages(Document.LastPage, Doc1, 0, 1, PageInsertFlags.All);

Doc2.save(EnumSet.of(SaveFlags.FULL), "out.pdf")

Alternatively, there is another method called replacePages which makes the deletion unnecessary. It all depends on what your end goal is, of course.

like image 27
Arnulfo Arroyo Avatar answered Oct 04 '22 03:10

Arnulfo Arroyo