Aspose.PDF for .NET使用教程(一):使用附件

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

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

【下载体验Aspose.PDF for .NET最新版】

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

第一章:使用附件

附件可以包含各种各样的信息,并且可以是各种文件类型。想要向PDF文件添加附件只需两步:

  1. 使用要添加的文件和文件描述创建一个文件具体化对象。
  2. 使用集合的Add方法将Filspeciification对象添加到文档对象的EmbeddedFiles集合中。

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

//文档目录的路径。string  dataDir  =  RunExamples。GetDataDir_AsposePdf_Attachments();//打开文档Document pdfDocument = new Document(dataDir + "AddAttachment.pdf");//设置要添加为附件的新文件FileSpecification  fileSpecification  =  new  FileSpecification(dataDir  +  “ test.txt ”,“ Sample text file ”);//添加附件到文档的附件集合pdfDocument.EmbeddedFiles.Add(fileSpecification);dataDir = dataDir + "AddAttachment_out.pdf";//保存新输出pdfDocument.Save(dataDir);

从PDF文档获取所有附件


使用Aspose.PDF,可以从PDF文档中获取所有附件。当想要将文档与PDF分开保存,或者需要剥离PDF附件时,这非常有用。

要从PDF文件中获取所有附件只需两步:

  1. 循环遍历文档对象的EmbeddedFiles集合。EmbeddedFiles集合包含所有附件。这个集合的每个元素都表示一个文件具体化对象。Foreach循环通过EmbeddedFiles集合的每次迭代都返回一个filspeciification对象。
  2. 一旦对象可用,检索所有附加文件的属性或文件本身。

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

//文档目录的路径string dataDir = RunExamples.GetDataDir_AsposePdf_Attachments();//打开文档Document pdfDocument = new Document(dataDir + "GetAlltheAttachments.pdf");            //获取嵌入式文件集合EmbeddedFileCollection embeddedFiles = pdfDocument.EmbeddedFiles;            //获取嵌入文件的数量Console.WriteLine("Total files : {0}", embeddedFiles.Count);int count = 1;            //遍历集合以获取所有附件foreach (FileSpecification fileSpecification in 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(dataDir + count + "_out" + ".txt",    FileMode.Create);    fileStream.Write(fileContent, 0, fileContent.Length);    fileStream.Close();count+=1;

获得单独的附件


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

//文档目录的路径string dataDir = RunExamples.GetDataDir_AsposePdf_Attachments();//打开文档Document pdfDocument = new Document(dataDir + "GetIndividualAttachment.pdf");//获取特定的嵌入文件FileSpecification fileSpecification = pdfDocument.EmbeddedFiles[1];            //获取文件属性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(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方法保存更新的文件。
//文档目录的路径string dataDir = RunExamples.GetDataDir_AsposePdf_Attachments();//打开文档Document pdfDocument = new Document(dataDir + "DeleteAllAttachments.pdf");//删除所有附件pdfDocument.EmbeddedFiles.Delete();dataDir = dataDir + "DeleteAllAttachments_out.pdf";//保存更新的文件pdfDocument.Save(dataDir);

获取附件信息


附件信息保存在filspeciification对象中,与文档对象的EmbeddedFiles集合中的其他附件一起收集。文件化对象提供了获取hteattchment信息的方法,例如:

  • Name – 文件名。
  • Description – 文件描述。
  • MIMEType – 文件的MIME类型。
  • Params-有关文件的参数信息。

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

//文档目录的路径string dataDir = RunExamples.GetDataDir_AsposePdf_Attachments();//打开文档Document pdfDocument = new Document(dataDir + "GetAttachmentInfo.pdf");            //获取特定的嵌入文件FileSpecification fileSpecification = pdfDocument.EmbeddedFiles[1];            //获取文件属性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);}

— 未完待续 —


20万+用户答谢惠倒计时,ASPOSE系列产品火热促销中,最高直降8万元!欲购从速!!>>立即进入优惠专场

ASPOSE技术交流QQ群现已开通,各类资源及时分享,欢迎交流讨论!(扫描下方二维码加入群聊)

1560231367164.png

标签:

来源:慧都

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

上一篇 2019年5月24日
下一篇 2019年5月24日

相关推荐

发表回复

登录后才能评论