property Attachment[Index: Integer]: TBytes;
PDF documents can carry arbitrary auxiliary files (source spreadsheets, original Word documents, attached invoices, signed XML payloads, ZIP bundles, even other PDFs) inside a document-level container called the EmbeddedFiles name tree. Each entry is a PDF file specification dictionary whose stream holds the actual bytes of the attached file. PDFiumVCL exposes that tree as a flat 0-based index from 0 to AttachmentCount - 1.
Reading Attachment[Index] returns the decoded byte content of the attached file as a TBytes array — exactly the bytes any other PDF reader would extract when the user clicks the paperclip icon and saves the attachment. The returned bytes are already decompressed; you can write them straight to disk, push them to a stream, or feed them back into another TPdf instance if the attachment is itself a PDF.
Writing replaces the file stream of an existing attachment slot in place. The slot must already exist; use CreateAttachment first if you need to add a new entry. Writes are buffered in memory and only become persistent after the document is saved with SaveAs.
| Index | Integer. Zero-based position in the EmbeddedFiles name tree. Must satisfy 0 <= Index < AttachmentCount. Indexes match those reported by AttachmentName and AttachmentType, so iterating 0..AttachmentCount - 1 visits each attachment exactly once. |
TBytes array in one allocation — measure memory pressure before reading dozens of multi-megabyte payloads in a tight loop.Attachment[Index] never resizes the EmbeddedFiles tree; it only replaces stream bytes. To remove an entry use DeleteAttachment.
var
I: Integer;
Data: TBytes;
OutPath: string;
begin
Pdf1.LoadFromFile('with-attachments.pdf');
for I := 0 to Pdf1.AttachmentCount - 1 do
begin
Data := Pdf1.Attachment[I];
OutPath := 'extracted\' + Pdf1.AttachmentName[I];
TFile.WriteAllBytes(OutPath, Data);
Memo1.Lines.Add(Format('%s (%s, %d bytes)',
[Pdf1.AttachmentName[I], Pdf1.AttachmentType[I], Length(Data)]));
end;
end;