IRONSOFTWAREHOME

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

Curtis Chau
Curtis Chau
Updated: August 2, 2026

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.

  1. 1Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. 2Copy 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");
    C#
  3. 3Deploy 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:

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");

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
  • Index: The position of the attachment within the collection

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

Attachments

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.

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);
    }
}

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");

What Properties Are Available on Retrieved Attachments?

Each PdfAttachment object provides:

  • Name: The display name of the attachment
  • Data: Binary content as byte array
  • Index: The position of the attachment within the collection

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();

My favorite library of this kind is IronPDF. It allows for fast and efficient manipulation of PDF files. It also has many valuable features, like exporting to PDF/A format and digitally signing PDF documents.

Milan Jovanovic

Microsoft MVP

View case study

IronOCR means we can save $40,000 annually from manual processing, while enhancing productivity and freeing up resources for high-impact tasks. I would highly recommend it.

Brent Matzelle

Chief Technology Officer, OPYN

View case study

The IronSuite play a crucial role in our operations. These are tools that increase efficiencies across the business including creating floor plans and improving inventory management.

David Jones

Lead Software Engineer, Agorus Build

View case study

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:

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");

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());
}

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");

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 can I add a file as an attachment to a PDF using IronPDF?

To add a file as an attachment using IronPDF, first load the file as a byte array using `File.ReadAllBytes()`. Then, open your PDF with `PdfDocument.FromFile()` and use the `AddAttachment()` method to embed the file. Finally, save the document to preserve changes.

What types of files can be attached to a PDF with IronPDF?

IronPDF allows attaching a wide array of file types, including Office documents (DOCX, XLSX, PPTX), images (JPG, PNG, GIF), text files (TXT, CSV), and even other PDFs.

Where do attachments typically appear in PDF viewers?

Attachments are displayed differently depending on the PDF viewer. In Adobe Acrobat, they appear with a paperclip icon, while in Chrome's PDF Viewer, they are listed in the left sidebar.

How can I remove attachments from a PDF using IronPDF?

To remove an attachment, load your PDF using `PdfDocument.FromFile()`, access the attachment via the `Attachments` property, and employ the `RemoveAttachment()` method. Save the updated document afterward.

Can IronPDF handle multiple attachments simultaneously?

Yes, IronPDF can manage multiple attachments simultaneously. You can iterate through the `Attachments` collection for batch processing, like removing multiple attachments at once.

What does the `AddAttachment()` method return in IronPDF?

The `AddAttachment()` method returns a `PdfAttachment` object, which includes properties like Name, Data, and Index to manage the attachment within the PDF.

Is it possible to filter PDF attachments by type or name in IronPDF?

Yes, you can filter attachments using the `Attachments` collection based on file extensions or specific name patterns.

What are the best practices for managing PDF attachments in IronPDF?

Some best practices include managing file sizes, using descriptive naming conventions, securing sensitive content with passwords, and leveraging asynchronous operations for better performance.

How can I verify if an attachment was successfully removed?

You can verify attachment removal by checking the number of attachments before and after removal or confirming the absence of the specific attachment by name in the `Attachments` collection.

What happens to the PDF file size when attachments are removed using IronPDF?

Removing attachments with IronPDF reduces the file size roughly by the size of the attachment, as the attachment data is completely removed from the PDF file.

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...
Read More

Ready to Get Started?

Nuget Downloads 20,756,830Version:2026.8just released

Get your FREE

30-day Trial Key instantly.

bullet_checkedNo credit card or account creation required
bullet_testTest in production
without watermarks
bullet_calendar30 days fully
functional product
bullet_support24/5 technical
support during trial
Get your free 30-day Trial Key instantly.
No credit card or account creation required
C# NuGet Library for PDF
Install with NuGet

Version: 2026.8

PM > Install-Package IronPdf
nuget.org/packages/IronPdf/
  1. In Solution Explorer, right-click References, Manage NuGet Packages
  2. Select Browse and search "IronPdf"
  3. Select the package and install
C# PDF DLL
Download DLL

Version: 2026.8

or download Windows Installer here.

  1. Download and unzip IronPDF to a location such as ~/Libs within your Solution directory
  2. In Visual Studio Solution Explorer, right click References. Select Browse, "IronPdf.dll"

Licenses from $999

Key in blue circle

Get your free 30-day Trial Key instantly.

Your trial license will be sent to your email address

No limitations. 100% unlocked. No credit card.

bullet_checkedNo credit card or account creation requiredNo limitations. 100% unlocked. No credit card.
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
Book your free Live Demo
Booking Badge

Trusted by Millions of Engineers Worldwide

Iron Software's customer logos
Get Your No-Obligation Consult
Complete the form below or email sales@ironsoftware.com
Your details will always be kept confidential.
Trusted by Millions of Engineers Worldwide
Iron Software's customer logos
Get your free 30-day Trial Key instantly.
No credit card or account creation required