How to extract the attachment from a PDF document using C# and VB.NET
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:
- Create a new C# console application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
- 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
- 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.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.