java 从 PDF 中删除页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7312824/
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-10-30 19:30:30  来源:igfitidea点击:

Remove page from PDF

javapdfitext

提问by AlanFoster

I'm currently using iText and I'm wondering if there is a way to delete a page from a PDF file?

我目前正在使用 iText,我想知道是否有办法从 PDF 文件中删除页面?

I have opened it up with a reader etc., and I want to remove a page before it is then saved back to a new file; how can I do that?

我已经用阅读器等打开了它,我想在将页面保存回新文件之前删除它;我怎样才能做到这一点?

回答by AlanFoster

The 'better' way to 'delete' pages is doing

“删除”页面的“更好”方法是

reader.selectPages("1-5,10-12");

Which means we only select pages 1-5, 10-12 effectively 'deleting' pages 6-9.

这意味着我们只选择第 1-5、10-12 页有效地“删除”第 6-9 页。

回答by Ankit Sharma

Get the reader of existing pdf file by

通过以下方式获取现有 pdf 文件的阅读器

PdfReader pdfReader = new PdfReader("source pdf file path");

Now update the reader by

现在更新读者

 pdfReader.selectPages("1-5,15-20");

then get the pdf stamper object to write the changes into a file by

然后获取 pdf 压模对象以将更改写入文件

PdfStamper pdfStamper = new PdfStamper(pdfReader,
                new FileOutputStream("destination pdf file path"));

close the PdfStamper by

通过以下方式关闭 PdfStamper

pdfStamper.close();

It will close the PdfReader too.

它也会关闭 PdfReader。

Cheers.....

干杯.....

回答by Jes

You can use a PdfStamperin combination with PdfCopy.

您可以将 aPdfStamper与 结合使用PdfCopy

In thisanswer it is explained how to copy a whole document. If you change the criteria for the loop in the sample code you can remove the pages you don't need.

这个答案中解释了如何复制整个文档。如果您更改示例代码中的循环条件,您可以删除不需要的页面。

回答by Pavel Vlasov

Here is a removing function ready for real life usage. Proven to work ok with itext 2.1.7. It does not use "strigly typing" also.

这是一个可以在现实生活中使用的删除功能。经证明可以正常工作itext 2.1.7。它也不使用“严格打字”。

/**
 * Removes given pages from a document.
 * @param reader    document
 * @param pagesToRemove pages to remove; 1-based
 */
public static void removePages(PdfReader reader, int... pagesToRemove) {
    int pagesTotal = reader.getNumberOfPages();
    List<Integer> allPages = new ArrayList<>(pagesTotal);
    for (int i = 1; i <= pagesTotal; i++) {
        allPages.add(i);
    }
    for (int page : pagesToRemove) {
        allPages.remove(new Integer(page));
    }
    reader.selectPages(allPages);
}

回答by Ray Hulha

For iText 7 I found this example:

对于 iText 7,我找到了这个例子:

    PdfReader pdfReader = new PdfReader(PATH + name + ".pdf");
    PdfDocument srcDoc = new PdfDocument(pdfReader);
    PdfDocument resultDoc = new PdfDocument(new PdfWriter(PATH + name + "_cut.pdf"));
    resultDoc.initializeOutlines();

    srcDoc.copyPagesTo(1, 2, resultDoc);

    resultDoc.close();
    srcDoc.close();

See also here: clone-reordering-pagesand here: clone-splitting-pdf-file

另请参阅此处:clone-reordering-pages和此处:clone-splitting-pdf-file