2017-10-09 95 views
1

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

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

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

我需要这个但PDFBOX。

我正在调查API和文档为两者,我无法找到解决方案atm。任何帮助都会很棒。

PS。对不起,如果我印象中我需要在iText中的解决方案,我需要它在PdfBox中,因为迁移是从iText到PdfBox。

回答

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(相当最低限度),使之成为便携式采集与平铺视图。

根据第12.3.5 ISO 32000-1的“集合”(我没有第二部分还没有),这意味着我们必须添加一个集合词典到PDF目录与查看条目以值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(...); 
相关问题