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.

1.pom

<!--pdfbox-->
<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox-tools</artifactId>
    <version>2.0.25</version>
</dependency>
<dependency>
    <groupId>net.sf.cssbox</groupId>
    <artifactId>pdf2dom</artifactId>
    <version>2.0.1</version>
</dependency>

<!--poi-->
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.10</version>
</dependency>
<dependency>
    <groupId>com.itextpdf.tool</groupId>
    <artifactId>xmlworker</artifactId>
    <version>5.5.10</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.15</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-scratchpad</artifactId>
    <version>3.15</version>
</dependency>


2.Implementation method

/**from fhadmin.cn
 * 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()) {
        if(i==number){
            System.out.println("Currently inserted page number:"+number);
            pdf2.save(tempPath+"/"+ i +".pdf");
            i++;
        }
        PDDocument pd = iterator.next();
        String tempFile = tempPath+"/"+ i +".pdf";
        System.out.println("Start split:"+tempFile);
        pd.save(tempFile);
        i++;
    }

    //2. Start restructuring
    PDFmerger.setDestinationFileName(newfilename);

    //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);
        PDFmerger.addSource(tempFile);
    }

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

    pdf1.close();
    pdf2.close();
}


3.test
//from fhadmin.cn
@Test
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;

    pdfUtils.insertPage(filename1,filename2,insertNum,newfilename,tempPath);
}

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