robert
robert

Reputation: 813

How to create pdf package using PdfBox?

I was migrating some code (originally using iText) to use PdfBox for PDF merging. All went fine except creating PDF packages or portfolios. I have to admit I was not aware that this existed until now.

this is a snippet of my code (using iText):

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

I need this but with PdfBox.

I'm looking into API and docs for both and I can not find a solution atm. Any help would be great.

PS. Sorry if I made impression that I need solution in iText, I need it in PdfBox because migration is going from iText to PdfBox.

Upvotes: 2

Views: 900

Answers (1)

mkl
mkl

Reputation: 95928

As far as I know PDFBox does not contain a single, dedicated method for that task. On the other hand it is fairly easy to use existing generic PDFBox methods to implement it.

First of all, the task is effectively defined to do the equivalent to

stamper.makePackage(PdfName.T);

using PDFBox. That method in iText is documented as:

/**
 * 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 )

Thus, we need to change a PDF (fairly minimally) to make it a portable collection with a tiled view.

According to section 12.3.5 "Collections" of ISO 32000-1 (I don't have part two yet) this means we have to add a Collection dictionary to the PDF catalog with a View entry with value T. Thus:

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(...);

Upvotes: 4

Related Questions