iText pdf library
Website search

PDF Portfolios

This example demonstrates the use of the PDF Portfolio feature (sometimes called Portable Collections). 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.

PortableCollection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/27063677/use-of-relative-path-for-anchor-method-using-itext-for-pdf-generation
 */
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.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);
    }
}
PortableCollection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
 
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);
        }
    }
}

Click the following link to see the legacy example for iText 5. Except for security fixes, iText 5 is no longer being developed.



Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now