Working with Attachments - Facades

In this section, we will explain how to work with attachments in PDF using Aspose.PDF for .NET Facades. An attachment is an additional file that is attached to a parent document, it can be a variety of file types, such as pdf, word, image, or other files. You will learn how to add attachments to pdf, get the information of an attachment, and save it to file, delete the attachment from PDF programmatically with C#.

Add Attachment from a File in an Existing PDF

You can add an attachment in an existing PDF file using PdfContentEditor class. The attachment can be added from a file on the disk using the file path. You can add attachment using AddDocumentAttachment method. This method takes two arguments: file path and attachment description. First, you need to open the existing PDF file and add the attachement into it. Then you can save the output PDF file using Save method of PdfContentEditor.

The following code snippet shows you how to add attachment from a file. For example, let’s add the MP3 file.

public static void AttachmentDemo01()
    {
        PdfContentEditor editor = new PdfContentEditor(new Document(_dataDir + "sample.pdf"));
        editor.AddDocumentAttachment(@"C:\Samples\file_example_MP3_700KB.mp3","Demo MP3 file");
        editor.Save(_dataDir + "PdfContentEditorDemo07.pdf");
    }

Add Attachment from a Stream in an Existing PDF

Attachment can be added in a PDF file from a stream – FileStream – using AddDocumentAttachment method. This method takes three arguments: stream, attachment name, and attachment description. In order to add attachment, you need to create an object of PdfContentEditor class and bind the input PDF file using BindPdf method. After that, you can call AddDocumentAttachment method to add the attachment. Finally, you can call Save method to save the updated PDF file. The following code snippet shows you how to add attachment from a Stream.

public static void AttachmentDemo02()
    {
        PdfContentEditor editor = new PdfContentEditor(new Document(_dataDir + "sample.pdf"));
        var fileStream = System.IO.File.OpenRead(@"C:\Samples\file_example_MP3_700KB.mp3");
        editor.AddDocumentAttachment(fileStream, "file_example_MP3_700KB.mp3", "Demo MP3 file");
        editor.Save(_dataDir + "PdfContentEditorDemo08.pdf");
    }

Delete All Attachments from an Existing PDF File

DeleteAttachments method of PdfContentEditor class allows you to delete all the attachments from an existing PDF file. Call the DeleteAttachments method. Finally, you have to call Save method to save the updated PDF file. The following code snippet shows you how to delete all attachments from an existing PDF file.

    public static void DeleteAllAttachments()
    {
        AttachmentDemo02();
        PdfContentEditor editor = new PdfContentEditor(new Document(_dataDir + "PdfContentEditorDemo07.pdf"));
        editor.DeleteAttachments();
        editor.Save(_dataDir + "PdfContentEditorDemo09.pdf");
    }