码迷,mamicode.com
首页 > 编程语言 > 详细

java从一个pdf中取出指定页生成一个新的pdf

时间:2020-06-23 19:12:22      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:file   tst   trace   stat   int end   read   import   fread   str   

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;
            }
            
            document = new Document(reader.getPageSize(1));
            copy = new PdfCopy(document, new FileOutputStream(newFile));
            document.open();
            for (int j = from; j <= end; j++) {
                document.newPage();
                PdfImportedPage page = copy.getImportedPage(reader, j);
                copy.addPage(page);
            }
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

  取出指定页的pdf生成新的pdf,参数说明:

pdfFile为源pdf,
newFile为需要新生成的pdf,
from为需要截取的起始页,

end为需要截取的末页。

java从一个pdf中取出指定页生成一个新的pdf

标签:file   tst   trace   stat   int end   read   import   fread   str   

原文地址:https://www.cnblogs.com/taleche/p/13183521.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!