java 使用itext分割pdf_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java 使用itext分割pdf

java 使用itext分割pdf

 2018/7/17 18:20:34  mooyinn  程序员俱乐部  我要评论(0)
  • 摘要:splitpdfonline在线pdf分割功能上线了。戳这里试用http://pdfmerge.online/pdfsplit/index.html有了pdf合并功能还不够,总会遇到这种情况,下载了一本pdf文件,由于文件太大不方便阅读和传播。那么按照章节进行分割是很正常的需求,新上线的的pdf分割功能可以通过制定分割的页码进行分割,比如制定第1,3,5,9页;-会分割为三个文件,分别为:-第1到2页为一个文件-第3到8页为一个文件-第9到最后一页为一个文件核心代码
  • 标签:使用 Java
split pdf online
在线pdf分割功能上线了。

戳这里试用
http://pdfmerge.online/pdfsplit/index.html

有了pdf合并功能还不够,总会遇到这种情况,下载了一本pdf文件,由于文件太大不方便阅读和传播。
那么按照章节进行分割是很正常的需求,

新上线的的pdf分割功能可以通过制定分割的页码进行分割,

比如制定第1,3,5,9页;
- 会分割为三个文件,分别为:
- 第1到2页为一个文件
- 第3到8页为一个文件
- 第9到最后一页为一个文件



核心代码:

```
public String splitFile(String pdfFile,Integer from,Integer end){

        Document document = null;
        PdfCopy copy = null;
        try {
            PdfReader reader = new PdfReader(pdfFile);
            int n = reader.getNumberOfPages();
            if(end==0){
                end = n;
            }
            List<String> savepaths = new ArrayList<String>();
            int a = pdfFile.lastIndexOf(".pdf");
            String staticpath = pdfFile.substring(0, a);
            String savepath = staticpath+ "_from_"+from+"_to_"+end+"_.pdf";
            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();
            return new File(savepath).getName();
        } catch (IOException e) {
            logger.error("split pdf file error:{}",e.getMessage(),e);
            return null;
        } catch(DocumentException e) {
            logger.error("split pdf file error:{}",e.getMessage(),e);
            return null;
        }
    }
   
```

上一篇: 和平之翼代码生成器SHCEU 3.2版 千年隼 正在火热开发 下一篇: 没有下一篇了!
发表评论
用户名: 匿名