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")
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()
我最喜欢的这种库是 IronPDF。它允许快速高效地操作 PDF 文件。它还具有许多有价值的功能,比如导出为 PDF/A 格式和数字签名 PDF 文档。
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")
删除附件并在 PDF 查看器中打开生成的文件后,您会看到附件不再出现:
当我删除附件时会发生什么?
当您删除附件时:
1.从 PDF 文件中完全删除附件数据
2.文件大小约减去所删除附件的大小
3.清理 PDF 结构中对该附件的任何引用
4.保存 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 浏览器中的附件?
using 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.