property Attachment[Index: Integer]: TBytes;
PDF 文档可以在名为 EmbeddedFiles 的文档级容器中携带任意辅助文件(源电子表格、原始 Word 文档、附带发票、签名 XML 载荷、ZIP 包,甚至其他 PDF)。每个条目都是一个 PDF 文件规范字典,其流保存附加文件的实际字节。PDFiumPas 将该树以 0 基索引展平为从 0 到 AttachmentCount - 1 的序列
读取 Attachment[Index] 会以 TBytes 数组形式返回附件文件的已解码字节内容 —— 这正是用户点击回形针图标并保存附件时,其他 PDF 阅读器会提取出的字节。返回的字节已经解压;你可以直接写入磁盘、推送到流中,或者在附件本身也是 PDF 时回灌到另一个 TPdf 实例
写入会就地替换现有附件槽位中的文件流。槽位必须已经存在;如果需要新增条目,请先使用 CreateAttachment。写入会缓存在内存中,并且只有在使用 SaveAs 保存文档后才会持久化
| Index | Integer。EmbeddedFiles 名称树中的零基位置。必须满足 0 <= Index < AttachmentCount。索引与 AttachmentName 和 AttachmentType 报告的索引一致,因此遍历 0..AttachmentCount - 1 时每个附件恰好访问一次 |
TBytes 数组;如果要在紧循环中读取数十个数 MB 载荷,请先评估内存压力Attachment[Index] 不会改变 EmbeddedFiles 树的大小;它只会替换流字节。若要移除条目,请使用 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;