How to Add and Remove Attachments in PDF C#

How to Add and Remove PDF Attachments in C# with IronPDF

IronPDF enables you to programmatically add, retrieve, and remove file attachments in PDF documents using simple C# methods like AddAttachment() and RemoveAttachment(), allowing you to embed supplementary files directly into your PDFs.

Attachments in a PDF document refer to files or additional data embedded within the PDF file itself. This is distinct from the regular content of the PDF, which includes visible text, images, and formatting when you view the PDF. These attachments can take the form of various file types, including images, documents, spreadsheets, or other formats. Typically, attachments are used to provide additional reference materials or supplementary data that users can access when they open the PDF. This capability is particularly useful when creating comprehensive PDF reports or when you need to merge multiple PDFs with supporting documentation.

Quickstart: Adding Attachments to PDF

Add attachments to your PDF documents using IronPDF. This quick example demonstrates how to embed a file as an attachment into a PDF. Load your existing PDF, use the AddAttachment method, and save the updated document. This process ensures your supplementary materials are included with your PDF, making them accessible directly from any PDF viewer.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

    var pdf = IronPdf.PdfDocument.FromFile("example.pdf");
    pdf.Attachments.AddAttachment("file.txt", System.IO.File.ReadAllBytes("file.txt"));
    pdf.SaveAs("updated.pdf");
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer


How Do I Add a File as an Attachment to a PDF?

To add a file as an attachment, first load it as a byte[]. The easiest way to do this is to use the File.ReadAllBytes method. With the file loaded as a byte[], use the AddAttachment method to add the object into a PDF as an attachment:

:path=/static-assets/pdf/content-code-examples/how-to/add-remove-attachments-add-attachment.cs
using IronPdf;
using System.IO;

// Import attachment file
byte[] fileData = File.ReadAllBytes(@"path/to/file");

// Open existing PDF
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");

// Add attachment to the PDF
pdf.Attachments.AddAttachment("Example", fileData);

pdf.SaveAs("addAttachment.pdf");
$vbLabelText   $csharpLabel

The AddAttachment function outputs a PdfAttachment object that you can keep for future reference or remove later if needed. This approach is similar to how you would add images to PDFs or manage other PDF assets.

After saving the PDF, you can open the attachment from the toolbar of a PDF viewer. The image below demonstrates where to find this feature in Google Chrome's PDF Viewer:

PDF viewer showing Hello World document with navigation controls and sidebar panel

From there, you can click on it and save the attachment to your storage.

What File Types Can I Attach to a PDF?

IronPDF supports attaching virtually any file type to PDF documents. Common attachment types include:

  • Office documents (DOCX, XLSX, PPTX)
  • Images (JPG, PNG, GIF, SVG)
  • Text files (TXT, CSV, XML)
  • Archives (ZIP, RAR)
  • Other PDFs

The attachment system works with binary data, so any file that can be read as bytes can be attached. When working with specific document types, you might also consider IronPDF's built-in conversion features, such as converting DOCX to PDF or converting images to PDF.

Where Do Attachments Appear in PDF Viewers?

Different PDF viewers display attachments in various locations:

  • Adobe Acrobat: Shows a paperclip icon in the navigation pane
  • Chrome PDF Viewer: Displays attachments in the left sidebar when clicked
  • Firefox PDF Viewer: Shows attachments in a dedicated panel
  • Microsoft Edge: Similar to Chrome, with a sidebar attachment view

Most modern PDF viewers support attachments, though the interface may vary slightly between applications.

What Happens to the PdfAttachment Object After Adding?

When you call AddAttachment(), IronPDF creates a PdfAttachment object that contains:

  • Name: The display name of the attachment
  • Data: The binary content of the attached file
  • Description: Optional metadata about the attachment

This object is added to the PDF's internal attachment collection and remains accessible through the Attachments property until explicitly removed.

How Can I Retrieve Attachments from an Existing PDF?

The attachments in a PDF can be retrieved as binary data by accessing the Attachments property of the PdfDocument object. With the binary data, you can export the attachments from the PDF as their respective file formats.

:path=/static-assets/pdf/content-code-examples/how-to/add-remove-attachments-retrieve-attachment.cs
using IronPdf;
using System.IO;

// Open existing PDF
PdfDocument pdf = PdfDocument.FromFile("addAttachment.pdf");

// Iterate through all attachments
foreach (var attachment in pdf.Attachments)
{
    if (attachment.Name.Contains("Example"))
    {
        // Save byte to file
        File.WriteAllBytes($"{attachment.Name}.doc", attachment.Data);
    }
}
$vbLabelText   $csharpLabel

This process is particularly useful when you need to extract content from PDFs or process attached documents programmatically.

How Do I Access Multiple Attachments in a PDF?

The Attachments property returns a collection that you can iterate through or query using LINQ:

// Get all attachments as a list
var allAttachments = pdf.Attachments.ToList();

// Filter attachments by size (e.g., files larger than 1MB)
var largeAttachments = pdf.Attachments
    .Where(a => a.Data.Length > 1024 * 1024)
    .ToList();

// Find specific attachment by exact name
var specificAttachment = pdf.Attachments
    .FirstOrDefault(a => a.Name == "report.xlsx");
// Get all attachments as a list
var allAttachments = pdf.Attachments.ToList();

// Filter attachments by size (e.g., files larger than 1MB)
var largeAttachments = pdf.Attachments
    .Where(a => a.Data.Length > 1024 * 1024)
    .ToList();

// Find specific attachment by exact name
var specificAttachment = pdf.Attachments
    .FirstOrDefault(a => a.Name == "report.xlsx");
$vbLabelText   $csharpLabel

What Properties Are Available on Retrieved Attachments?

Each PdfAttachment object provides:

  • Name: The display name of the attachment
  • Data: Binary content as byte array
  • Description: Optional description metadata (if set)

You can use these properties to identify, filter, and process attachments based on your requirements.

How Can I Filter Attachments by Name or Type?

Since attachments are stored with their display names, you can filter them using string operations:

// Filter by file extension (assuming names include extensions)
var imageAttachments = pdf.Attachments
    .Where(a => a.Name.EndsWith(".jpg") || 
                a.Name.EndsWith(".png") || 
                a.Name.EndsWith(".gif"))
    .ToList();

// Filter by name pattern
var reportsOnly = pdf.Attachments
    .Where(a => a.Name.StartsWith("Report_"))
    .ToList();
// Filter by file extension (assuming names include extensions)
var imageAttachments = pdf.Attachments
    .Where(a => a.Name.EndsWith(".jpg") || 
                a.Name.EndsWith(".png") || 
                a.Name.EndsWith(".gif"))
    .ToList();

// Filter by name pattern
var reportsOnly = pdf.Attachments
    .Where(a => a.Name.StartsWith("Report_"))
    .ToList();
$vbLabelText   $csharpLabel

How Do I Remove Attachments from a PDF?

To remove an attachment, use the RemoveAttachment function. This method requires a reference to the attachment, which can be retrieved from the Attachments property. Here's how to do this using the saved file from above:

:path=/static-assets/pdf/content-code-examples/how-to/add-remove-attachments-remove-attachment.cs
using IronPdf;
using System.Linq;

// Open existing PDF
PdfDocument pdf = PdfDocument.FromFile("addAttachment.pdf");

// Add attachment to the PDF
PdfAttachmentCollection retrieveAttachments = pdf.Attachments;

// Remove attachment from PDF
pdf.Attachments.RemoveAttachment(retrieveAttachments.First());

pdf.SaveAs("removeAttachment.pdf");
$vbLabelText   $csharpLabel

After removing the attachment and opening the resulting file in a PDF viewer, you will see that the attachment no longer appears:

PDF viewer with attachment panel open, showing Hello World document and Show Attachments button

What Happens When I Remove an Attachment?

When you remove an attachment:

  1. The attachment data is completely removed from the PDF file
  2. The file size decreases by approximately the size of the removed attachment
  3. Any references to that attachment in the PDF structure are cleaned up
  4. The change is permanent once you save the PDF

This is similar to other PDF modification operations like removing pages or redacting content.

Can I Remove Multiple Attachments at Once?

Yes, you can remove multiple attachments in a single operation. Here's an example:

// Remove all attachments that match a pattern
var attachmentsToRemove = pdf.Attachments
    .Where(a => a.Name.StartsWith("temp_"))
    .ToList();

foreach (var attachment in attachmentsToRemove)
{
    pdf.Attachments.RemoveAttachment(attachment);
}

// Or remove all attachments at once
while (pdf.Attachments.Count > 0)
{
    pdf.Attachments.RemoveAttachment(pdf.Attachments.First());
}
// Remove all attachments that match a pattern
var attachmentsToRemove = pdf.Attachments
    .Where(a => a.Name.StartsWith("temp_"))
    .ToList();

foreach (var attachment in attachmentsToRemove)
{
    pdf.Attachments.RemoveAttachment(attachment);
}

// Or remove all attachments at once
while (pdf.Attachments.Count > 0)
{
    pdf.Attachments.RemoveAttachment(pdf.Attachments.First());
}
$vbLabelText   $csharpLabel

How Do I Verify an Attachment Was Successfully Removed?

You can verify attachment removal in several ways:

// Check the attachment count
int attachmentCountBefore = pdf.Attachments.Count;
pdf.Attachments.RemoveAttachment(targetAttachment);
int attachmentCountAfter = pdf.Attachments.Count;

// Verify the count decreased
if (attachmentCountAfter < attachmentCountBefore)
{
    Console.WriteLine("Attachment successfully removed");
}

// Check if specific attachment exists
bool attachmentExists = pdf.Attachments
    .Any(a => a.Name == "specificFile.txt");
// Check the attachment count
int attachmentCountBefore = pdf.Attachments.Count;
pdf.Attachments.RemoveAttachment(targetAttachment);
int attachmentCountAfter = pdf.Attachments.Count;

// Verify the count decreased
if (attachmentCountAfter < attachmentCountBefore)
{
    Console.WriteLine("Attachment successfully removed");
}

// Check if specific attachment exists
bool attachmentExists = pdf.Attachments
    .Any(a => a.Name == "specificFile.txt");
$vbLabelText   $csharpLabel

Best Practices for PDF Attachments

When working with PDF attachments in IronPDF, consider these best practices:

  1. File Size Management: Be mindful of attachment sizes, as they directly increase PDF file size
  2. Naming Conventions: Use clear, descriptive names for attachments to help users identify them
  3. Security Considerations: When handling sensitive attachments, consider applying PDF passwords and permissions
  4. Performance: For large attachments or many files, consider using async operations to maintain application responsiveness

Ready to see what else you can do? Check out our tutorial page here: Organize PDFs

Frequently Asked Questions

How do I add file attachments to a PDF programmatically in C#?

IronPDF provides a simple AddAttachment() method to embed files into PDFs. First, load your file as a byte array using File.ReadAllBytes(), then use the AddAttachment method on your PdfDocument object. The method takes the filename and byte array as parameters.

What types of files can be attached to PDFs?

IronPDF allows you to attach various file types to PDFs, including images, documents, spreadsheets, and other formats. These attachments are embedded directly into the PDF file and can be accessed through any standard PDF viewer's attachment panel.

How do I load an existing PDF to add attachments?

You can load an existing PDF using IronPDF's PdfDocument.FromFile() method. Once loaded, you can use the Attachments property to manage attachments, including adding new ones with AddAttachment() or removing existing ones.

What is the difference between PDF content and PDF attachments?

PDF content includes visible text, images, and formatting that you see when viewing the PDF. Attachments are separate files embedded within the PDF that don't appear in the main document view. With IronPDF, attachments are accessed through the PDF viewer's attachment panel and serve as supplementary materials.

How can I remove attachments from a PDF?

IronPDF provides a RemoveAttachment() method to programmatically remove embedded files from PDFs. The AddAttachment function returns a PdfAttachment object that you can reference later for removal operations.

Where can users find attachments in a PDF viewer?

After using IronPDF to add attachments, users can access them through the PDF viewer's toolbar. Most PDF viewers, including Google Chrome's built-in viewer, display an attachment icon or panel where embedded files can be opened or saved.

Jordi Bardia
Software Engineer
Jordi is most proficient in Python, C# and C++, when he isn’t leveraging his skills at Iron Software; he’s game programming. Sharing responsibilities for product testing, product development and research, Jordi adds immense value to continual product improvement. The varied experience keeps him challenged and engaged, and he ...
Read More
Ready to Get Started?
Nuget Downloads 16,901,161 | Version: 2025.12 just released