How to read a PDF Portfolio using iTextSharp

匆匆过客 提交于 2019-12-06 06:31:20

The example you referenced adds the embedded files as document-level attachments. So you can extract the files like this:

PdfReader reader = new PdfReader(readerPath);
PdfDictionary root = reader.Catalog;
PdfDictionary documentnames = root.GetAsDict(PdfName.NAMES);
PdfDictionary embeddedfiles = 
    documentnames.GetAsDict(PdfName.EMBEDDEDFILES);
PdfArray filespecs = embeddedfiles.GetAsArray(PdfName.NAMES);
for (int i = 0; i < filespecs.Size; ) {
  filespecs.GetAsString(i++);
  PdfDictionary filespec = filespecs.GetAsDict(i++);
  PdfDictionary refs = filespec.GetAsDict(PdfName.EF);
  foreach (PdfName key in refs.Keys) {
    PRStream stream = (PRStream) PdfReader.GetPdfObject(
      refs.GetAsIndirectObject(key)
    );

    using (FileStream fs = new FileStream(
      filespec.GetAsString(key).ToString(), FileMode.OpenOrCreate
    )){
      byte[] attachment = PdfReader.GetStreamBytes(stream);
      fs.Write(attachment, 0, attachment.Length);
    }
  }
} 

Pass the output file from the Kubrick Collection Example you referenced to the PdfReader constructor (readerPath) if you want to test this.

Hopefully I'll have time to update the C# examples this month from version 5.2.0.0 (the iTextSharp version is about three weeks behind the Java version right now).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!