2017-10-09 77 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(...); 
相關問題