Skip to content

Instantly share code, notes, and snippets.

@gyurisc
Last active September 20, 2024 20:33
Show Gist options
  • Select an option

  • Save gyurisc/1026807 to your computer and use it in GitHub Desktop.

Select an option

Save gyurisc/1026807 to your computer and use it in GitHub Desktop.

Revisions

  1. gyurisc revised this gist Jul 26, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion PDFExtractor.cs
    Original file line number Diff line number Diff line change
    @@ -35,7 +35,7 @@ internal void ExtractAttachments(string file_name, string folderName)

    for (int i = 0; i < filespecs.Size; i++)
    {
    i++;
    // i++; commenting this out as it is a mistake to change the loop variable
    fileArray = filespecs.GetAsDict(i);
    file = fileArray.GetAsDict(PdfName.EF);

  2. @invalid-email-address Anonymous created this gist Jun 15, 2011.
    56 changes: 56 additions & 0 deletions PDFExtractor.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using iTextSharp.text.pdf;

    namespace PDFExtract
    {
    public class PDFExtractor
    {
    public void ExtractAttachments()
    {
    }

    // Origin of the code: http://stackoverflow.com/questions/3007780/itextsharp-how-to-open-read-extract-a-file-attachment
    internal void ExtractAttachments(string file_name, string folderName)
    {
    PdfDictionary documentNames = null;
    PdfDictionary embeddedFiles = null;
    PdfDictionary fileArray = null;
    PdfDictionary file = null;
    PRStream stream = null;

    PdfReader reader = new PdfReader(file_name);
    PdfDictionary catalog = reader.Catalog;

    documentNames = (PdfDictionary)PdfReader.GetPdfObject(catalog.Get(PdfName.NAMES));

    if (documentNames != null)
    {
    embeddedFiles = (PdfDictionary)PdfReader.GetPdfObject(documentNames.Get(PdfName.EMBEDDEDFILES));
    if (embeddedFiles != null)
    {
    PdfArray filespecs = embeddedFiles.GetAsArray(PdfName.NAMES);

    for (int i = 0; i < filespecs.Size; i++)
    {
    i++;
    fileArray = filespecs.GetAsDict(i);
    file = fileArray.GetAsDict(PdfName.EF);

    foreach (PdfName key in file.Keys)
    {
    stream = (PRStream)PdfReader.GetPdfObject(file.GetAsIndirectObject(key));
    string attachedFileName = fileArray.GetAsString(key).ToString();
    byte[] attachedFileBytes = PdfReader.GetStreamBytes(stream);

    System.IO.File.WriteAllBytes(attachedFileName, attachedFileBytes);
    }

    }
    }
    }
    }
    }
    }