java inserts data into pdf (pdfbox+poi)

Specify page number insert / replace

pdfbox doesn't seem to provide this method specifically, but the existing methods can also realize this function by multiple combinations,

Requirements: A pdf file A has 10 pages. Now I want to insert A new pdf file B on page 6. After insertion, the whole pdf file A becomes 11 pages.

Train of thought 1 (insert):

First split the 10 PDFs into 10 1-page PDFs and put them in order. The file names are: 1 pdf,2.pdf....10.pdf. When you split it to page 6, put in file B and rename it Pdf, page 6 of the original PDF file A is renamed 7 Pdf, pushed back in turn, the final 1 pdf----->11.pdf, A total of 11 documents

Then make the merge function merge the 11 PDFs in order.

Train of thought 2 (replacement):

On the basis of insertion, when splitting, discard the sixth page in pdf file A and use A new page to name it 6 instead pdf, and then the merger is over.




2.Implementation method

 * Specify page number insert page
 * @param filename1  Source pdf path
 * @param filename2  pdf path to insert
 * @param number     Inserted page number
 * @param newfilename   New pdf path
 * @throws Exception
public void insertPage(String filename1,String filename2,int number,String newfilename,String tempPath) throws Exception {
    PDDocument pdf1 = PDDocument.load(new File(filename1));
    PDDocument pdf2 = PDDocument.load(new File(filename2));

    //1. Take the first pdf apart by page number
    Splitter splitter = new Splitter();
    List<PDDocument> Pages = splitter.split(pdf1);

    Iterator<PDDocument> iterator = Pages.listIterator();

    PDFMergerUtility PDFmerger = new PDFMergerUtility();

    int i = 1;
    while(iterator.hasNext()) {
            System.out.println("Currently inserted page number:"+number);
  "/"+ i +".pdf");
        PDDocument pd =;
        String tempFile = tempPath+"/"+ i +".pdf";
        System.out.println("Start split:"+tempFile);;

    //2. Start restructuring

    //The i above is added once more at last. There is no waiting here
    for(int j=1;j<i;j++){
        String tempFile = tempPath+"/"+ j +".pdf";
        System.out.println("Start merge:"+tempFile);

    //Merge documents
    System.out.println("Document merge complete");


void insertPage() throws Exception {
    PdfUtils pdfUtils = new PdfUtils();
    String filename1 = "F:\\Users\\admin\\Desktop\\A.pdf";
    String filename2 = "F:\\Users\\admin\\Desktop\\B.pdf";
    String newfilename = "F:\\Users\\admin\\Desktop\\newA.pdf";
    String tempPath = "F:\\Users\\admin\\Desktop\\temp";
    int insertNum = 32;


A few wordy words

1. The page I am going to modify is first split out, such as page 6 here, and then (my whole page is full of pictures) after modifying the content and merging it, it is found that the size is wrong. Yes, if you hear it correctly, it is the wrong size. When I put the modified pdf into the merge, it becomes smaller ~, it turns out that I am saving the picture as pdf, Or when printing and saving as pdf, there are several types of paper sizes (A4/A3, etc.), so I quit. It's ugly.

2. At this time, use the picture insertion function of pdfbox: write the picture into the original 6 Pdf this page inside, you want to ask me why? Because the original 6 The PDF size is correct. When drawing, the starting positions x and y start from 0.

Added by Anant on Tue, 22 Feb 2022 07:40:40 +0200