如何使用 PdfBox 创建 pdf 包?
How to create pdf package using PdfBox?
我正在迁移一些代码(最初使用 iText)以使用 PdfBox 进行 PDF 合并。除了创建 PDF 包或投资组合外,一切都很顺利。我不得不承认,直到现在我才意识到它的存在。
这是我的代码片段(使用 iText):
PdfStamper stamper = new PdfStamper(reader, out);
stamper.makePackage(PdfName.T);
stamper.close();
我需要这个但是有 PdfBox。
我正在研究 API 和两者的文档,但我找不到解决方案 atm。任何帮助都会很棒。
PS。抱歉,如果我给人的印象是我需要 iText 中的解决方案,我需要 PdfBox 中的解决方案,因为迁移是从 iText 到 PdfBox。
据我所知,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 "Collections" 部分(我还没有第二部分)这意味着我们必须添加一个 Collection 字典使用 View 条目和值为 T 的 PDF 目录。因此:
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(...);
我正在迁移一些代码(最初使用 iText)以使用 PdfBox 进行 PDF 合并。除了创建 PDF 包或投资组合外,一切都很顺利。我不得不承认,直到现在我才意识到它的存在。
这是我的代码片段(使用 iText):
PdfStamper stamper = new PdfStamper(reader, out);
stamper.makePackage(PdfName.T);
stamper.close();
我需要这个但是有 PdfBox。
我正在研究 API 和两者的文档,但我找不到解决方案 atm。任何帮助都会很棒。
PS。抱歉,如果我给人的印象是我需要 iText 中的解决方案,我需要 PdfBox 中的解决方案,因为迁移是从 iText 到 PdfBox。
据我所知,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 "Collections" 部分(我还没有第二部分)这意味着我们必须添加一个 Collection 字典使用 View 条目和值为 T 的 PDF 目录。因此:
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(...);