
PDF 格式支持向 PDF 文件添加附件,类似于电子邮件附件。 PDF 附件可以是 TXT、DOCX、XLSX 或任何其他文档格式。在本文中,您将学习如何在您的 .NET 应用程序中实现一些基本的 PDF 附件操作功能。在本文结束时,您将能够使用 C# 以编程方式提取、添加或删除 PDF 中的附件。
PDF 附件操作 API - 免费下载
Aspose.PDF for .NET 是一个著名的 PDF 操作 API,可让您无缝处理 PDF 文件。您可以通过几个步骤阅读、创建、编辑和转换 PDF 文件并操作 PDF 附件。 API 可以作为 DLL 或 MSI 下载,也可以使用 NuGet 安装。
Install-Package Aspose.Pdf
使用 C# 将附件添加到 PDF
Aspose.PDF for .NET 还允许您将附件添加到 PDF 文件中。为此,您只需使用 FileSpecification 类将文件添加到 Document.EmbeddedFiles 集合。以下是向 PDF 文档添加附件的步骤。
- 使用 Document 类创建一个新的 PDF 文档。
- 创建 FileSpecification 类的实例以加载附件文件。
- 使用 Document.EmbeddedFiles.Add(FileSpecification) 方法添加附件。
- 使用 Document.Save(String) 方法保存文档。
以下代码示例展示了如何使用 C# 向 PDF 文档添加附件。
// 如需完整的示例和数据文件,请访问 https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// 打开文档
Document pdfDocument = new Document("document.pdf");
// 设置要添加为附件的新文件
FileSpecification fileSpecification = new FileSpecification("test.txt", "Sample text file");
// 将附件添加到文档的附件集合
pdfDocument.EmbeddedFiles.Add(fileSpecification);
// 保存新输出
pdfDocument.Save("output.pdf");
使用 C# 提取 PDF 附件
首先,让我们看看如何从 PDF 文档中检索附件。为此,请按照以下步骤操作:
- 创建 Document 类的实例。
- 使用 Document.EmbeddedFiles 属性将附件放入 EmbeddedFileCollection 对象。
- 使用 FileSpecification 对象循环通过 EmbeddedFileCollection 中的附件。
- 使用 FileSpecification 对象访问每个附件的属性。
- 将附件另存为文件(如果需要)。
以下代码示例展示了如何使用 C# 提取 PDF 附件。
// 如需完整的示例和数据文件,请访问 https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// 打开文档
Document pdfDocument = new Document("document.pdf");
// 获取特定的嵌入文件
foreach(FileSpecification fileSpecification in pdfDocument.EmbeddedFiles)
{
// 获取文件属性
Console.WriteLine("Name: {0}", fileSpecification.Name);
Console.WriteLine("Description: {0}", fileSpecification.Description);
Console.WriteLine("Mime Type: {0}", fileSpecification.MIMEType);
// 检查参数对象是否包含参数
if (fileSpecification.Params != null)
{
Console.WriteLine("CheckSum: {0}",
fileSpecification.Params.CheckSum);
Console.WriteLine("Creation Date: {0}",
fileSpecification.Params.CreationDate);
Console.WriteLine("Modification Date: {0}",
fileSpecification.Params.ModDate);
Console.WriteLine("Size: {0}", fileSpecification.Params.Size);
}
// 获取附件并写入文件或流
byte[] fileContent = new byte[fileSpecification.Contents.Length];
fileSpecification.Contents.Read(fileContent, 0, fileContent.Length);
FileStream fileStream = new FileStream(fileSpecification.Name, FileMode.Create);
fileStream.Write(fileContent, 0, fileContent.Length);
fileStream.Close();
}
使用 C# 从 PDF 中删除附件
您可以从 PDF 文件中删除全部或特定附件。为此,Aspose.PDF for .NET 提供了以下方法:
- 删除() - Deletes all the attachments.
- 删除(字符串文件名) - Deletes attachment by name.
- DeleteByKey(字符串键) - Deletes attachment by key in the collection.
以下是从 PDF 中删除附件的步骤。
- 创建 Document 类的实例以加载 PDF 文件。
- 使用 Document.EmbeddedFiles.Delete() (或其他删除方法)删除附件。
- 使用 Document.Save(String) 方法保存文件。
以下代码示例展示了如何在 C# 中从 PDF 文件中删除附件。
// 如需完整的示例和数据文件,请访问 https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// 打开文档
Document pdfDocument = new Document("document.pdf");
// 删除所有附件
pdfDocument.EmbeddedFiles.Delete();
// 保存更新的文件
pdfDocument.Save("output.pdf");
结论
在本文中,您了解了如何以编程方式处理 PDF 文档中的附件。分步指南、API 参考和代码示例展示了如何使用 C# 在 PDF 文件中检索、添加和删除附件。您可以使用 文档 了解更多关于 Aspose.PDF for .NET 的信息。