Class PdfAttachment
Represents a file attachment embedded within a PDF document. Provides access to attachment metadata and binary data for extraction or modification.
PDF attachments are complete files stored within the PDF container. They appear as paperclip icons in PDF readers and can contain any file type (spreadsheets, images, documents, etc.).
Example - Extract attachment to disk:
var pdf = PdfDocument.FromFile("invoice_with_receipt.pdf");
foreach (var attachment in pdf.Attachments)
{
Console.WriteLine($"Found: {attachment.Name} at index {attachment.Index}");
File.WriteAllBytes(attachment.Name, attachment.Data);
}
Namespace: IronPdf
Assembly: IronPdf.dll
Syntax
public class PdfAttachment : PdfClientAccessor
Remarks
Common Use Cases:
Related Resources:
Properties
Data
Gets or sets the raw binary data of the attachment. Read to extract, write to replace content while preserving the filename.
Example - Extract and modify:
// Extract attachment data:
byte[] data = attachment.Data;
File.WriteAllBytes($"extracted_{attachment.Name}", data);
// Replace attachment content:
attachment.Data = File.ReadAllBytes("updated_receipt.pdf");
pdf.SaveAs("invoice_updated.pdf");
Declaration
public byte[] Data { get; set; }
Property Value
| Type | Description |
|---|---|
| System.Byte[] | The complete binary content of the attached file. |
Remarks
Reading this property retrieves data from the PDF. For large attachments, consider caching the result if accessed multiple times.
Index
Gets the zero-based index of this attachment within the PDF document. Index may change if attachments are added or removed (alphabetically sorted).
Declaration
public int Index { get; }
Property Value
| Type | Description |
|---|---|
| System.Int32 | Zero-based position in the attachment collection. |
Name
Gets the filename of this attachment as stored in the PDF document. Typically includes file extension (e.g., "receipt.pdf", "data.xlsx").
Use this name when saving extracted attachments to preserve original filenames.
Declaration
public string Name { get; }
Property Value
| Type | Description |
|---|---|
| System.String | The attachment filename including extension. |