PDF Portfolios
This example demonstrates the use of the PDF Portfolio feature (sometimes called Portable Collections or PDF Packages). A PDF Portfolio can contain multiple files integrated into a single PDF.
Files in a PDF Portfolio can be in a wide range of file types created in different applications. For example, a PDF Portfolio can include text documents, e-mail messages, spreadsheets, CAD drawings, and PowerPoint presentations. Files added to a PDF Portfolio are not converted to PDF, and you can edit or modify them in their native application without removing them from the Portfolio.
PDF Portfolios offer several advantages over merging multiple files into a single PDF. For more information, see Adobe's Overview of PDF Portfolios or see this article for examples of creating PDF Portfolios in newer versions of iText.
portablecollection
JAVA
package sandbox.collections;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfFileSpecification;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.collection.PdfCollection;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
@WrapToTest
public class PortableCollection {
public static final String DEST = "results/collections/portable_collection.pdf";
public static final String DATA = "resources/data/united_states.csv";
public static final String HELLO = "resources/pdfs/hello.pdf";
public static final String IMG = "resources/images/berlin2013.jpg";
public static void main(String[] args) throws IOException,
DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new PortableCollection().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
document.add(new Paragraph("Portable collection"));
PdfCollection collection = new PdfCollection(PdfCollection.TILE);
writer.setCollection(collection);
PdfFileSpecification fileSpec = PdfFileSpecification.fileEmbedded(
writer, DATA, "united_states.csv", null);
writer.addFileAttachment("united_states.csv", fileSpec);
fileSpec = PdfFileSpecification.fileEmbedded(
writer, HELLO, "hello.pdf", null);
writer.addFileAttachment("hello.pdf", fileSpec);
fileSpec = PdfFileSpecification.fileEmbedded(
writer, IMG, "berlin2013.jpg", null);
writer.addFileAttachment("berlin2013.jpg", fileSpec);
document.close();
}
}
Resources
united_states.csv
hello.pdf
berlin2013.jpg