What I received is slightly different from what I saw on the Internet.
So, just in case, I thought I'd post it here to help someone else. I had to go through many different iterations to figure out - the hard way - what I need to get it to work.
I am combining two PDF files into a third PDF file, in which one of the first two PDF files can have file attachments that need to be transferred to a third PDF file. I fully work in threads with ASP.NET, C # 4.0, ITextSharp 5.1.2.0.
// Extract Files from Submit PDF Dictionary<string, byte[]> files = new Dictionary<string, byte[]>(); PdfDictionary names; PdfDictionary embeddedFiles; PdfArray fileSpecs; int eFLength = 0; names = writeReader.Catalog.GetAsDict(PdfName.NAMES); // may be null, writeReader is the PdfReader for a PDF input stream if (names != null) { embeddedFiles = names.GetAsDict(PdfName.EMBEDDEDFILES); //may be null if (embeddedFiles != null) { fileSpecs = embeddedFiles.GetAsArray(PdfName.NAMES); //may be null if (fileSpecs != null) { eFLength = fileSpecs.Size; for (int i = 0; i < eFLength; i++) { i++; //objects are in pairs and only want odd objects (1,3,5...) PdfDictionary fileSpec = fileSpecs.GetAsDict(i); // may be null if (fileSpec != null) { PdfDictionary refs = fileSpec.GetAsDict(PdfName.EF); foreach (PdfName key in refs.Keys) { PRStream stream = (PRStream)PdfReader.GetPdfObject(refs.GetAsIndirectObject(key)); if (stream != null) { files.Add(fileSpec.GetAsString(key).ToString(), PdfReader.GetStreamBytes(stream)); } } } } } } }
source share