IronPDF 操作指南 添加和移除附件 How to Add and Remove PDF Attachments Jordi Bardia 已更新:八月 14, 2025 Download IronPDF NuGet 下载 DLL 下载 Windows 安装程序 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article This article was translated from English: Does it need improvement? Translated View the article in English PDF 文档中的附件是指嵌入在 PDF 文件本身内的文件或附加数据。 这不同于 PDF 的常规内容,包括可见的文本、图像和格式,当您查看 PDF 时可以看到。 这些附件可以是各种文件类型,包括图像、文件、电子表格或其他格式。 通常,附件用于提供用户在打开 PDF 时可以访问的附加参考资料或补充数据。 快速入门:向 PDF 添加附件 使用 IronPDF 强大的库可轻松将附件添加到您的 PDF 文档中。 该快速示例演示了如何将文件嵌入为 PDF 中的附件。 只需加载现有 PDF,使用 AddAttachment 方法,然后保存更新后的文档。 此过程确保您的补充材料与 PDF 无缝集成,使其可以直接从任何 PDF 查看器访问。 Get started making PDFs with NuGet now: Install IronPDF with NuGet Package Manager PM > Install-Package IronPdf Copy and run this code snippet. var pdf = IronPdf.PdfDocument.FromFile("example.pdf"); pdf.Attachments.AddAttachment("file.txt", System.IO.File.ReadAllBytes("file.txt")); pdf.SaveAs("updated.pdf"); Deploy to test on your live environment Start using IronPDF in your project today with a free trial Free 30 day Trial class="hsg-featured-snippet"> 最小化工作流程(5 步) 从 NuGet 下载 IronPDF C# 库 加载现有 PDF 或渲染一个新 PDF 使用 File.ReadAllBytes 方法将要附加的文件作为 byte[] 导入 使用 AddAttachment 方法将其附加到 PDF 使用 RemoveAttachment 方法从 PDF 中删除附件 添加附件示例 要将文件添加为附件,首先在程序中将其加载为 byte[]。 最简单的方法是使用 File.ReadAllBytes 方法。 在将文件加载为 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") $vbLabelText $csharpLabel AddAttachment 函数输出一个 PdfAttachment 对象,我们可以保留以备将来参考,或在需要时将其删除。 保存 PDF 后,您可以从 PDF 查看器的工具栏中打开附件。 我们在下图中展示了如何在 Google Chrome 的 PDF 查看器中找到此功能: 从那里,您可以点击它并将附件保存到您自己的存储中。 检索附件示例 PDF 中的附件可以通过访问 PdfDocument 对象的 Attachments 属性以二进制数据形式检索。 通过二进制数据,您可以将附件导出为其各自的文件格式。 :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 $vbLabelText $csharpLabel <hr 删除附件示例 要删除附件,只需使用 RemoveAttachment 函数。 此方法需要附件的引用,可以从 Attachments 属性中检索。 我们展示如何使用上面保存的文件来做到这一点。 :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") $vbLabelText $csharpLabel 删除附件并在 PDF 查看器中打开生成的文件后,您会看到附件不再出现: 准备好看看您还能做些什么吗? 在这里查看我们的教程页面:组织PDF 常见问题解答 如何使用C#在PDF文档中添加附件? 您可以通过使用File.ReadAllBytes方法将文件加载为字节数组,然后使用AddAttachment方法将其嵌入到PDF中来使用IronPDF向PDF文档添加附件。 如何移除PDF中的附件? 要使用IronPDF移除PDF中的附件,请使用RemoveAttachment方法。您必须首先从PdfDocument对象的Attachments属性获取附件的引用。 哪种文件格式可以作为附件添加到PDF中? 可以附加到PDF中的文件格式范围广泛,包括图片、文档、电子表格和其他文件类型。 如何检索和导出PDF中的附件? 要使用IronPDF从PDF中检索附件,访问PdfDocument对象的Attachments属性以获取它们作为二进制数据,然后可以将其导出到磁盘。 开始在C#中管理PDF附件需要进行哪些步骤? 要开始管理PDF附件,从NuGet下载IronPDF C#库,加载或创建PDF文档,并使用相关方法添加或移除附件。 用户是否可以访问和保存PDF中的附件? 是的,一旦使用IronPDF将附件添加到PDF中,可以通过PDF查看器的工具栏访问附件,允许用户将其保存到存储中。 如何确保PDF附件的更改被保存? 在使用IronPDF修改PDF中的附件后,使用SaveAs方法将更新的PDF文档保存到所需位置。 可见的PDF内容和附件有什么区别? 可见的PDF内容包括文字、图片和格式,而附件是嵌入PDF中的附加文件或数据,提供补充信息。 Jordi Bardia 立即与工程团队聊天 软件工程师 Jordi 最擅长 Python、C# 和 C++,当他不在 Iron Software 利用这些技能时,他就在游戏编程。分享产品测试、产品开发和研究的责任,Jordi 在持续的产品改进中增加了巨大的价值。多样的经验使他面临挑战并保持投入,他表示这是在 Iron Software 工作的最喜欢的方面之一。Jordi 在佛罗里达州迈阿密长大,并在佛罗里达大学学习计算机科学和统计学。 准备开始了吗? Nuget 下载 16,154,058 | 版本: 2025.11 刚刚发布 免费 NuGet 下载 总下载量:16,154,058 查看许可证