标签:java name int 页数 param new pdf document String
依赖:
<dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox-app</artifactId> <version>1.8.10</version> </dependency>
java 用PDFBox 删除 PDF文件中的某一页,前n页,后n页,效率低,不推荐使用
package com.everjiankang; import java.io.File; import org.apache.pdfbox.pdmodel.PDDocument; /**运行效率很慢,因为每次删除一页就读取和保存一次文件,初始文件名格式:xxxx0.pdf*/ public class Test { static String name_pre = "C:\\log\\jvm"; //文件名前缀 static String name_after = ".pdf";//文件名后缀 public static void main(String[] args) { //1.刪除前n頁 // cutPdfPreNPage(2); //2.刪除后n頁 cutPdfAfterNPage(5); //3.刪除第n頁 cutPdf(name_pre + 0 + name_after,name_pre + (0+1) + name_after,7);//删除第n页 } /** * 删除前n页 * @param n */ public static void cutPdfPreNPage(int n) { for(int i = 0; i < n; i++) cutPdf(name_pre + i + name_after,name_pre + (i+1) + name_after,0); } /** * 删除后n页 * @param n */ public static void cutPdfAfterNPage(int n) { for(int i = 0; i < n; i++) cutPdf(name_pre + i + name_after,name_pre + (i+1) + name_after,1); } /** * * @param pdfPath 旧路径 * @param newPdfPath 新路径 * @param flag 0:第一页;1:最后一页 ;else : 要删除的页码 */ public static void cutPdf(String pdfPath,String newPdfPath, int flag) { File file = new File(pdfPath); PDDocument document = new PDDocument(); try{ document = PDDocument.load(file); }catch(Exception e){ e.printStackTrace(); } int noOfPages = document.getNumberOfPages(); System.out.println(noOfPages); if(flag == 0) document.removePage(0); else if(flag == 1) { document.removePage(noOfPages-1); } else { document.removePage(flag-1); } try{ document.save(newPdfPath); document.close(); }catch(Exception e){ e.printStackTrace(); } System.out.println("已经转完了哦"); } }
抽取任意范围的PDF页作为新的PDF. 效率高
依赖
<dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.13</version> </dependency>
代码
/** * 截取pdfFile的第from页至第end页,组成一个新的文件名 * @param pdfFile 需要分割的PDF * @param savepath 新PDF * @param from 起始页 * @param end 结束页 */ public static void splitPDFFile(String respdfFile, String savepath, int from, int end) { Document document = null; PdfCopy copy = null; try { PdfReader reader = new PdfReader(respdfFile); int n = reader.getNumberOfPages(); if(end==0){ end = n; } ArrayList<String> savepaths = new ArrayList<String>(); String staticpath = respdfFile.substring(0, respdfFile.lastIndexOf("\\")+1); //String savepath = staticpath+ newFile; savepaths.add(savepath); document = new Document(reader.getPageSize(1)); copy = new PdfCopy(document, new FileOutputStream(savepaths.get(0))); document.open(); for(int j=from; j<=end; j++) { document.newPage(); PdfImportedPage page = copy.getImportedPage(reader, j); copy.addPage(page); } document.close(); } catch (IOException e) { e.printStackTrace(); } catch(DocumentException e) { e.printStackTrace(); } }
标签:java,name,int,页数,param,new,pdf,document,String 来源: https://www.cnblogs.com/qianzf/p/11859051.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。