How to extract attachments from PDF files in ASP.NET, C#, VB.NET, and VBScript (VB6) using PDF Extractor SDK - ByteScout
Announcement
Our ByteScout SDK products are sunsetting as we focus on expanding new solutions.
Learn More Open modal
Close modal
Announcement Important Update
ByteScout SDK Sunsetting Notice
Our ByteScout SDK products are sunsetting as we focus on our new & improved solutions. Thank you for being part of our journey, and we look forward to supporting you in this next chapter!

How to extract attachments from PDF files in ASP.NET, C#, VB.NET, and VBScript (VB6) using PDF Extractor SDK

  • Home
  • /
  • Articles
  • /
  • How to extract attachments from PDF files in ASP.NET, C#, VB.NET, and VBScript (VB6) using PDF Extractor SDK

With PDF Extractor SDK, you can easily extract attachments from PDF files. Check the samples below to learn how to extract attachments from PDF files.

Also, check this article to learn how to extract pdf document info such as Author, Creator, Producer etc.

Select your programming language:

How to extract attachments from PDF files in ASP.NET

using System;
using System.IO;
using Bytescout.PDFExtractor;

namespace ExtractAttachments
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String inputFile = Server.MapPath(@"bin\attachments.pdf");

            // Create Bytescout.PDFExtractor.AttachmentExtractor instance
            AttachmentExtractor extractor = new AttachmentExtractor();
            extractor.RegistrationName = "demo";
            extractor.RegistrationKey = "demo";

            // Load sample PDF document
            extractor.LoadDocumentFromFile(inputFile);

            Response.Clear();
            Response.ContentType = "text/html";

            for (int i = 0; i < extractor.Count; i++)
            {
                Response.Write("Saving attachment: " + extractor.GetFileName(i) + "<br>");
                //extractor.Save(i, extractor.GetFileName(i)); // you can save into temp folder or save to Stream object to avoid temp files
                Response.Write("File size: " + extractor.GetSize(i) + "<br>");
            }

            Response.End();

            extractor.Dispose();
        }
    }
}

How to extract attachments from PDF files in C#

using System;
using Bytescout.PDFExtractor;

namespace ExtractAttachments
{
    class Program
    {
        static void Main()
        {
            // Create Bytescout.PDFExtractor.AttachmentExtractor instance
            AttachmentExtractor extractor = new AttachmentExtractor();
            extractor.RegistrationName = "demo";
            extractor.RegistrationKey = "demo";

            // Load sample PDF document
            extractor.LoadDocumentFromFile("attachments.pdf");

            for (int i = 0; i < extractor.Count; i++)
            {
                Console.WriteLine("Saving attachment: " + extractor.GetFileName(i));
                // Save attachment to file
                extractor.Save(i, extractor.GetFileName(i));
                Console.WriteLine("File size: " + extractor.GetSize(i));
            }

            extractor.Dispose();
        }
    }
}

How to extract attachments from PDF files in Visual Basic .NET

Imports Bytescout.PDFExtractor

Module Module1

    Sub Main()

        ' Create Bytescout.PDFExtractor.AttachmentExtractor instance
        Dim extractor = New AttachmentExtractor()
        extractor.RegistrationName = "demo"
        extractor.RegistrationKey = "demo"

        ' Load sample PDF document
        extractor.LoadDocumentFromFile("attachments.pdf")

        For i As Integer = 0 To extractor.Count - 1
            Console.WriteLine("Saving attachment: " + extractor.GetFileName(i))
            ' Save attachment to file
            extractor.Save(i, extractor.GetFileName(i))
            Console.WriteLine("File size: " + extractor.GetSize(i).ToString())
        Next

        extractor.Dispose()

    End Sub

End Module

How to extract attachments from PDF files in VBScript (Visual Basic 6)

' Create Bytescout.PDFExtractor.extractor object
Set extractor = CreateObject("Bytescout.PDFExtractor.AttachmentExtractor")
extractor.RegistrationName = "demo"
extractor.RegistrationKey = "demo"

' Load sample PDF document with embedded attachments
extractor.LoadDocumentFromFile("..\..\attachments.pdf")

' Iterate through attachments and save them
For i = 0 To extractor.Count - 1
 extractor.Save i, extractor.GetFileName(i) ' save in the current folder with original filename
Next

MsgBox "Done: " & CStr(extractor.Count) & " attachments extracted"

Set extractor = Nothing

Tutorials:

prev
next