Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8790515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:40:43+00:00 2026-06-13T22:40:43+00:00

Using the answers in How to merge PDFs into a PDF Portfolio? , I’ve

  • 0

Using the answers in How to merge PDFs into a PDF Portfolio?, I’ve been able to create an PDF portfolio using iTextSharp. However, using Adobe Acrobat I’m able to create folders, and I’m able to put files in those folders.

How do I create folders and how do I put files in those folders in a PDF portfolio using iTextSharp?

I’ve tried using a pdf inspector program to see the differences between a portfolio with and without folders, but I haven’t been able to see any. I guess I’m looking in the wrong places

EDIT

For this specific use case of mine it is actually possible to create the PDF portfolio with folder up front. So it’s way more important to be able to insert files in folders in an existing PDF portfolio as opposed to actually creating the folders themselves.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:40:45+00:00Added an answer on June 13, 2026 at 10:40 pm

    Here is some proof of concept code that creates a new portfolio pdf with couple of folders and inserts an existing pdf file into each of those folders. This approach is based on using a text editor to look at a pdf file created using the sample code linked to in the original post, then using Acrobat to create a folder and move the embedded file into that folder, saving the pdf, then looking at the changes with a text editor. In the code I’m recreating the changes found while comparing the two versions of the portfolio pdf, so while it works (at least on my machine), it may not be the best way to accomplish the task.

    If you want to view the before/after files like I did, create the portfolio using iTextsharp, then open with Acrobat and create a folder and move the embedded file(s) into the folder, then just save the file again using the save icon on the toolbar. Do not use the File -> Save As… option to save the file as a Portfolio PDF. Acrobat reorganizes the file and compresses or in some other way converts a lot of the file to binary data that you won’t be able read in a text editor. I found that deleting the binary stream data made the structure much easier to follow. Just get rid of everything between each pair of stream/endstream keywords.

    One thing that Acrobat does in portfolio pdfs is embed a flash file that provides animation and a more attractive theme for the portfolio. I wasn’t able to figure out how to do that, so the resulting file from this code is a bit plain looking.

    using System;
    using System.IO;
    using System.Linq;
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using iTextSharp.text.pdf.collection;
    
    public class FolderWriter {
        private const string Folder = @"C:\Path\to\your\pdf\files";
        private const string File1 = @"Pdf File 1.pdf";
        private const string File2 = @"Pdf File 2.pdf";
        private readonly string file1Path = Path.Combine(Folder, File1);
        private readonly string file2Path = Path.Combine(Folder, File2);
        private readonly string[] keys = new[] {
            "Type",
            "File"
        };
    
        public void Write(Stream stream) {
            using (Document document = new Document()) {
                PdfWriter writer = PdfWriter.GetInstance(document, stream);
    
                document.Open();
                document.Add(new Paragraph("This document contains a collection of PDFs"));
    
                PdfIndirectReference parentFolderObjectReference = writer.PdfIndirectReference;
                PdfIndirectReference childFolder1ObjectReference = writer.PdfIndirectReference;
                PdfIndirectReference childFolder2ObjectReference = writer.PdfIndirectReference;
    
                PdfDictionary parentFolderObject = GetFolderDictionary(0);
                parentFolderObject.Put(new PdfName("Child"), childFolder1ObjectReference);
                parentFolderObject.Put(PdfName.NAME, new PdfString());
    
                PdfDictionary childFolder1Object = GetFolderDictionary(1);
                childFolder1Object.Put(PdfName.NAME, new PdfString("Folder 1"));
                childFolder1Object.Put(PdfName.PARENT, parentFolderObjectReference);
                childFolder1Object.Put(PdfName.NEXT, childFolder2ObjectReference);
    
                PdfDictionary childFolder2Object = GetFolderDictionary(2);
                childFolder2Object.Put(PdfName.NAME, new PdfString("Folder 2"));
                childFolder2Object.Put(PdfName.PARENT, parentFolderObjectReference);
    
                PdfCollection collection = new PdfCollection(PdfCollection.DETAILS);
                PdfCollectionSchema schema = CollectionSchema();
                collection.Schema = schema;
                collection.Sort = new PdfCollectionSort(keys);
                collection.Put(new PdfName("Folders"), parentFolderObjectReference);
                writer.Collection = collection;
    
                PdfFileSpecification fs;
                PdfCollectionItem item;
    
                fs = PdfFileSpecification.FileEmbedded(writer, file1Path, File1, null);
                item = new PdfCollectionItem(schema);
                item.AddItem("Type", "pdf");
                fs.AddCollectionItem(item);
                // the description is apparently used to place the 
                // file in a particular folder.  The number between the < and >
                // is used to put the file in the folder that has the matching id
                fs.AddDescription(GetDescription(1, File1), false);
                writer.AddFileAttachment(fs);
    
                fs = PdfFileSpecification.FileEmbedded(writer, file2Path, File2, null);
                item = new PdfCollectionItem(schema);
                item.AddItem("Type", "pdf");
                fs.AddCollectionItem(item);
                fs.AddDescription(GetDescription(2, File2), false);
                writer.AddFileAttachment(fs);
    
                writer.AddToBody(parentFolderObject, parentFolderObjectReference);
                writer.AddToBody(childFolder1Object, childFolder1ObjectReference);
                writer.AddToBody(childFolder2Object, childFolder2ObjectReference);
    
                document.Close();
            }
        }
    
        private static string GetDescription(int id, string fileName) {
            return string.Format("<{0}>{1}", id, fileName);
        }
    
        private static PdfDictionary GetFolderDictionary(int id) {
            PdfDictionary dic = new PdfDictionary(new PdfName("Folder"));
            dic.Put(PdfName.CREATIONDATE, new PdfDate(DateTime.Now));
            dic.Put(PdfName.MODDATE, new PdfDate(DateTime.Now));
            dic.Put(PdfName.ID, new PdfNumber(id));
            return dic;
        }
    
        private static PdfCollectionSchema CollectionSchema() {
            PdfCollectionSchema schema = new PdfCollectionSchema();
            PdfCollectionField type = new PdfCollectionField("File type", PdfCollectionField.TEXT);
            type.Order = 0;
            schema.AddField("Type", type);
            PdfCollectionField filename = new PdfCollectionField("File", PdfCollectionField.FILENAME);
            filename.Order = 1;
            schema.AddField("File", filename);
            return schema;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I see many questions and answers about using C# to generate PDF files. I
I am using Yahoo Answers API to build a c# desktop application which goes
Update: Based on the answers I initially went the route of using IsInstanceOf() which
I am using an asp radio button group to list out answers in a
I've found a few answers for this using mySQL alone, but I was hoping
I am using Doctrine2 migrations. I need some answers about my doubt, I canno
After checking 20+ answers I'm sure there is no way to trace (using JS
I'm using Perforce, if that changes the tune of the answers at all. I'd
Although I read dozens of answers I could not find a solution. I'm using
This is the 1st time I am using stackoverflow, I usually find answers to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.