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

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

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

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

第一章:使用附件

附件能够包含各类各样的信息,而且能够是各类文件类型。想要向PDF文件添加附件只需两步:code

  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文档中获取全部附件:ip

//文档目录的路径
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对象中指定附件索引。请尝试使用如下代码片断。ci

//文档目录的路径
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);
}

-- 未完待续 --

更多教程资源可关注ASPOSE技术交流QQ群(642018183)哦~欢迎交流讨论!

相关文章
相关标签/搜索