Here are some examples of concept code that creates a new PDF portfolio with a couple of folders and inserts an existing PDF file into each of these folders. This approach is based on using a text editor to view the pdf file created using the sample code associated with the original message, and then using Acrobat to create a folder and move the embedded file to that folder, save the pdf file, and then view the changes in the text editor . In the code, I update the changes found when comparing two versions of the pdf portfolio, so while it works (at least on my machine), this may not be the best way to complete the task.
If you want to view files before and after, like me, create a portfolio using iTextsharp, then open Acrobat and create a folder and move the embedded file to the folder, and 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 PDF Portfolio. Acrobat reorganizes the file and compresses or in some other way converts many files into binary data that you cannot read in a text editor. I found that deleting binary stream data simplified the structure. Just get rid of everything between each pair of stream / endstream .
One thing that Acrobat does in the pdfs portfolio is its built-in flash file, which provides animation and an attractive theme for the portfolio. I could not figure out how to do this, so the resulting file from this code looks a bit.
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);
source share