Add, Extract and Delete Attachments in PDF in C#

We can attach a variety types of files such as PDF, Word, Excel, TXT and Images to a PDF file. In this blog, I will show you how to add, extract and delete attachments in a PDF file in C# by using Free Spire.PDF for .NET library.

Before using the below code, you need to install Free Spire.PDF for .NET via NuGet or download Free Spire.PDF for .NET from this link , install it and then reference Spire.Pdf.dll in the BIN folder into your project.

Add attachments

The library provides a PdfAttachmentCollection.Add(PdfAttachment attachment) method which is used to add attachments to a PDF file. The following code snippet shows how to attach txt, image, PDF and Word files to a PDF file.

using Spire.Pdf;
using Spire.Pdf.Attachments;

namespace AddAttachments
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();

            //Attach txt file to PDF
            PdfAttachment attachment = new PdfAttachment("Input.txt");
            attachment.Description = "Input.txt";
            attachment.MimeType = "application/txt";
            pdf.Attachments.Add(attachment);

            //Attach image file to PDF
            attachment = new PdfAttachment("panda.jpg");
            attachment.Description = "panda.jpg";
            attachment.MimeType = "image/jpg";
            pdf.Attachments.Add(attachment);

            //Attach PDF file to PDF
            attachment = new PdfAttachment("Example.pdf");
            attachment.Description = "Example.pdf";
            attachment.MimeType = "application/pdf";
            pdf.Attachments.Add(attachment);

            //Attach Word file to PDF
            attachment = new PdfAttachment("Template.docx");
            attachment.Description = "Template.docx";
            attachment.MimeType = "application/msword";
            pdf.Attachments.Add(attachment);

            //Save the result file
            pdf.SaveToFile("AddAttachments.pdf"); 
        }
    }
}

1.png

Extract attachments

To extract attachments, you first need to get the attachment collection of a PDF file by using PdfDocument.Attachments attribute, second, loop through the collection, extract and save each attachment in the collection to disk.

using Spire.Pdf;
using Spire.Pdf.Attachments;
using System.IO;

namespace ExtractAttachments
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load the PDF file  
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile("AddAttachments.pdf");

            //Get the collection of attachments in the file  
            PdfAttachmentCollection collection = pdf.Attachments;

            //Save all the attachments to files  
            for (int i = 0; i < collection.Count; i++)
            {
                File.WriteAllBytes(@"output/" + collection[i].FileName, collection[i].Data);
            }  
        }
    }
}

2.png

Delete attachments

You can easily delete all of the attachments from a PDF file by using the PdfAttachmentCollection.Clear() method.

using Spire.Pdf;
using Spire.Pdf.Attachments;

namespace DeleteAttachments
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load the PDF file
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile("AddAttachments.pdf");

            // Get the collection of attachments in the file
            PdfAttachmentCollection attachments = pdf.Attachments;
            //Delete all the attachments from the collection
            attachments.Clear();

            //Save the result file
            pdf.SaveToFile("DeleteAttachments.pdf");

        }
    }
}

3.png

No Comments Yet