如何使用PdfBox创建pdf包?

时间:2017-10-09 09:23:30

标签: itext pdfbox

我正在迁移一些代码(最初使用iText)以使用PdfBox进行PDF合并。除了创建PDF包或组合外,一切都很顺利。我不得不承认我直到现在还没有意识到这一点。

这是我的代码片段(使用iText):

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

我需要这个但是使用PdfBox。

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

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

1 个答案:

答案 0 :(得分:2)

据我所知,PDFBox不包含该任务的单一,专用方法。另一方面,使用现有的泛型 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节“集合”(我还没有第二部分),这意味着我们必须在目录中添加集合字典,并带有< strong>查看条目,其值为 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(...);
相关问题