最近在上下班的路上看pdf文件比较多,想把整本书dpdf分割成对应的章节,所以自己看了一些参考资料后,自己写了一个小程序,实现了自己的想法。
我的基本需求是:提供一个pdf文件的全路径 + 新生成pdf文件名称 + 起始页码 + 结束页码 最后就能在相同目录下找到新文件。
可以运行的代码是(需要导入的三个jar包见附件)
package com.peter.utils; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.pdf.PdfCopy; import com.lowagie.text.pdf.PdfImportedPage; import com.lowagie.text.pdf.PdfReader; public class MyPDFUtil { public static void main(String[] args) { partitionPdfFile("D:\\mag_test\\test_pdf.pdf","Chapter04.pdf", 11,23); } /** * 截取pdfFile的第from页至第end页,组成一个新的文件名 * @param pdfFile * @param subfileName * @param from * @param end */ public static void partitionPdfFile(String pdfFile, String newFile, int from, int end) { Document document = null; PdfCopy copy = null; try { PdfReader reader = new PdfReader(pdfFile); int n = reader.getNumberOfPages(); if(end==0){ end = n; } ArrayList<String> savepaths = new ArrayList<String>(); String staticpath = pdfFile.substring(0, pdfFile.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的开发源码-PDF分割与合并源代码.zip 基于java的开发源码-PDF分割与合并源代码.zip 基于java的开发源码-PDF分割与合并源代码.zip 基于java的开发源码-PDF分割与合并源代码.zip 基于java的开发源码-PDF分割与...
基于Java的PDF分割与合并源代码
基于Java的PDF分割与合并源代码.zip
基于java的PDF分割与合并源代码.zip
java PDF分割与合并源代码.zip
PDF分割合并工具(用过最好的PDF分割器,不好用拍砖!)PDF分割合并工具(用过最好的PDF分割器,不好用拍砖!)PDF分割合并工具(用过最好的PDF分割器,不好用拍砖!) 郑重声明:此软件不能合并PDF,合并工具请下载...
Java经典实例.第三版(高清PDF+配套代码)
ceb 转 pdf 代码,包含java代码
我整理生成的PDF格式文件 java2实用教程(第2版)程序代码.pdf 整理了一些资料和大家分享 并非本人原创,郑重感谢原作者!
pdf 分割合并pdf 分割合并pdf 分割合并pdf 分割合并
pdf分割的小工具,把pdf文件按照自己喜欢的页面进行分割处理。把一份大的pdf文件截取自己想要的部分。pdf精准拆分使用。
JAVA 对 PDF合并与分割 范例和包一起。
java 写的转pdf代码,支持表格内容的中文换行,css样式随意设置。源代码亲测好用
java源码:PDF分割与合并源代码.rar
Java开发代码规范,用于行业内部开发的准则,包括开发标准和开发技巧。
Java常用代码-PDF版,学用例子……
重构改善既有代码的设计 改善java代码结构, 让你的代码简洁
JAVA代码审查[整理].pdf
JAVA核心知识点整理.pdf 整理了java开发中比较重要的知识点,对java开发有很大的帮助!主要介绍了 JVM,JAVA集合,多线程,Spring原理,微服务,Zookeeper等...
java生成PDF源代码 t,java生成pdf的源码。非常不错的程序。