Portfolio PDF

From
Jump to: navigation, search

Beginning with PDF 1.7, PDF documents may specify how an interactive PDF reader's user interface presents collections of file attachments, where the attachments are related in structure or content. Such a presentation is called a portable collection.

The intent of portable collections is to present, sort, and search collections of related documents embedded in the containing PDF document, such as email archives, photo collections, and engineering bid sets. There is no requirement that documents in a collection have an implicit relationship or even a similarity. However, showing differentiating characteristics of related documents can be helpful for document navigation.

Beginning with PDF 2.0, a portable collection can contain a Folders object for the purpose of organising files into a hierarchical structure. The structure is represented by a tree with a single root folder acting as the common ancestor for all other folders and files in the collection. That is what is called a portfolio PDF in other way.

Take a look at the code sample below demonstrating how one can create a PDF document carrying a set of files contained in folders, creating the portfolio PDF.

// Sample class which can be used as a base for the creation of your portfolio PDF.
public static class PortfolioDocument
{
    /// <summary>
    /// Creates the portfolio pdf file.
    /// </summary>
    /// <param name="fileName">Name of the file.</param>
    public static void CreateFile(string fileName)
    {
        using (FixedDocument document = new FixedDocument())
        {
            // create the collection schema first
            CollectionSchema schema = new CollectionSchema();
            schema.Fields.Add("title", new CollectionField("Title", CollectionFieldType.FileName, 1));
            schema.Fields.Add("description", new CollectionField("Description", CollectionFieldType.EmbeddedFileDescription, 2));
            schema.Fields.Add("owner", new CollectionField("Owner", CollectionFieldType.TextField, 3));
            schema.Fields.Add("mdate", new CollectionField("Modification Date", CollectionFieldType.ModificationDate, 4));
            schema.Fields.Add("cdate", new CollectionField("Creation Date", CollectionFieldType.CreationDate, 5));

            document.Collection.Schema = schema;
            document.Collection.View = CollectionViewMode.Detailed;

            CollectionFolder root = document.Collection.Folder;
            root.Name = "root";

            AppendEmbededFiles(root);

            CreateFolder(root, "Folder 1", "This is the 1st folder");
            CreateFolder(root, "Folder 2", "This is the 2nd folder");
            CreateFolder(root, "Folder 3", "This is the 3rd folder");
            CreateFolder(root, "Folder 4", "This is the 4th folder");


            Page page = new Page();
            document.Pages.Add(page);

            page.Content.SetTranslate(50, 780);
            TextObject text = new TextObject(StandardFonts.HelveticaBold, 20);
            text.AppendText("This document is the Portfolio PDF");
            page.Content.AppendText(text);

            using (Stream stream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite))
            {
                document.Save(stream);
            }
        }
    }

    /// <summary>
    /// Creates the folder in the files collection.
    /// </summary>
    /// <param name="parent">The collection parent.</param>
    /// <param name="folderName">Name of the folder.</param>
    /// <param name="description">The description.</param>
    private static void CreateFolder(CollectionFolder parent, string folderName, string description)
    {
        CollectionFolder folder = new CollectionFolder(folderName) { Description = description };

        CollectionItem item = new CollectionItem();
        item.SetValue("owner", "Folder Owner", false);

        folder.CollectionItem = item;

        AppendEmbededFiles(folder);

        parent.Folders.Add(folder);
    }

    /// <summary>
    /// Appends five embedded files as stubs for demo purposes.
    /// </summary>
    /// <param name="folder">The folder to add the files to.</param>
    private static void AppendEmbededFiles(CollectionFolder folder)
    {
        for (int i = 0; i < 6; i++)
        {
            string index = i.ToString();
            string fileName = string.Format("{0}_{1}.pdf", folder.Name, index);

            using (FixedDocument document = new FixedDocument())
            {
                // Create page and add simple content
                Page page = new Page();
                document.Pages.Add(page);

                page.Content.SetTranslate(50, 780);
                TextObject text = new TextObject(StandardFonts.HelveticaBold, 20);
                text.AppendText(string.Format("This document is a portfolio item in folder: {0} File#{1}",folder.Name,index));

                page.Content.AppendText(text);

                using (Stream stream = File.Create(fileName))
                {
                    document.Save(stream);
                }
            }
            EmbeddedFile file = new EmbeddedFile(fileName);
            file.Description = "This document is a portfolio item #: " + index;
            file.CollectionItem = new CollectionItem();
            file.CollectionItem.SetValue("owner", "File Owner", false);

            folder.Files.Add(file);
        }
    }
}
 

And the result looks as follows:

Portfolio PDF document