Search Results for

    Show / Hide Table of Contents

    Class PdfAttachmentCollection

    Manages the collection of file attachments embedded in a PDF document. Supports adding, removing, and iterating through attachments.

    Access this collection via Attachments property.

    Example - Manage attachments:

    var pdf = PdfDocument.FromFile("document.pdf");
    

    // Add attachment: byte[] excelData = File.ReadAllBytes("report.xlsx"); pdf.Attachments.AddAttachment("Q4_Report.xlsx", excelData);

    // List all attachments: foreach (var att in pdf.Attachments) Console.WriteLine($"{att.Index}: {att.Name}");

    // Remove first attachment: pdf.Attachments.RemoveAttachment(pdf.Attachments[0]);

    pdf.SaveAs("modified.pdf");

    Inheritance
    System.Object
    PdfClientAccessor
    PdfAttachmentCollection
    Implements
    System.Collections.Generic.IEnumerable<PdfAttachment>
    System.Collections.IEnumerable
    Namespace: IronPdf
    Assembly: IronPdf.dll
    Syntax
    public class PdfAttachmentCollection : PdfClientAccessor, IEnumerable<PdfAttachment>, IEnumerable
    Remarks

    Attachments are automatically sorted alphabetically (case-sensitive) by the PDF library. This means attachment indexes may change after adding new files.

    Related Resources:

    Properties

    Item[Int32]

    Gets the attachment at the specified index within this collection.

    Example:

    var firstAttachment = pdf.Attachments[0];
    File.WriteAllBytes(firstAttachment.Name, firstAttachment.Data);

    Declaration
    public PdfAttachment this[int index] { get; }
    Parameters
    Type Name Description
    System.Int32 index

    Zero-based index of the attachment to retrieve.

    Property Value
    Type Description
    PdfAttachment

    The PdfAttachment at the specified index.

    Exceptions
    Type Condition
    System.ArgumentOutOfRangeException

    Index is outside the valid range.

    Methods

    AddAttachment(String, Byte[])

    Embeds a file as an attachment within the PDF document. The attachment appears as a paperclip icon in PDF readers and can be extracted by users.

    Example - Attach supporting files:

    // Attach Excel report:
    byte[] excelData = File.ReadAllBytes("financials.xlsx");
    pdf.Attachments.AddAttachment("Q4_Financials.xlsx", excelData);
    

    // Attach multiple files: foreach (var file in Directory.GetFiles("receipts")) { var data = File.ReadAllBytes(file); pdf.Attachments.AddAttachment(Path.GetFileName(file), data); }

    Declaration
    public PdfAttachment AddAttachment(string name, byte[] data)
    Parameters
    Type Name Description
    System.String name

    Filename for the attachment including extension (e.g., "report.xlsx", "receipt.pdf"). This name will appear in PDF readers.

    System.Byte[] data

    Complete binary content of the file to attach. Use File.ReadAllBytes() to read from disk.

    Returns
    Type Description
    PdfAttachment

    The newly created PdfAttachment for further manipulation.

    Remarks

    Attachments increase PDF file size by the size of attached files.

    For PDF/A-3 compliant attachments (e.g., ZUGFeRD invoices), see with EmbedFileConfiguration.

    Standards:

    See Also
    RemoveAttachment(PdfAttachment)
    https://ironpdf.com/how-to/pdf-attachments/

    GetEnumerator()

    Retrieve enumerator

    Declaration
    public IEnumerator<PdfAttachment> GetEnumerator()
    Returns
    Type Description
    System.Collections.Generic.IEnumerator<PdfAttachment>

    Enumerator

    RemoveAttachment(PdfAttachment)

    Removes the specified attachment from the PDF document permanently.

    Example - Remove sensitive attachments:

    // Remove specific attachment by reference:
    var pdf = PdfDocument.FromFile("document.pdf");
    var sensitiveFile = pdf.Attachments.FirstOrDefault(
        a => a.Name.Contains("confidential"));
    if (sensitiveFile != null)
        pdf.Attachments.RemoveAttachment(sensitiveFile);
    

    // Remove all attachments: while (pdf.Attachments.Any()) pdf.Attachments.RemoveAttachment(pdf.Attachments[0]);

    pdf.SaveAs("cleaned.pdf");

    Declaration
    public void RemoveAttachment(PdfAttachment attachment)
    Parameters
    Type Name Description
    PdfAttachment attachment

    The PdfAttachment instance to remove. Obtain from indexer or enumeration.

    Remarks

    After removal, indexes of remaining attachments may change due to automatic alphabetical sorting.

    See Also
    AddAttachment(String, Byte[])

    Implements

    System.Collections.Generic.IEnumerable<>
    System.Collections.IEnumerable

    See Also

    PdfAttachment
    Attachments
    ☀
    ☾
    Downloads
    • Download with Nuget
    • Start for Free
    In This Article
    Back to top
    Install with Nuget
    Want to deploy IronPDF to a live project for FREE?
    What’s included?
    30 days of fully-functional product
    Test and share in a live environment
    No watermarks in production
    Get your free 30-day Trial Key instantly.
    No credit card or account creation required
    Your Trial License Key has been emailed to you.
    Download IronPDF free to apply
    your Trial Licenses Key
    Install with NuGet View Licenses
    Licenses from $499. Have a question? Get in touch.