Attachments in .NET PDF Library

13 Aug 202617 minutes to read

Essential® PDF provides support for file attachments in PDF documents. These attachments can include any type of file, providing additional information within the PDF.

To quickly get started with adding, removing, and extracting file attachments in PDF documents using the Syncfusion® PDF Library for .NET, refer to this video tutorial:

Adding an attachment to a new PDF document

You can add a file attachment to a new PDF document using the PdfAttachment class. The following code example illustrates this.

using Syncfusion.Pdf;
using Syncfusion.Pdf.Interactive;

// Create a new PDF document
PdfDocument document = new PdfDocument();
// Create an attachment
FileStream fileStream = new FileStream("Input.txt", FileMode.Open, FileAccess.Read);
PdfAttachment attachment = new PdfAttachment("Input.txt", fileStream);
attachment.ModificationDate = DateTime.Now;
attachment.Description = "Input.txt";
attachment.MimeType = "text/plain";
// Add the attachment to the document
document.Attachments.Add(attachment);

// Save the document
document.Save("Output.pdf");
// Close the document
document.Close(true);
using Syncfusion.Pdf;
using Syncfusion.Pdf.Interactive;

// Create a new PDF document
PdfDocument document = new PdfDocument();
// Create an attachment
PdfAttachment attachment = new PdfAttachment("Input.txt");
attachment.ModificationDate = DateTime.Now;
attachment.Description = "Input.txt";
attachment.MimeType = "text/plain";
// Add the attachment to the document
document.Attachments.Add(attachment);

// Save and close the PDF document
document.Save("Output.pdf");
document.Close(true);
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Interactive

' Create a new PDF document
Dim document As New PdfDocument()
' Create an attachment
Dim attachment As New PdfAttachment("Input.txt")
attachment.ModificationDate = DateTime.Now
attachment.Description = "Input.txt"
attachment.MimeType = "text/plain"
' Add the attachment to the document
document.Attachments.Add(attachment)

' Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)

The PdfAttachment constructor accepts the file name and a Stream that contains the file content. The following table describes the key properties used above:

Property Description
FileName Gets the file name of the attachment as displayed in the PDF.
Data Gets the raw byte content of the attachment.
Description Sets a human-readable description for the attachment.
MimeType Sets the MIME type (for example, text/plain, application/pdf, image/png).
ModificationDate Sets the last modified date of the attachment.

You can download a complete working sample from GitHub.

Essential® PDF also supports adding attachments to an existing PDF document using the PdfAttachment class. The following code example illustrates the same.

using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;

// Load the existing PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
// Create an attachment
Stream fileStream = new FileStream("Input.txt", FileMode.Open, FileAccess.Read);
PdfAttachment attachment = new PdfAttachment("Input.txt", fileStream);
attachment.ModificationDate = DateTime.Now;
attachment.Description = "Input.txt";
attachment.MimeType = "text/plain";
// Create the attachments section if it does not exist
if (loadedDocument.Attachments == null)
    loadedDocument.CreateAttachment();
// Add the attachment to the document
loadedDocument.Attachments.Add(attachment);

// Save the document
loadedDocument.Save("Output.pdf");
// Close the document
loadedDocument.Close(true);
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;

// Load the existing PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
// Create an attachment
PdfAttachment attachment = new PdfAttachment("Input.txt");
attachment.ModificationDate = DateTime.Now;
attachment.Description = "Input.txt";
attachment.MimeType = "text/plain";
// Create the attachments section if it does not exist
if (loadedDocument.Attachments == null)
    loadedDocument.CreateAttachment();
// Add the attachment to the document
loadedDocument.Attachments.Add(attachment);

// Save and close the PDF document
loadedDocument.Save("Output.pdf");
loadedDocument.Close(true);
Imports System
Imports Syncfusion.Pdf.Interactive
Imports Syncfusion.Pdf.Parsing

' Load the existing PDF document
Dim loadedDocument As New PdfLoadedDocument("Input.pdf")

' Create an attachment
Dim attachment As New PdfAttachment("Input.txt")
attachment.ModificationDate = DateTime.Now
attachment.Description = "Input.txt"
attachment.MimeType = "text/plain"
' Create the attachments section if it does not exist
If loadedDocument.Attachments Is Nothing Then
    loadedDocument.CreateAttachment()
End If
' Add the attachment to the document
loadedDocument.Attachments.Add(attachment)

' Save and close the PDF document
loadedDocument.Save("Output.pdf")
loadedDocument.Close(True)

You can download a complete working sample from GitHub.

Removing an attachment from an existing PDF

Essential® PDF allows you to remove attachments from an existing document by using the Remove and RemoveAt methods of the PdfAttachmentCollection class. The following code example explains the same.

using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;

// Load the PDF document
PdfLoadedDocument document = new PdfLoadedDocument("Input.pdf");
// Remove an attachment by reference
PdfAttachment attachment = document.Attachments[0];
document.Attachments.Remove(attachment);
// Remove another attachment by index
document.Attachments.RemoveAt(0);

// Save the document
document.Save("Output.pdf");
// Close the document
document.Close(true);
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;

// Load the PDF document
PdfLoadedDocument document = new PdfLoadedDocument("Input.pdf");
// Remove an attachment by reference
PdfAttachment attachment = document.Attachments[0];
document.Attachments.Remove(attachment);
// Remove another attachment by index
document.Attachments.RemoveAt(0);

// Save and close the document
document.