IronPDF 允許您通過簡單的 C# 方法(如 AddAttachment() 和 RemoveAttachment())以程式化方式新增、檢索和移除 PDF 文件中的文件附件,讓您直接將輔助文件嵌入您的 PDF 中。
PDF 文件中的附件是指嵌入在 PDF 文件本身內的文件或附加資料。 這與 PDF 的常規內容不同,後者包括您查看 PDF 時的可見文字、圖像和格式設置。 這些附件可以是多種文件型別,包括圖像、文件、電子表格或其他格式。 通常,附件用來提供其他參考資料或補充資料,讓使用者在開啟 PDF 時可以存取。 這種能力在建立綜合 PDF 報告或需要合併多個 PDF以支持文件時特別有用。
快速開始:向 PDF 新增附件
using IronPDF 將附件新增到您的 PDF 文件中。 此快速範例演示了如何將文件作為附件嵌入到 PDF 中。 載入現有的 PDF,使用 AddAttachment 方法,然後保存更新的文件。 此過程確保您的輔助材料包含在您的 PDF 中,使其直接在任何 PDF 查看器中即可存取。
1Install IronPDF with NuGet Package Manager
PM > Install-Package IronPdf
Install-Package IronPdf
2複製並運行這段程式碼片段。
var pdf = IronPdf.PdfDocument.FromFile("example.pdf");pdf.Attachments.AddAttachment("file.txt", System.IO.File.ReadAllBytes("file.txt"));pdf.SaveAs("updated.pdf");
var pdf = IronPdf.PdfDocument.FromFile("example.pdf");
pdf.Attachments.AddAttachment("file.txt", System.IO.File.ReadAllBytes("file.txt"));
pdf.SaveAs("updated.pdf");
using IronPdf;using System.IO;// Import attachment filebyte[] fileData = File.ReadAllBytes(@"path/to/file");// Open existing PDFPdfDocument pdf = PdfDocument.FromFile("sample.pdf");// Add attachment to the PDFpdf.Attachments.AddAttachment("Example", fileData);pdf.SaveAs("addAttachment.pdf");
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");
ImportsIronPdfImportsSystem.IO' Import attachment filePrivate fileData() AsByte = File.ReadAllBytes("path/to/file")' Open existing PDFPrivate pdf AsPdfDocument = PdfDocument.FromFile("sample.pdf")' Add attachment to the PDFpdf.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 資產。
保存 PDF 後,您可以從 PDF 查看器的工具欄打開附件。 下面的圖像演示了如何在 Google Chrome 的 PDF 查看器中找到此功能:
此物件被新增到 PDF 的內部附件集合中,並通過 Attachments 屬性保持可存取,直到明確移除。
Attachments
如何從現有 PDF 中檢索附件?
可以通過存取 Attachments 物件的 PdfDocument 屬性,將 PDF 中的附件作為二進位資料檢索。 有了二進位資料,您可以將附件導出為其相應的文件格式。
using IronPdf;using System.IO;// Open existing PDFPdfDocument pdf = PdfDocument.FromFile("addAttachment.pdf");// Iterate through all attachmentsforeach (var attachment in pdf.Attachments){ if (attachment.Name.Contains("Example")) { // Save byte to fileFile.WriteAllBytes($"{attachment.Name}.doc", attachment.Data); }}
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);
}
}
ImportsIronPdfImportsSystem.IO' Open existing PDFPrivate pdf AsPdfDocument = PdfDocument.FromFile("addAttachment.pdf")' Iterate through all attachmentsFor Each attachment In pdf.Attachments If attachment.Name.Contains("Example") Then ' Save byte to fileFile.WriteAllBytes($"{attachment.Name}.doc", attachment.Data) End IfNext attachment
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
// Get all attachments as a listvar allAttachments = pdf.Attachments.ToList();// Filter attachments by size (e.g., files larger than 1MB)var largeAttachments = pdf.Attachments .Where(a => a.Data.Length > 1024 * 1024) .ToList();// Find specific attachment by exact namevar specificAttachment = pdf.Attachments .FirstOrDefault(a => a.Name == "report.xlsx");
// Get all attachments as a list
var allAttachments = pdf.Attachments.ToList();
// Filter attachments by size (e.g., files larger than 1MB)
var largeAttachments = pdf.Attachments
.Where(a => a.Data.Length > 1024 * 1024)
.ToList();
// Find specific attachment by exact name
var specificAttachment = pdf.Attachments
.FirstOrDefault(a => a.Name == "report.xlsx");
' Get all attachments as a listDim allAttachments = pdf.Attachments.ToList()' Filter attachments by size (e.g., files larger than 1MB)Dim largeAttachments = pdf.Attachments _ .Where(Function(a) a.Data.Length > 1024 * 1024) _ .ToList()' Find specific attachment by exact nameDim specificAttachment = pdf.Attachments _ .FirstOrDefault(Function(a) a.Name = "report.xlsx")
' Get all attachments as a list
Dim allAttachments = pdf.Attachments.ToList()
' Filter attachments by size (e.g., files larger than 1MB)
Dim largeAttachments = pdf.Attachments _
.Where(Function(a) a.Data.Length > 1024 * 1024) _
.ToList()
' Find specific attachment by exact name
Dim specificAttachment = pdf.Attachments _
.FirstOrDefault(Function(a) a.Name = "report.xlsx")
檢索到的附件有哪些可用屬性?
每個 PdfAttachment 物件提供:
Name:附件的顯示名稱
Data:以位元組陣列形式的二進位內容
Description:可選的描述元資料(如果已設置)
您可以使用這些屬性來識別、過濾和處理基於您需求的附件。
如何按名稱或型別篩選附件?
由於附件是使用顯示名稱儲存的,您可以使用字串操作來篩選它們:
// Filter by file extension (assuming names include extensions)var imageAttachments = pdf.Attachments .Where(a => a.Name.EndsWith(".jpg") || a.Name.EndsWith(".png") || a.Name.EndsWith(".gif")) .ToList();// Filter by name patternvar reportsOnly = pdf.Attachments .Where(a => a.Name.StartsWith("Report_")) .ToList();
// Filter by file extension (assuming names include extensions)
var imageAttachments = pdf.Attachments
.Where(a => a.Name.EndsWith(".jpg") ||
a.Name.EndsWith(".png") ||
a.Name.EndsWith(".gif"))
.ToList();
// Filter by name pattern
var reportsOnly = pdf.Attachments
.Where(a => a.Name.StartsWith("Report_"))
.ToList();
' Filter by file extension (assuming names include extensions)Dim imageAttachments = pdf.Attachments _ .Where(Function(a) a.Name.EndsWith(".jpg") OrElse _ a.Name.EndsWith(".png") OrElse _ a.Name.EndsWith(".gif")) _ .ToList()' Filter by name patternDim reportsOnly = pdf.Attachments _ .Where(Function(a) a.Name.StartsWith("Report_")) _ .ToList()
' Filter by file extension (assuming names include extensions)
Dim imageAttachments = pdf.Attachments _
.Where(Function(a) a.Name.EndsWith(".jpg") OrElse _
a.Name.EndsWith(".png") OrElse _
a.Name.EndsWith(".gif")) _
.ToList()
' Filter by name pattern
Dim reportsOnly = pdf.Attachments _
.Where(Function(a) a.Name.StartsWith("Report_")) _
.ToList()
using IronPdf;using System.Linq;// Open existing PDFPdfDocument pdf = PdfDocument.FromFile("addAttachment.pdf");// Add attachment to the PDFPdfAttachmentCollection retrieveAttachments = pdf.Attachments;// Remove attachment from PDFpdf.Attachments.RemoveAttachment(retrieveAttachments.First());pdf.SaveAs("removeAttachment.pdf");
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");
ImportsIronPdfImportsSystem.Linq' Open existing PDFPrivate pdf AsPdfDocument = PdfDocument.FromFile("addAttachment.pdf")' Add attachment to the PDFPrivate retrieveAttachments AsPdfAttachmentCollection = pdf.Attachments' Remove attachment from PDFpdf.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")
// Remove all attachments that match a patternvar attachmentsToRemove = pdf.Attachments .Where(a => a.Name.StartsWith("temp_")) .ToList();foreach (var attachment in attachmentsToRemove){ pdf.Attachments.RemoveAttachment(attachment);}// Or remove all attachments at oncewhile (pdf.Attachments.Count > 0){ pdf.Attachments.RemoveAttachment(pdf.Attachments.First());}
// Remove all attachments that match a pattern
var attachmentsToRemove = pdf.Attachments
.Where(a => a.Name.StartsWith("temp_"))
.ToList();
foreach (var attachment in attachmentsToRemove)
{
pdf.Attachments.RemoveAttachment(attachment);
}
// Or remove all attachments at once
while (pdf.Attachments.Count > 0)
{
pdf.Attachments.RemoveAttachment(pdf.Attachments.First());
}
ImportsSystem.Linq' Remove all attachments that match a patternDim attachmentsToRemove = pdf.Attachments _ .Where(Function(a) a.Name.StartsWith("temp_")) _ .ToList()For Each attachment In attachmentsToRemove pdf.Attachments.RemoveAttachment(attachment)Next' Or remove all attachments at onceWhile pdf.Attachments.Count > 0 pdf.Attachments.RemoveAttachment(pdf.Attachments.First())EndWhile
Imports System.Linq
' Remove all attachments that match a pattern
Dim attachmentsToRemove = pdf.Attachments _
.Where(Function(a) a.Name.StartsWith("temp_")) _
.ToList()
For Each attachment In attachmentsToRemove
pdf.Attachments.RemoveAttachment(attachment)
Next
' Or remove all attachments at once
While pdf.Attachments.Count > 0
pdf.Attachments.RemoveAttachment(pdf.Attachments.First())
End While
如何確認附件已成功移除?
您可以通過幾種方式確認附件的移除:
// Check the attachment countint attachmentCountBefore = pdf.Attachments.Count;pdf.Attachments.RemoveAttachment(targetAttachment);int attachmentCountAfter = pdf.Attachments.Count;// Verify the count decreasedif (attachmentCountAfter < attachmentCountBefore){Console.WriteLine("Attachment successfully removed");}// Check if specific attachment existsbool attachmentExists = pdf.Attachments .Any(a => a.Name == "specificFile.txt");
// Check the attachment count
int attachmentCountBefore = pdf.Attachments.Count;
pdf.Attachments.RemoveAttachment(targetAttachment);
int attachmentCountAfter = pdf.Attachments.Count;
// Verify the count decreased
if (attachmentCountAfter < attachmentCountBefore)
{
Console.WriteLine("Attachment successfully removed");
}
// Check if specific attachment exists
bool attachmentExists = pdf.Attachments
.Any(a => a.Name == "specificFile.txt");
' Check the attachment countDim attachmentCountBefore AsInteger = pdf.Attachments.Countpdf.Attachments.RemoveAttachment(targetAttachment)Dim attachmentCountAfter AsInteger = pdf.Attachments.Count' Verify the count decreasedIf attachmentCountAfter < attachmentCountBefore ThenConsole.WriteLine("Attachment successfully removed")End If' Check if specific attachment existsDim attachmentExists AsBoolean = pdf.Attachments _ .Any(Function(a) a.Name = "specificFile.txt")
' Check the attachment count
Dim attachmentCountBefore As Integer = pdf.Attachments.Count
pdf.Attachments.RemoveAttachment(targetAttachment)
Dim attachmentCountAfter As Integer = pdf.Attachments.Count
' Verify the count decreased
If attachmentCountAfter < attachmentCountBefore Then
Console.WriteLine("Attachment successfully removed")
End If
' Check if specific attachment exists
Dim attachmentExists As Boolean = pdf.Attachments _
.Any(Function(a) a.Name = "specificFile.txt")
PDF 內容包括當您查看 PDF 時所見的可見文字、圖像和格式。附件是嵌入 PDF 內的單獨檔案,但不會出現在主文件檢視中。使用 IronPDF,附件可以在 PDF 檢視器的附件面板中存取,作為補充材料。
如何從 PDF 中移除附件?
IronPDF 提供了一個 RemoveAttachment() 方法以程式化方式移除嵌入的 PDF 附件。AddAttachment 方法返回一個 PdfAttachment 物件,您可以稍後參考以進行移除操作。
使用者在 PDF 檢視器中可在哪裡找到附件?
在使用 IronPDF 新增附件後,使用者可以通過 PDF 檢視器的工具欄存取它們。大多數 PDF 檢視器,包括 Google Chrome 的內建檢視器,顯示一個附件圖標或面板,其中嵌入的檔案可以打開或保存。
Is it possible to filter PDF attachments by type or name in IronPDF?
Yes, you can filter attachments using the `Attachments` collection based on file extensions or specific name patterns.
What are the best practices for managing PDF attachments in IronPDF?
Some best practices include managing file sizes, using descriptive naming conventions, securing sensitive content with passwords, and leveraging asynchronous operations for better performance.
How can I verify if an attachment was successfully removed?
You can verify attachment removal by checking the number of attachments before and after removal or confirming the absence of the specific attachment by name in the `Attachments` collection.
What happens to the PDF file size when attachments are removed using IronPDF?
Removing attachments with IronPDF reduces the file size roughly by the size of the attachment, as the attachment data is completely removed from the PDF file.