java 使用 iText 从 pdf 文件中提取一页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28382299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:29:47  来源:igfitidea点击:

extracting one page from pdf file using iText

javagoogle-app-enginepdfitext

提问by Ahmed

I want to return one page from pdf files from java servlet (to reduce file size download), using itext library. using this code

我想使用 itext 库从 java servlet 的 pdf 文件返回一页(以减少文件大小下载)。使用此代码

     try {
        PdfReader reader = new PdfReader(input);
        Document document = new Document(reader.getPageSizeWithRotation(page_number) );


        PdfSmartCopy copy1 = new PdfSmartCopy(document, response.getOutputStream());
        copy1.setFullCompression();
        document.open();

        copy1.addPage(copy1.getImportedPage(reader, page_i) );
        copy1.freeReader(reader);
        reader.close();

        document.close();

    } catch (DocumentException e) {
        e.printStackTrace();
    }

this code returns the page, but the file size is large and some times equals the original file size, even it is just a one page.

此代码返回页面,但文件大小很大,有时甚至等于原始文件大小,即使它只是一页。

回答by Bruno Lowagie

I have downloaded a single file from your repository: Abdomen.pdf

我从您的存储库下载了一个文件:Abdomen.pdf

I have then used the following code to "burst" that PDF:

然后我使用以下代码“爆破”该 PDF:

public static void main(String[] args) throws DocumentException, IOException {
    PdfReader reader = new PdfReader("resources/Abdomen.pdf");
    int n = reader.getNumberOfPages();
    reader.close();
    String path;
    PdfStamper stamper;
    for (int i = 1; i <= n; i++) {
        reader = new PdfReader("resources/abdomen.pdf");
        reader.selectPages(String.valueOf(i));
        path = String.format("results/abdomen/p-%s.pdf", i);
        stamper = new PdfStamper(reader,new FileOutputStream(path));
        stamper.close();
        reader.close();
    }
}

To "burst" means to split in separate pages. While the original file Abdomen.pdf is 72,570 KB (about 70.8 MB), the separate pages are much smaller:

“burst”意味着分成不同的页面。虽然原始文件 Abdomen.pdf 为 72,570 KB(约 70.8 MB),但单独的页面要小得多:

enter image description here

在此处输入图片说明

I can not reproduce the problem you describe.

我无法重现您描述的问题。

回答by Marc Magon

A bit more updated and a lot cleaner (5.5.6 and up) :

更新了一点,更干净了(5.5.6 及更高版本):

/**
 * Manipulates a PDF file src with the file dest as result
 * @param src the original PDF
 * @param dest the resulting PDF
 * @throws IOException
 * @throws DocumentException
 */
public void manipulatePdf(String src, String dest)
    throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    SmartPdfSplitter splitter = new SmartPdfSplitter(reader);
    int part = 1;
    while (splitter.hasMorePages()) {
        splitter.split(new FileOutputStream("results/merge/part_" + part + ".pdf"), 200000);
        part++;
    }
    reader.close();
}