Itext 如何使用PdfBox创建pdf包?

Itext 如何使用PdfBox创建pdf包?,itext,pdfbox,Itext,Pdfbox,我正在迁移一些代码(最初使用iText)以使用PdfBox进行PDF合并。除了创建PDF包或公文包,一切都进行得很顺利。我必须承认,直到现在我才意识到这一点 这是我的代码片段(使用iText): 我需要这个,但要用PdfBox 我正在研究API和文档,但找不到解决方案。任何帮助都会很好 另外,如果我给人的印象是我需要在iText中使用解决方案,我需要在PdfBox中使用它,因为要从iText迁移到PdfBox。据我所知,PdfBox不包含用于该任务的单一专用方法。另一方面,使用现有的通用PDFB

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

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

我需要这个,但要用PdfBox

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


另外,如果我给人的印象是我需要在iText中使用解决方案,我需要在PdfBox中使用它,因为要从iText迁移到PdfBox。

据我所知,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公文包似乎是对所附文档进行用户友好描述的一种方式。因此,从本质上讲,创建公文包是一个分两步的过程——首先创建一个带有附件的文档,然后添加mkl建议的上述代码,以创建这些附件的用户友好的平铺“视图”。PDFBox食谱和示例都说明了如何执行第一步:
/**
 * 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 )
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(...);