Rivu
Rivu

Reputation: 177

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?

Upvotes: 5

Views: 3051

Answers (2)

Tilman Hausherr
Tilman Hausherr

Reputation: 18861

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.

Upvotes: 4

Arnulfo Arroyo
Arnulfo Arroyo

Reputation: 208

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.

Upvotes: 1

Related Questions