PDF转换控件Aspose.PDF for .Net使用教程(六):将SWF文件注释添加到PDF文档

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

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

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

第二章:使用注释

▲第二节:添加,删除和获取注释

将SWF文件注释添加到PDF文档


PDF文档中的注释包含在Page对象的Annotations集合中。此集合仅包含该单个页面的所有注释:每个页面都有自己的Annotations集合。要向特定页面添加注释,请Annotations使用该Add方法将其添加到该页面的集合中。要将SWF文件作为注释包含在PDF文档中,请使用命名空间中的ScreenAnnotation类Aspose.PDF.InteractiveFeatures.Annotations。

ScreenAnnotation 有三个参数:

  1. 要添加注释的页面
  2. 矩形对象,它定义了PDF中显示注释的区域
  3. 指向SWF多媒体文件的路径

要添加SWF文件作为注释:

  1. 创建一个ScreenAnnotation实例
  2. 使用add方法将其添加到页面的注释集合中

下面的代码片段向您展示了如何在PDF页面中添加SWF注释:

//指向documents目录的路径string dataDir = RunExamples.GetDataDir_AsposePdf_Annotations();// 打开PDF文件Document doc = new Document(dataDir + "AddSwfFileAsAnnotation.pdf");            // 获取需要添加注释的页面的引用Page page = doc.Pages[1];            // 使用.swf多媒体文件作为参数创建ScreenAnnotation对象ScreenAnnotation annotation = new ScreenAnnotation(page, new Aspose.Pdf.Rectangle(0, 400, 600, 700), dataDir + "input.swf");           // 将注释添加到页面的注释集合中page.Annotations.Add(annotation);dataDir = dataDir + "AddSwfFileAsAnnotation_out.pdf";//保存带有注释的更新PDF文档doc.Save(dataDir);

从PDF文件的页面中删除所有注释

一个Page对象的AnnotationCollection集合包含对特定页面的所有注释。要从页面中删除所有注释,请调用集合的Delete方法AnnotationCollectoin。

以下代码段显示了如何从特定页面删除所有注释:

// 文档目录的路径string dataDir = RunExamples.GetDataDir_AsposePdf_Annotations();//打开文档Document pdfDocument = new Document(dataDir + "DeleteAllAnnotationsFromPage.pdf");//删除特定注释pdfDocument.Pages[1].Annotations.Delete();dataDir = dataDir + "DeleteAllAnnotationsFromPage_out.pdf";//保存更新的文档pdfDocument.Save(dataDir);
从PDF文件中删除特定注释


要从PDF中删除特定注释,请调用AnnotationCollection集合的Delete方法。此集合属于该Page对象。该Delete方法需要您要删除的注释的索引。然后,保存更新的PDF文件。以下代码段显示了如何删除特定注释:

// 文档目录的路径string dataDir = RunExamples.GetDataDir_AsposePdf_Annotations();//打开文档Document pdfDocument = new Document(dataDir + "DeleteParticularAnnotation.pdf");//删除特定注释pdfDocument.Pages[1].Annotations.Delete(1);dataDir = dataDir + "DeleteParticularAnnotation_out.pdf";// 保存更新的文档pdfDocument.Save(dataDir);
从PDF文档的页面获取所有注释

Aspose.PDF允许您从整个文档或给定页面获取注释。要从PDF文档中的页面获取所有注释,请遍历AnnotationCollection所需页面资源的集合。以下代码段显示了如何获取页面的所有注释:

//文档目录的路径string dataDir = RunExamples.GetDataDir_AsposePdf_Annotations();//打开文档Document pdfDocument = new Document(dataDir + "GetAllAnnotationsFromPage.pdf");//遍历所有注释foreach (MarkupAnnotation annotation in pdfDocument.Pages[1].Annotations){    //获取注释属性    Console.WriteLine("Title : {0} ", annotation.Title);    Console.WriteLine("Subject : {0} ", annotation.Subject);    Console.WriteLine("Contents : {0} ", annotation.Contents);                }
从PDF文件中获取特定注释

注释与单个页面相关联并存储在Page对象的AnnotationCOllection集合中。要获取特定注释,请指定其索引。例如,这返回一个Annotation需要转换为特定注释类型的对象TextAnnotation。以下代码段显示了如何获取特定注释及其属性:

//文档目录的路径string dataDir = RunExamples.GetDataDir_AsposePdf_Annotations();//打开文档Document pdfDocument = new Document(dataDir + "GetParticularAnnotation.pdf");//获取特定注释TextAnnotation textAnnotation = (TextAnnotation)pdfDocument.Pages[1].Annotations[1];            //获取注释属性Console.WriteLine("Title : {0} ", textAnnotation.Title);Console.WriteLine("Subject : {0} ", textAnnotation.Subject);Console.WriteLine("Contents : {0} ", textAnnotation.Contents);
获取注释资源

Aspose.PDF允许您从整个文档或给定页面获取注释资源。以下代码片段显示了如何获取注释资源作为输入PDF文件的FileSpecification对象:

// 文档目录的路径string dataDir = RunExamples.GetDataDir_AsposePdf_Annotations();//打开文档Document doc = new Document(dataDir + "AddAnnotation.pdf");//创建注释ScreenAnnotation sa = new ScreenAnnotation(doc.Pages[1], new Rectangle(100, 400, 300, 600), dataDir + "AddSwfFileAsAnnotation.swf");doc.Pages[1].Annotations.Add(sa);//保存文档doc.Save(dataDir + "GetResourceOfAnnotation_Out.pdf");//打开文档Document doc1 = new Document(dataDir + "GetResourceOfAnnotation_Out.pdf");//获取注释的操作RenditionAction action = (doc.Pages[1].Annotations[1] as ScreenAnnotation).Action as RenditionAction;//获取演绎动作的再现Rendition rendition = ((doc.Pages[1].Annotations[1] as ScreenAnnotation).Action as RenditionAction).Rendition;//媒体剪辑MediaClip clip = (rendition as MediaRendition).MediaClip;FileSpecification data = (clip as MediaClipData).Data;MemoryStream ms = new MemoryStream();byte[] buffer = new byte[1024];int read = 0;//可以在FileSpecification.Contents中访问媒体数据Stream source = data.Contents;while ((read = source.Read(buffer, 0, buffer.Length)) > 0){    ms.Write(buffer, 0, read);}Console.WriteLine(rendition.Name);Console.WriteLine(action.RenditionOperation);

*想要购买Aspose.PDF for .NET正版授权的朋友可以联系在线客服了解详情哦~

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

1560231367164.png

标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2019年7月1日
下一篇 2019年7月1日

相关推荐

发表回复

登录后才能评论