Skip to main content
Skip table of contents

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 and use cases for creating PDF Portfolios with iText Core.

portablecollection

JAVA
JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.collections;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.collection.PdfCollection;
import com.itextpdf.kernel.pdf.filespec.PdfFileSpec;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class PortableCollection {
    public static final String DEST = "./target/sandbox/collections/portable_collection.pdf";

    public static final String DATA = "./src/main/resources/data/united_states.csv";
    public static final String HELLO = "./src/main/resources/pdfs/hello.pdf";
    public static final String IMG = "./src/main/resources/img/berlin2013.jpg";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new PortableCollection().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);

        doc.add(new Paragraph("Portable collection"));
        
        PdfCollection collection = new PdfCollection();
        collection.setView(PdfCollection.TILE);
        pdfDoc.getCatalog().setCollection(collection);

        addFileAttachment(pdfDoc, DATA, "united_states.csv");
        addFileAttachment(pdfDoc, HELLO, "hello.pdf");
        addFileAttachment(pdfDoc, IMG, "berlin2013.jpg");

        doc.close();
    }

    // This method adds file attachment to the pdf document
    private void addFileAttachment(PdfDocument document, String attachmentPath, String fileName) throws IOException {
        String embeddedFileName = fileName;
        String embeddedFileDescription = fileName;
        String fileAttachmentKey = fileName;

        // the 5th argument is the mime-type of the embedded file;
        // the 6th argument is the AFRelationship key value.
        PdfFileSpec fileSpec = PdfFileSpec.createEmbeddedFileSpec(document, attachmentPath, embeddedFileDescription,
                embeddedFileName, null, null);
        document.addFileAttachment(fileAttachmentKey, fileSpec);
    }
}
C#
C#
using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Collection;
using iText.Kernel.Pdf.Filespec;
using iText.Layout;
using iText.Layout.Element;

namespace iText.Samples.Sandbox.Collections
{
    public class PortableCollection
    {
        public static readonly String DEST = "results/sandbox/collections/portable_collection.pdf";

        public static readonly String DATA = "../../../resources/data/united_states.csv";
        public static readonly String HELLO = "../../../resources/pdfs/hello.pdf";
        public static readonly String IMG = "../../../resources/img/berlin2013.jpg";

        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();

            new PortableCollection().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc);

            doc.Add(new Paragraph("Portable collection"));
            
            PdfCollection collection = new PdfCollection();
            collection.SetView(PdfCollection.TILE);
            pdfDoc.GetCatalog().SetCollection(collection);

            AddFileAttachment(pdfDoc, DATA, "united_states.csv");
            AddFileAttachment(pdfDoc, HELLO, "hello.pdf");
            AddFileAttachment(pdfDoc, IMG, "berlin2013.jpg");

            doc.Close();
        }

        // This method adds file attachment to the pdf document
        private void AddFileAttachment(PdfDocument document, String attachmentPath, String fileName)
        {
            String embeddedFileName = fileName;
            String embeddedFileDescription = fileName;
            String fileAttachmentKey = fileName;

            // the 5th argument is the mime-type of the embedded file;
            // the 6th argument is the AFRelationship key value.
            PdfFileSpec fileSpec = PdfFileSpec.CreateEmbeddedFileSpec(document, attachmentPath, embeddedFileDescription,
                embeddedFileName, null, null);
            document.AddFileAttachment(fileAttachmentKey, fileSpec);
        }
    }
}

Resources

united_states.csv
hello.pdf
berlin2013.jpg

Results

cmp_portable_collection.pdf

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.