【问题标题】:How to create pdf package using PdfBox?如何使用 PdfBox 创建 pdf 包?
【发布时间】:2018-03-20 11:01:17
【问题描述】:

我正在迁移一些代码(最初使用 iText)以使用 PdfBox 进行 PDF 合并。除了创建 PDF 包或投资组合外,一切都很好。我不得不承认,直到现在我才知道这存在。

这是我的代码的 sn-p(使用 iText):

PdfStamper stamper = new PdfStamper(reader, out);
stamper.makePackage(PdfName.T);
stamper.close();

我需要这个,但要使用 PdfBox。

我正在研究两者的 API 和文档,但我找不到解决方案 atm。任何帮助都会很棒。

PS。抱歉,如果我觉得我需要 iText 中的解决方案,我需要在 PdfBox 中使用它,因为迁移是从 iText 到 PdfBox。

【问题讨论】:

    标签: itext pdfbox


    【解决方案1】:

    据我所知,PDFBox 不包含用于该任务的单一、专用 方法。另一方面,使用现有的 generic PDFBox 方法来实现它相当容易。

    首先,任务被有效定义为做等价于

    stamper.makePackage(PdfName.T);
    

    使用 PDFBox。 iText 中的该方法记录为:

    /**
     * This is the most simple way to change a PDF into a
     * portable collection. Choose one of the following names:
     * <ul>
     * <li>PdfName.D (detailed view)
     * <li>PdfName.T (tiled view)
     * <li>PdfName.H (hidden)
     * </ul>
     * Pass this name as a parameter and your PDF will be
     * a portable collection with all the embedded and
     * attached files as entries.
     * @param initialView can be PdfName.D, PdfName.T or PdfName.H
     */
    public void makePackage( final PdfName initialView )
    

    因此,我们需要更改 PDF(相当少),使其成为具有平铺视图的便携式集合。

    根据 ISO 32000-1 的第 12.3.5 节“集合”(我还没有第二部分),这意味着我们必须在 PDF 目录中添加一个 Collection 字典,并带有 查看 值为 T 的条目。因此:

    PDDocument pdDocument = PDDocument.load(...);
    
    COSDictionary collectionDictionary = new COSDictionary();
    collectionDictionary.setName(COSName.TYPE, "Collection");
    collectionDictionary.setName("View", "T");
    PDDocumentCatalog catalog = pdDocument.getDocumentCatalog();
    catalog.getCOSObject().setItem("Collection", collectionDictionary);
    
    pdDocument.save(...);
    

    【讨论】:

    • 详细说明,PDF Portfolios 似乎是一种对附加文档进行用户友好描述的方法。所以本质上,创建一个投资组合是一个两步过程 - 首先创建一个带有附件的文档,然后添加 mkl 建议的上述代码来创建这些附件的用户友好的平铺“视图”。 PDFBox Cookbook 和示例都展示了如何进行第一步:pdfbox.apache.org/1.8/cookbook/workingwithattachments.html
    猜你喜欢
    • 2013-11-11
    • 1970-01-01
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-28
    • 1970-01-01
    相关资源
    最近更新 更多