如何添加和移除 PDF 附件
PDF 文件中的附件是指嵌入在 PDF 文件本身內的文件或額外數據。 這與PDF的常規內容不同,常規內容在您查看PDF時包括可見的文字、圖像和格式設定。 這些附件可以是各種檔案類型,包括圖片、文件、試算表或其他格式。 通常,附件用於提供額外的參考資料或補充數據,用戶在打開PDF時可以訪問這些資料。
在IronPDF中處理附件時,過程簡單且對用戶友好。
如何添加和移除 PDF 附件
新增附件示例
要將文件作為附件添加,首先在您的程序中以字节[]. 使用 File.ReadAllBytes
方法是最簡單的方式。 With the file loaded in as byte[], 您可以使用 AddAttachment
方法將物件作為附件添加到 PDF 中,如下所示:
:path=/static-assets/pdf/content-code-examples/how-to/add-remove-attachments-add-attachment.cs
using IronPdf;
using System.IO;
// Import attachment file
byte[] fileData = File.ReadAllBytes(@"path/to/file");
// Open existing PDF
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");
// Add attachment to the PDF
pdf.Attachments.AddAttachment("Example", fileData);
pdf.SaveAs("addAttachment.pdf");
Imports IronPdf
Imports System.IO
' Import attachment file
Private fileData() As Byte = File.ReadAllBytes("path/to/file")
' Open existing PDF
Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")
' Add attachment to the PDF
pdf.Attachments.AddAttachment("Example", fileData)
pdf.SaveAs("addAttachment.pdf")
AddAttachment
函數輸出一個 PdfAttachment 物件,我們可以保留它以供將來參考,或在需要時將其刪除。
儲存PDF後,您可以從PDF查看器的工具欄打開附件。 我們在下面的圖片中展示如何在Google Chrome的PDF查看器中找到此功能:
從那裡,您可以點擊它並將附件保存到您自己的存儲空間。
檢索附件範例
透過存取 PdfDocument 物件的 Attachments 屬性,可以將 PDF 中的附件作為二進位資料檢索。 使用二進制數據,您可以將 PDF 中的附件匯出為各自的檔案格式。
:path=/static-assets/pdf/content-code-examples/how-to/add-remove-attachments-retrieve-attachment.cs
using IronPdf;
using System.IO;
// Open existing PDF
PdfDocument pdf = PdfDocument.FromFile("addAttachment.pdf");
// Iterate through all attachments
foreach (var attachment in pdf.Attachments)
{
if (attachment.Name.Contains("Example"))
{
// Save byte to file
File.WriteAllBytes($"{attachment.Name}.doc", attachment.Data);
}
}
Imports IronPdf
Imports System.IO
' Open existing PDF
Private pdf As PdfDocument = PdfDocument.FromFile("addAttachment.pdf")
' Iterate through all attachments
For Each attachment In pdf.Attachments
If attachment.Name.Contains("Example") Then
' Save byte to file
File.WriteAllBytes($"{attachment.Name}.doc", attachment.Data)
End If
Next attachment
刪除附件示例
要移除附件,只需使用 RemoveAttachment
函式。 此方法需要引用附件,可以從附件屬性中檢索。 我們展示如何使用上面保存的文件來做到這一點。
:path=/static-assets/pdf/content-code-examples/how-to/add-remove-attachments-remove-attachment.cs
using IronPdf;
using System.Linq;
// Open existing PDF
PdfDocument pdf = PdfDocument.FromFile("addAttachment.pdf");
// Add attachment to the PDF
PdfAttachmentCollection retrieveAttachments = pdf.Attachments;
// Remove attachment from PDF
pdf.Attachments.RemoveAttachment(retrieveAttachments.First());
pdf.SaveAs("removeAttachment.pdf");
Imports IronPdf
Imports System.Linq
' Open existing PDF
Private pdf As PdfDocument = PdfDocument.FromFile("addAttachment.pdf")
' Add attachment to the PDF
Private retrieveAttachments As PdfAttachmentCollection = pdf.Attachments
' Remove attachment from PDF
pdf.Attachments.RemoveAttachment(retrieveAttachments.First())
pdf.SaveAs("removeAttachment.pdf")
刪除附件並在 PDF 檢視器中開啟生成的檔案後,可以看到附件不再出現: