PDF管理控件Aspose.PDF for .Net使用教程(二十八):添加、获取、删除PDF中的附件

在本系列教程中,将为开发者带来Aspose.PDF for .NET的一系列使用教程,例如进行文档间的转换,如何标记PDF文件,如何使用表单和图表等等。本文将介绍如何添加、获取、删除PDF中的附件。

Aspose.PDF for .NET是一种高PDF处理和解析API,用于在跨平台应用程序中执行文档管理和操作任务。API可以轻松用于生成、修改、转换、渲染、保护和打印PDF文档,而无需使用Adobe Acrobat。此外,API还提供PDF压缩选项,表格创建和操作,图形和图像功能,广泛的超链接功能,印章和水印任务,扩展的安全控制和自定义字体处理。

在接下来的系列教程中,将为开发者带来Aspose.PDF for .NET的一系列使用教程,例如进行文档间的转换,如何标记PDF文件,如何使用表单和图表等等。本文将介绍如何添加、获取、删除PDF中的附件。

>>Aspose.PDF for .NET更新至最新版v20.2,欢迎下载体验。


PDF文件允许将文件作为内容附加,并且是PDF文件格式的主要功能之一。可以将不同的文件格式(如Word DOC / DOCX,Excel XLS / XLSX,EML,JPG,PNG,GLB等)嵌入PDF文件中。.NET API的Aspose.PDF可让您使用.NET应用程序处理PDF文件中的附件。

该API可以在PDF文件中添加/嵌入附件,从PDF获取附件信息,从PDF文件访问单个附件,以及使用C#或任何其他.NET编程语言从PDF中删除附件。

将附件添加到PDF

附件可以包含多种信息,并且可以具有多种文件类型。步骤如下:

  1. 创建一个新的C#项目。
  2. 添加对Aspose.PDF DLL的引用。
  3. 创建一个 Document对象。
  4. FileSpecification使用要添加的文件和文件描述创建一个对象。
  5. 添加FileSpecification对象到Document对象的EmbeddedFiles集合,集合的Add方法。

该EmbeddedFiles集合包含PDF文件中的所有附件。以下代码段显示了如何在PDF文档中添加附件。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_Attachments();// Open documentDocument pdfDocument = new Document(dataDir + "AddAttachment.pdf");// Setup new file to be added as attachmentFileSpecification fileSpecification = new FileSpecification(dataDir + "test.txt", "Sample text file");// Add attachment to document's attachment collectionpdfDocument.EmbeddedFiles.Add(fileSpecification);dataDir = dataDir + "AddAttachment_out.pdf";// Save new outputpdfDocument.Save(dataDir);

从PDF文档获取所有附件

使用Aspose.PDF,可以从PDF文档中获取所有附件。当您想要将文档与PDF分开保存时,或者需要剥离附件PDF时,此功能很有用。要从PDF文件获取所有附件:

  1. 遍历Document对象的EmbeddedFiles集合。该EmbeddedFiles集合包含所有附件。此集合的每个元素代表一个FileSpecification对象。遍历EmbeddedFiles集合的foreach循环的每次迭代都会返回一个FileSpecification对象。
  2. 对象可用后,检索所有附加文件的属性或文件本身。

以下代码段显示了如何从PDF文档中获取所有附件。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_Attachments();// Open documentDocument pdfDocument = new Document(dataDir + "GetAlltheAttachments.pdf");    // Get embedded files collectionEmbeddedFileCollection embeddedFiles = pdfDocument.EmbeddedFiles;    // Get count of the embedded filesConsole.WriteLine("Total files : {0}", embeddedFiles.Count);int count = 1;    // Loop through the collection to get all the attachmentsforeach (FileSpecification fileSpecification in embeddedFiles){    Console.WriteLine("Name: {0}", fileSpecification.Name);    Console.WriteLine("Description: {0}",    fileSpecification.Description);    Console.WriteLine("Mime Type: {0}", fileSpecification.MIMEType);      // Check if parameter object contains the parameters    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);    }     // Get the attachment and write to file or stream    byte[] fileContent = new byte[fileSpecification.Contents.Length];    fileSpecification.Contents.Read(fileContent, 0,    fileContent.Length);    FileStream fileStream = new FileStream(dataDir + count + "_out" + ".txt",    FileMode.Create);    fileStream.Write(fileContent, 0, fileContent.Length);    fileStream.Close();    count+=1;}

获取个人附件

为了获得单个附件,我们可以在Document实例的EmbeddedFiles对象中指定附件的索引。请尝试使用以下代码段。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_Attachments();// Open documentDocument pdfDocument = new Document(dataDir + "GetIndividualAttachment.pdf");// Get particular embedded fileFileSpecification fileSpecification = pdfDocument.EmbeddedFiles[1];    // Get the file propertiesConsole.WriteLine("Name: {0}", fileSpecification.Name);Console.WriteLine("Description: {0}", fileSpecification.Description);Console.WriteLine("Mime Type: {0}", fileSpecification.MIMEType);    // Check if parameter object contains the parametersif (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);}    // Get the attachment and write to file or streambyte[] fileContent = new byte[fileSpecification.Contents.Length];fileSpecification.Contents.Read(fileContent, 0, fileContent.Length);FileStream fileStream = new FileStream(dataDir + "test_out" + ".txt", FileMode.Create);fileStream.Write(fileContent, 0, fileContent.Length);fileStream.Close();

从PDF文档中删除所有附件

Aspose.PDF可以从PDF文件中删除附件。PDF文档的附件保存在Document对象的EmbeddedFiles集合中。要删除与PDF文件关联的所有附件:

  1. 调用EmbeddedFiles集合的Delete方法。
  2. 使用Document对象的Save方法保存更新的文件。

以下代码段显示了如何从PDF文档中删除附件。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_Attachments();// Open documentDocument pdfDocument = new Document(dataDir + "DeleteAllAttachments.pdf");// Delete all attachmentspdfDocument.EmbeddedFiles.Delete();dataDir = dataDir + "DeleteAllAttachments_out.pdf";// Save updated filepdfDocument.Save(dataDir);

获取附件信息

附件信息在举行FileSpecification的对象,与其他附件聚集Document对象的EmbeddedFiles集合。该FileSpecification对象提供获取有关附件信息的方法,例如:

  • Name -文件名。
  • Description -文件描述。
  • MIMEType -文件的MIME类型。
  • Params-有关文件的参数信息,例如CheckSum,CreationDate,ModDate和Size。

要获取这些参数,请首先确保该Params属性不为null。可以EmbeddedFiles使用foreach循环遍历集合中的所有附件,也可以通过指定其索引值来获取单个附件。以下代码段显示了如何获取有关特定附件的信息。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_Attachments();// Open documentDocument pdfDocument = new Document(dataDir + "GetAttachmentInfo.pdf");    // Get particular embedded fileFileSpecification fileSpecification = pdfDocument.EmbeddedFiles[1];    // Get the file propertiesConsole.WriteLine("Name: {0}", fileSpecification.Name);Console.WriteLine("Description: {0}", fileSpecification.Description);Console.WriteLine("Mime Type: {0}", fileSpecification.MIMEType);    // Check if parameter object contains the parametersif (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);}

还想要更多吗可以点击阅读【2019 · Aspose最新资源整合】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询
标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年1月2日
下一篇 2020年1月2日

相关推荐

发表回复

登录后才能评论