function TryGetAnnotationFileAttachment(AnnotIndex: Integer; out Attachment: TPdfAnnotationAttachment; MaxBytes: Cardinal = 67108864): Boolean;
| AnnotIndex | Integer。目前頁面上註解的零起算索引,範圍為 0 .. AnnotationCount - 1。超出範圍的索引會拋出 EPdfError |
| Attachment | TPdfAnnotationAttachment。成功時:Name、Description、MimeType 與 Data(TBytes)中的解碼內容。呼叫執行前會先重設為預設值 |
| MaxBytes | Cardinal。內容大小的上限。預設值 67108864 允許最多 64 MB;更大的內容會拋出 EPdfError |
| Result | Boolean。當註解帶有檔案附件且內容完整讀取時為 True;當它沒有附件或內容無法解碼時為 False |
檔案附件註解會將內嵌檔案釘選到頁面上的某個位置。此呼叫透過原生介面(FPDFAnnot_GetFileAttachment)開啟註解,並在註解帶有附件時,讀取其描述欄位與完整解碼內容,填入 TPdfAnnotationAttachment 記錄
這是 Try 模式的呼叫:沒有附件的註解會傳回 False 而不是拋出例外,無法解碼的內容同樣傳回 False。只有過大的內容才會轉為例外,因此 MaxBytes 限制可作為防範惡意文件耗盡記憶體的硬性防護
註解附件與 Attachment 公開的文件層級 EmbeddedFiles 樹是彼此獨立的:附件可以位於頁面註解上而不出現在該樹中,反之亦然。若要以單一呼叫將資料附加到既有註解,請使用 AddAnnotationFileAttachment
MaxBytes 設為適合應用程式的值
var
I: Integer;
Attachment: TPdfAnnotationAttachment;
begin
for I := 0 to Pdf1.AnnotationCount - 1 do
if (Pdf1.Annotation[I].Subtype = anFileAttachment) and
Pdf1.TryGetAnnotationFileAttachment(I, Attachment) then
begin
TFile.WriteAllBytes('extracted\' + Attachment.Name, Attachment.Data);
Memo1.Lines.Add(Format('%s (%s, %d bytes)',
[Attachment.Name, Attachment.MimeType, Length(Attachment.Data)]));
end;
end;