Replace PDF page using PDFBox
java, pdf-generation, pdfbox
Solution
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.
Problem
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?