自己整理的java版的PDF分割实用代码
来源:互联网 发布:keil 端口电压查看 编辑:程序博客网 时间:2024/05/16 13:08
http://zhengjj-2009.iteye.com/blog/1841190原文地址
最近在上下班的路上看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();
- }
- }
- }
阅读全文
0 0
- 自己整理的java版的PDF分割实用代码
- 自己整理的java版的PDF分割实用代码
- 实用phpcms v9代码(自己整理的)
- 一些自己整理实用代码
- PDF分割的制作方法
- 自己的Matlab代码整理
- 自己写的部分实用的代码
- 自己写的部分实用的代码
- 实用的keil5代码整理小技巧
- java程序员第五课 分享下自己之前录制的视频汇总以及整理的pdf
- 实用的java代码片段
- 实用的java代码片段
- PDF文件的分割方法
- pdf分割的三步骤
- PDF分割器的问题
- 免费的pdf 分割工具
- 自己整理的一套java编码规则,让自己写代码不会那么乱
- 自己的小代码整理库
- git快速入门之二:将远程仓库克隆到本地
- 六.Python爬虫Beautiful Soup的用法
- 为什么在js中要写var this =that?
- Scala File 文件操作
- 打地鼠小游戏
- 自己整理的java版的PDF分割实用代码
- 24天学会设计模式------代理模式
- Go批量修改文件名
- 五种主要多核并行编程方法分析与比较
- 后数字化时代的商业与技术领导力
- 八数码算法研究(转载)
- bootstrap tab手机端无法在同一行显示
- 算法设计与分析 Ackerman函数的递归实现算法
- 24天学会设计模式------代理模式