說兩句廢話
本來我只是想把我的一個pdf文件,多余的地方刪除掉,再把個別頁面的內容改下(多余內容干掉~),然后打印出來,我想要的這些wps還有很多軟件都有,但是...
參考文獻
https://www.baeldung.com/pdf-conversions-java
https://iowiki.com/pdfbox/pdfbox_merging_multiple_pdf_documents.html
基本功能就不多說了,看上面兩個鏈接(創建、刪除、拆分、合並等)
指定頁碼插入/替換
pdfbox好像沒有專門提供這個方法,但是現有的方法多重組合起來也能實現這個功能,
需求:一個pdf文件A有10頁,現在想在第6頁插入一頁新的pdf文件B,插入完成后整個pdf文件A變成11頁。
思路1(插入):
先將這個10的pdf拆分成10個1頁的pdf,按順序放好,文件名分別是:1.pdf、2.pdf....10.pdf。再拆分到第6頁的時候將文件B放進來,重命名問6.pdf,原本pdf文件A里面的第6頁重命名為7.pdf,依次后推,最后的得到的1.pdf----->11.pdf一共11個文件
然后使合並功能將這個11個pdf按順序合並。
思路2(替換):
在插入的基礎上,拆分的時候將pdf文件A里面的第6個頁丟棄,使用新的頁面來代替它命名6.pdf,然后合並就完事了。
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>
實現方法
/** * 指定頁碼插入頁 * @param filename1 源pdf路徑 * @param filename2 需要插入的pdf路徑 * @param number 插入的頁碼 * @param newfilename 全新pdf的路徑 * @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、將第一個pdf按頁碼全部拆開 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("當前插入頁碼:"+number); pdf2.save(tempPath+"/"+ i +".pdf"); i++; } PDDocument pd = iterator.next(); String tempFile = tempPath+"/"+ i +".pdf"; System.out.println("開始拆分:"+tempFile); pd.save(tempFile); i++; } //2、開始重組 PDFmerger.setDestinationFileName(newfilename); //上面的i最后多加了一次,這里不取等 for(int j=1;j<i;j++){ String tempFile = tempPath+"/"+ j +".pdf"; System.out.println("開始合並:"+tempFile); PDFmerger.addSource(tempFile); } //合並文檔 PDFmerger.mergeDocuments(); System.out.println("文檔合並完成"); pdf1.close(); pdf2.close(); }
測試
@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); }
啰嗦幾句
1、我將要修改的頁面先拆分出來了,比如這里的第6頁,然后(我這個整頁都是圖片)將內容修改后,合並進來發現尺碼不對,是的,你沒有聽錯就是尺碼不對,當我修改后的pdf在放進來合並的時候,這一頁它變小了~,原來是我在將圖片另存為pdf,或者使用打印另存為pdf的時候,紙張大小就那么幾類(A4/A3等),那我就不干了啊,丑里吧唧的。
2、這個時候就用pdfbox的圖片插入功能:將圖片寫入原來的6.pdf這一頁里面來,你要問我為啥?因為原來的6.pdf尺碼是對的,其中畫圖的時候開始位置x,y都從0開始。
總結一下
pdfbox基本上我能想到的pdf操作幾乎都能實現,簡單、易用、好上手、白嫖。又剩下幾塊錢....