依赖:

<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();
}
}

最新文章

  1. StructureMap 代码分析之Widget 之Registry 分析 (1)
  2. Matlab中使用脚本和xml文件自动生成bus模块
  3. 备忘:aliyun maven mirror
  4. [转]finished with non-zero exit value 2
  5. Web前端知识技能大汇总
  6. WPF命令参数CommandParameter
  7. python urllib2的proxyhandler
  8. P、NP、NP-Complete、NP-hard问题
  9. C#的装箱和拆箱
  10. 201521123080《Java程序设计》第14周学习总结
  11. .net 分布式的未来:微服务
  12. MyCat基础安装配置-笔记(一)
  13. Python学习笔记【Supervisor】:使用Supervisor监控Tornado进程
  14. clion打断点不生效
  15. Axis2之异步调用
  16. vijos1448校门外的树
  17. 2019.02.09 codeforces451 E. Devu and Flowers(容斥原理)
  18. CNN基础
  19. nginx解析漏洞
  20. 使用openstack部署云计算服务环境

热门文章

  1. Alpha_4
  2. MySQL优化——MySQL 生产环境下 my.cnf 优化配置
  3. Python学习日记(二十七) 反射和几个内置函数
  4. LeetCode初级算法--其他02:有效的括号
  5. linux下写tomcat启动,重启的脚本
  6. 《基于 UML 的教务系统设计方法研究》论文笔记(十五)
  7. 同步关键词synchronized
  8. zabbix-agent主动模式和proxy
  9. Laravel —— 路由问题
  10. django-缓存django-redis