Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to insert data into pdf with pdfbox+poi in java

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you the relevant knowledge points about how to insert data into pdf using pdfbox+poi in java. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

Specify page number insertion / replacement

Pdfbox does not seem to specifically provide this method, but the existing methods can also be combined to achieve this function.

Requirements: a pdf file A has 10 pages. Now you want to insert a new pdf file B on page 6. After the insertion is completed, the entire pdf file A becomes 11 pages.

Idea 1 (insert):

First split the 10 pdf into 10 1-page pdf, put them in order, the file names are: 1.pdf, 2.pdf....10.pdf. When you split to page 6, put the file B in, rename it and ask 6.pdf. Page 6 in the original pdf file A was renamed to 7.pdf, then pushed back in turn, and the final 1.pdf-> 11.pdf has a total of 11 files.

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

Idea 2 (replacement):

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

1.pom org.apache.pdfbox pdfbox-tools 2.0.25 net.sf.cssbox pdf2dom 2.0.1 com.itextpdf itextpdf 5.5.10 com.itextpdf.tool xmlworker 5.5.10 org.apache.poi poi-ooxml 3.15 org.apache.poi poi-scratchpad 3.152. Implementation method / * * from fhadmin.cn * specify page number insertion page * @ param filename1 source pdf path * @ param filename2 pdf path to be inserted * @ 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, disassemble the first pdf by page number Splitter splitter = new Splitter (); List Pages = splitter.split (pdf1); Iterator 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"); iTunes;} PDDocument pd = iterator.next (); String tempFile = tempPath+ "/" + I + ".pdf"; System.out.println ("start split:" + tempFile); pd.save (tempFile); iTunes;} / / 2, start reassembling PDFmerger.setDestinationFileName (newfilename) / / the I above has been added for the last time. Here we do not take the for (int job1witj).

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report