java操作pdf——基礎功能實現
阿新 • • 發佈:2018-12-01
正經學徒,佛系記錄,不搞事情
itextpdf 是java用於操作pdf檔案的工具,速度極快,目前測試上千頁的pdf檔案都不在話下
下面編寫的工具類,可看作是將pdf檔案轉成類似list的集合,通過對list的CURD操作,最終生成一個新的list集合,即生成一個新的pdf檔案
提供的方法:
- pdf追加pdf
- pdf插入pdf 指定序列號下
- 刪除pdf某頁
- 交換pdf指定的兩頁
第一步:新增 pom 依賴
<!--新增操作pdf依賴--> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.13</version> </dependency>
第二步:建立工具類 PDFUtil.java
第三步:定義最終生成的pdf檔案
這裡定義一個temp.pdf檔案,無需建立可直接生成,實際專案中,可使用時間戳+業務規則作為最終生成的檔名。
public static String PDF_PATH = "D:\\pdf\\temp.pdf";
第四步:編寫工具類
/** * @description 追加pdf 在headReader後面追加tailReader * @param headReader PdfReader * @param tailReader PdfReader * @return out */ public static FileOutputStream pdfFollowPdf(PdfReader headReader, PdfReader tailReader){ //建立文件 Document document = new Document(); PdfWriter writer; FileOutputStream out = null; try { //最終檔案輸出 out = new FileOutputStream(PDF_PATH); writer = PdfWriter.getInstance(document, out); //開啟文件 document.open(); PdfContentByte cb = writer.getDirectContent(); int currentPage=1; while(currentPage<=headReader.getNumberOfPages()){ document.newPage(); //獲取某一頁 PdfImportedPage page = writer.getImportedPage(headReader, currentPage); cb.addTemplate(page, 0, 0); currentPage++; } currentPage=1; while(currentPage<=tailReader.getNumberOfPages()){ document.newPage(); //獲取某一頁 PdfImportedPage page = writer.getImportedPage(tailReader, currentPage); cb.addTemplate(page, 0, 0); currentPage++; } document.close(); out.flush(); out.close(); } catch (DocumentException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return out; } /** * @description 插入pdf 指定序列號下 * @param headReader PdfReader * @param tailReader PdfReader * @param int insertPage * @return out FileOutputStream */ public static FileOutputStream pdfIntoPdf(PdfReader headReader,PdfReader tailReader,int insertPage){ if(insertPage>headReader.getNumberOfPages()+1){ System.out.println("需插入的頁號超過總頁數"); return null; } FileOutputStream out = null; Document document = new Document(); PdfWriter writer; //最終檔案輸出 try { out = new FileOutputStream(PDF_PATH); writer = PdfWriter.getInstance(document, out); document.open(); PdfContentByte cb = writer.getDirectContent(); int headCurrentPage=1; int tailCurrentPage=1; int currentPage=1; while(currentPage<=headReader.getNumberOfPages()+tailReader.getNumberOfPages()){ document.newPage(); if(currentPage==insertPage){ while(tailCurrentPage<=tailReader.getNumberOfPages()){ //獲取某一頁 PdfImportedPage page = writer.getImportedPage(tailReader, tailCurrentPage); cb.addTemplate(page, 0, 0); tailCurrentPage++; currentPage++; } }else{ //獲取某一頁 PdfImportedPage page = writer.getImportedPage(headReader, headCurrentPage); cb.addTemplate(page, 0, 0); headCurrentPage++; currentPage++; } } document.close(); out.flush(); out.close(); } catch (DocumentException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return out; } /** * @description 刪除指定頁的pdf * @param headReader PdfReader * @param deletePage int * @return out FileOutputStream */ public static FileOutputStream delPdfByIndex(PdfReader headReader,int deletePage){ if(deletePage>headReader.getNumberOfPages()){ System.out.println("需刪除的頁號超過總頁數"); return null; } //處理需要的頁數 StringBuffer showPages=new StringBuffer(""); for(int i=1;i<=headReader.getNumberOfPages();i++){ if(i!=deletePage){ showPages.append(",").append(i); } } //去除第一個逗號 showPages.delete(0, 1); FileOutputStream out = null; try { out = new FileOutputStream(PDF_PATH); headReader.selectPages(showPages.toString()); PdfStamper stamp; stamp = new PdfStamper(headReader,out); stamp.close(); out.flush(); out.close(); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return out; } /** * @description 交換pdf(headIndex頁交換tailIndex頁) * @param headReader PdfReader * @param headIndex int * @param tailIndex int * @return out FileOutputStream */ public static FileOutputStream pdfChangePage(PdfReader headReader,int headIndex,int tailIndex){ if(headIndex>headReader.getNumberOfPages()||tailIndex>headReader.getNumberOfPages()){ System.out.println("需交換的頁號超過總頁數"); return null; } Document document = new Document(); PdfWriter writer; FileOutputStream out = null; try { out = new FileOutputStream(PDF_PATH); writer = PdfWriter.getInstance(document, out); document.open(); PdfContentByte cb = writer.getDirectContent(); int currentPage=1; while(currentPage<=headReader.getNumberOfPages()){ document.newPage(); if(currentPage==headIndex){ //獲取某一頁 PdfImportedPage page = writer.getImportedPage(headReader, tailIndex); cb.addTemplate(page, 0, 0); currentPage++; }else if(currentPage==tailIndex){ //獲取某一頁 PdfImportedPage page = writer.getImportedPage(headReader, headIndex); cb.addTemplate(page, 0, 0); currentPage++; }else{ //獲取某一頁 PdfImportedPage page = writer.getImportedPage(headReader, currentPage); cb.addTemplate(page, 0, 0); currentPage++; } } document.close(); out.flush(); out.close(); } catch (DocumentException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return out; }
第五步:測試
public static void main(String[] args) { InputStream headStream = null; InputStream tailStream = null; PdfReader headReader; PdfReader tailReader; try { headStream = new FileInputStream("D:\\pdf\\Spring Cloud微服務實戰.pdf"); tailStream = new FileInputStream("D:\\pdf\\test1.pdf"); headReader = new PdfReader(headStream); tailReader = new PdfReader(tailStream); //PDFUtil.pdfFollowPdf(headReader,tailReader); //PDFUtil.pdfIntoPdf(headReader,tailReader,2); //PDFUtil.delPdfByIndex(headReader,2); //PDFUtil.pdfChangePage(headReader,1,2); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
工具類地址:
https://pan.baidu.com/s/1_7rApHY5dxX044SDadQUGQ 提取碼: tgg3