Articles in this section
Category / Section

How to extract the attachment from a PDF document using C# and VB.NET?

4 mins read

Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can extract the attachments from PDF using C# and VB.NET.

Steps to extract the attachments from PDF programmatically:

  1. Create a new C# console application project. Create a console application project
  2. Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org. NuGet package reference
  3. Include the following namespaces in the Program.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;
using System.IO;

 

VB.NET

Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Interactive
Imports Syncfusion.Pdf.Parsing
Imports System.IO

 

  1. Use the following code snippet to extract the attachment from PDF document.

C#

//Load the PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Attachment.pdf");
//Get first page from document
PdfLoadedPage page = loadedDocument.Pages[0] as PdfLoadedPage;
//Get the annotation collection from pages
PdfLoadedAnnotationCollection annotations = page.Annotations;
//Iterates the annotations
foreach (PdfLoadedAnnotation annot in annotations)
 {
     //Check for the attachment annotation
     if (annot is PdfLoadedAttachmentAnnotation)
     {
            PdfLoadedAttachmentAnnotation file = annot as PdfLoadedAttachmentAnnotation;
            //Extracts the attachment and saves it to the disk
            FileStream stream = new FileStream(file.FileName, FileMode.Create);
            stream.Write(file.Data, 0, file.Data.Length);
            stream.Dispose();
      }
 }
//Iterates through the attachments
if (loadedDocument.Attachments.Count != 0)
{
    foreach (PdfAttachment attachment in loadedDocument.Attachments)
    {
        //Extracts the attachment and saves it to the disk
        FileStream stream = new FileStream(attachment.FileName, FileMode.Create);
        stream.Write(attachment.Data, 0, attachment.Data.Length);
        stream.Dispose();
    }
}
//Close the document
loadedDocument.Close(true);

 

VB.NET

'Load the PDF document
Dim loadedDocument As New PdfLoadedDocument("Attachment.pdf")
'Get first page from document
Dim page As PdfLoadedPage = TryCast(loadedDocument.Pages(0), PdfLoadedPage)
'Get the annotation collection from pages
Dim annotations As PdfLoadedAnnotationCollection = page.Annotations
'Iterates the annotations
For Each annot As PdfLoadedAnnotation In annotations
    'Check for the attachment annotation
    If TypeOf annot Is PdfLoadedAttachmentAnnotation Then
        Dim file As PdfLoadedAttachmentAnnotation = TryCast(annot, PdfLoadedAttachmentAnnotation)
        'Extracts the attachment and saves it to the disk
        Dim stream As New FileStream(file.FileName, FileMode.Create)
        stream.Write(file.Data, 0, file.Data.Length)
        stream.Dispose()
    End If
Next
'Iterates through the attachments
If loadedDocument.Attachments.Count <> 0 Then
    For Each attachment As PdfAttachment In loadedDocument.Attachments
        'Extracts the attachment and saves it to the disk
        Dim stream As New FileStream(attachment.FileName, FileMode.Create)
        stream.Write(attachment.Data, 0, attachment.Data.Length)
        stream.Dispose()
    Next
End If
'Close the document
loadedDocument.Close(True)

 

A complete working sample can be downloaded from ExtractAttachmentSample.zip.

Take a moment to peruse the documentation, where you can find other options like adding and removing attachment, adding file attachment annotation, and File link annotation with code examples.

 Refer here to explore the rich set of Syncfusion Essential PDF features.



Note:

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.

 

Conclusion:

I hope you enjoyed learning about how to extract the attachment from a PDF document using C# and VB.NET.

 

You can refer to our Flutter PDF feature tour page to learn about its other groundbreaking features and documentation, and how to quickly get started with configuration specifications. You can also explore our Flutter PDF Flutter PDF examples to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to explore our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied