Extract Attachments from PDF document via C#

How to Extract Attachments from PDF programmatically with C#

Aspose.PDF for .NET Logo

How to Extract Attachments Using .NET Library

In order to extract Attachments in PDF file, we’ll use Aspose.PDF for .NET API which is a feature-rich, powerful and easy to use document manipulation API for net platform. Open NuGet package manager, search for Aspose.PDF and install. You may also use the following command from the Package Manager Console.

Package Manager Console

PM > Install-Package Aspose.PDF

Extract Attachments from PDF C#

You need Aspose.PDF for .NET to try the code in your environment.

  1. Get embedded files collection.
  2. Get count of the embedded files.
  3. Loop through the collection to get all the attachments.
  4. Check if parameter object contains the parameters.
  5. Get the Attachment and write to file or stream.

System Requirements

Aspose.PDF for C# is supported on all major operating systems. Just make sure that you have the following prerequisites.

  • Microsoft Windows or a compatible OS with .NET Framework, .NET Core, and PHP, VBScript, Delphi, C++ via COM Interop.
  • Development environment like Microsoft Visual Studio.
  • Aspose.PDF for .NET DLL referenced in your project.

Extract Attachment from PDF document

    // Open document
    Document pdfDocument = new Document(dataDir + "GetAlltheAttachments.pdf");

    // Get embedded files collection
    EmbeddedFileCollection embeddedFiles = pdfDocument.EmbeddedFiles;

    // Get count of the embedded files
    Console.WriteLine("Total files : {0}", embeddedFiles.Count);

    int count = 1;

    // Loop through the collection to get all the attachments
    foreach (FileSpecification fileSpecification in embeddedFiles)
        Console.WriteLine("Name: {0}", fileSpecification.Name);
        Console.WriteLine("Description: {0}",
        Console.WriteLine("Mime Type: {0}", fileSpecification.MIMEType);

        // Check if parameter object contains the parameters
        if (fileSpecification.Params != null)
            Console.WriteLine("CheckSum: {0}",
            Console.WriteLine("Creation Date: {0}",
            Console.WriteLine("Modification Date: {0}",
            Console.WriteLine("Size: {0}", fileSpecification.Params.Size);

        // Get the attachment and write to file or stream
        byte[] fileContent = new byte[fileSpecification.Contents.Length];
        fileSpecification.Contents.Read(fileContent, 0,
        FileStream fileStream = new FileStream(dataDir + count + "_out" + ".txt",
        fileStream.Write(fileContent, 0, fileContent.Length);