【问题标题】:How to show a limited number of pages of PDF file in JSP using iText?如何使用 iText 在 JSP 中显示有限页数的 PDF 文件?
【发布时间】:2011-08-27 07:14:17
【问题描述】:

我必须在 JSP 页面中显示 PDF 文档。 PDF 文档有 25 页,但我只想显示 PDF 文件的 10 页。如何在 iText 的帮助下实现这一目标?

【问题讨论】:

  • 您是使用 iText 创建 PDF 还是必须显示/修改现有文档?

标签: java jsp servlets itext


【解决方案1】:

假设您已经拥有 PDF 文件。

您可以使用PdfStamperPdfCopy 对PDF 进行切片:

PdfReader reader = new PdfReader("THE PDF SOURCE");

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Document document = new Document();
PdfCopy copy = new PdfCopy(document, outputStream);
document.open();
PdfStamper stamper = new PdfStamper(reader, outputStream);
for (int i = 1; i < reader.getNumberOfPages(); i++) {
   // Select what pages you need here
   PdfImportedPage importedPage = stamper.getImportedPage(reader, i);
   copy.addPage(importedPage);
}
copy.freeReader(reader);
outputStream.flush();
document.close();

// Now you can send the byte array to your user
// set content type to application/pdf 

至于发送 pdf 显示,这取决于你显示它的方式。输出流将在提供的代码末尾包含您在循环中复制的页面,在示例中它是所有页面。

这本质上是一个新的 PDF 文件,但在内存中。如果每次都是同一个文件的10页,可以考虑另存为文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-12
    • 2011-06-04
    • 2012-11-28
    相关资源
    最近更新 更多