翻译|使用教程|编辑:李显亮|2019-07-26 13:55:37.003|阅读 464 次
概述:Aspose.PDF for .NET是一种高PDF处理和解析API,用于在跨平台应用程序中执行文档管理和操作任务。API可以轻松用于生成、修改、转换、渲染、保护和打印PDF文档,而无需使用Adobe Acrobat。
# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>
相关链接:
Aspose.PDF for .NET是一种高PDF处理和解析API,用于在跨平台应用程序中执行文档管理和操作任务。API可以轻松用于生成、修改、转换、渲染、保护和打印PDF文档,而无需使用Adobe Acrobat。此外,API还提供PDF压缩选项,表格创建和操作,图形和图像功能,广泛的超链接功能,印章和水印任务,扩展的安全控制和自定义字体处理。
在接下来的系列教程中,将为开发者带来Aspose.PDF for .NET的一系列使用教程,例如进行文档间的转换,如何标记PDF文件,如何使用表单和图表等等。
注释包含在Annotations特定的集合中Page。此集合仅包含该单个页面的注释; 每个页面都有自己的Annotations集合。要向特定页面添加注释,请Annotations使用该Add方法将其添加到该页面的集合中。
以下代码段显示如何在PDF页面中添加注释:
//文档目录的路径。 string dataDir = RunExamples.GetDataDir_AsposePdf_Annotations(); //打开文档 Document pdfDocument = new Document(dataDir + "AddAnnotation.pdf"); //创建注释 TextAnnotation textAnnotation = new TextAnnotation(pdfDocument.Pages[1], new Aspose.Pdf.Rectangle(200, 400, 400, 600)); textAnnotation.Title = "Sample Annotation Title"; textAnnotation.Subject = "Sample Subject"; textAnnotation.State = AnnotationState.Accepted; textAnnotation.Contents = "Sample contents for the annotation"; textAnnotation.Open = true; textAnnotation.Icon = TextIcon.Key; Border border = new Border(textAnnotation); border.Width = 5; border.Dash = new Dash(1, 1); textAnnotation.Border = border; textAnnotation.Rect = new Aspose.Pdf.Rectangle(200, 400, 400, 600); //在页面的注释集合中添加注释 pdfDocument.Pages[1].Annotations.Add(textAnnotation); dataDir = dataDir + "AddAnnotation_out.pdf"; //保存输出文件 pdfDocument.Save(dataDir);
有时,有必要创建在查看时在文档中不可见的水印,但在打印文档时应该可见。为此目的使用注释标志。以下代码段显示了如何操作:
//文档目录的路径。 string dataDir = RunExamples.GetDataDir_AsposePdf_Annotations(); //打开文档。 Document doc = new Document(dataDir + "input.pdf"); FreeTextAnnotation annotation = new FreeTextAnnotation(doc.Pages[1], new Aspose.Pdf.Rectangle(50, 600, 250, 650), new DefaultAppearance("Helvetica", 16, System.Drawing.Color.Red)); annotation.Contents = "ABCDEFG"; annotation.Characteristics.Border = System.Drawing.Color.Red; annotation.Flags = AnnotationFlags.Print | AnnotationFlags.NoView; doc.Pages[1].Annotations.Add(annotation); dataDir = dataDir + "InvisibleAnnotation_out.pdf"; // 保存输出文件 doc.Save(dataDir);
InkAnnotation表示由一个或多个不相交点组成的徒手涂鸦。请尝试使用以下代码段在PDF文档中添加InkAnnotation:
//文档目录的路径。 string dataDir = RunExamples.GetDataDir_AsposePdf_Annotations(); Document doc = new Document(); Page pdfPage = doc.Pages.Add(); System.Drawing.Rectangle drect = new System.Drawing.Rectangle(); drect.Height = (int)pdfPage.Rect.Height; drect.Width = (int)pdfPage.Rect.Width; drect.X = 0; drect.Y = 0; Aspose.Pdf.Rectangle arect = Aspose.Pdf.Rectangle.FromRect(drect); IListinkList = new List(); Aspose.Pdf.Point[] arrpt = new Aspose.Pdf.Point[3]; inkList.Add(arrpt); arrpt[0] = new Aspose.Pdf.Point(100, 800); arrpt[1] = new Aspose.Pdf.Point(200, 800); arrpt[2] = new Aspose.Pdf.Point(200, 700); InkAnnotation ia = new InkAnnotation(pdfPage, arect, inkList); ia.Title = "XXX"; ia.Color = Aspose.Pdf.Color.LightBlue; // (GetColorFromString(stroke.InkColor)); ia.CapStyle = CapStyle.Rounded; Border border = new Border(ia); border.Width = 25; ia.Opacity = 0.5; pdfPage.Annotations.Add(ia); dataDir = dataDir + "AddlnkAnnotation_out.pdf"; //保存输出文件 doc.Save(dataDir);
可以使用LineInfo和Border对象更改InkAnnottion的宽度:
//文档目录的路径。 string dataDir = RunExamples.GetDataDir_AsposePdf_Annotations(); Document doc = new Document(); doc.Pages.Add(); IListinkList = new List(); LineInfo lineInfo = new LineInfo(); lineInfo.VerticeCoordinate = new float[] { 55, 55, 70, 70, 70, 90, 150, 60 }; lineInfo.Visibility = true; lineInfo.LineColor = System.Drawing.Color.Red; lineInfo.LineWidth = 2; int length = lineInfo.VerticeCoordinate.Length / 2; Aspose.Pdf.Point[] gesture = new Aspose.Pdf.Point[length]; for (int i = 0; i < length; i++) { gesture[i] = new Aspose.Pdf.Point(lineInfo.VerticeCoordinate[2 * i], lineInfo.VerticeCoordinate[2 * i + 1]); } inkList.Add(gesture); InkAnnotation a1 = new InkAnnotation(doc.Pages[1], new Aspose.Pdf.Rectangle(100, 100, 300, 300), inkList); a1.Subject = "Test"; a1.Title = "Title"; a1.Color = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Green); Border border = new Border(a1); border.Width = 3; border.Effect = BorderEffect.Cloudy; border.Dash = new Dash(1, 1); border.Style = BorderStyle.Solid; doc.Pages[1].Annotations.Add(a1); dataDir = dataDir + "lnkAnnotationLineWidth_out.pdf"; //保存输出文件 doc.Save(dataDir);
可以在PDF页面的特定位置使用WaterarkAnnotation添加水印文本,也可以使用不透明度属性控制水印的不透明度。请检查以下代码段以添加WatermarkAnnotation:
C#
//Load a Document Aspose.PDF.Document doc = new Aspose.PDF.Document("source.pdf"); //Load Page object to add Annotation Page page = doc.Pages[1]; //Create Annotation WatermarkAnnotation wa = new WatermarkAnnotation(page, new Aspose.PDF.Rectangle(100, 500, 400, 600)); //Add annotaiton into Annotation collection of Page page.Annotations.Add(wa); //Create TextState for Font settings Aspose.PDF.Text.TextState ts = new Aspose.PDF.Text.TextState(); ts.ForegroundColor = Aspose.PDF.Color.Blue; ts.Font = FontRepository.FindFont("Times New Roman"); ts.FontSize = 32; //Set opacity level of Annotaiton Text wa.Opacity = 0.5; //Add Text in Annotation wa.SetTextAndState(new string[] { "HELLO", "Line 1", "Line 2" }, ts); //Save the Docuemnt doc.Save("Output.pdf");
当您需要在PDF文档中添加外部视频链接时,您可以使用MovieAnnotaiton。但是当需要在PDF文档中嵌入媒体时,您需要使用RichMediaAnnotation。此注释允许将媒体文件嵌入PDF文档中并设置视频/音频播放器,实现为Flash应用程序。由于许可限制,我们不能在产品中包含第三方Flash脚本,因此您应该提供播放视频或音频的脚本。您应该提供Flash应用程序代码。例如,您可以使用随Adobe Acrobat一起分发的videoplayer.swf和audioplayer.swf,可以在Acrobat文件夹的Multimedia Skins / Players子文件夹中找到。其他选择是使用StrobeMediaPLayback.swf播放器或在flash中实现的任何其他视频播放器。
RichMediaAnnotation 可以使用以下类的方法/属性:
嵌入视频文件(C#)
string myDir = "C:/Temp/"; Aspose.PDF.Document doc = new Aspose.PDF.Document(); Page page = doc.Pages.Add(); RichMediaAnnotation rma = new RichMediaAnnotation(page, new Aspose.PDF.Rectangle(100,500, 300, 600)); //here we should specify stream containing code of the video player rma.CustomPlayer = new FileStream(@"C:\Adobe\Acrobat 11.0\Acrobat\MultimediaSkins\Players\Videoplayer.swf",FileMode.Open,FileAccess.Read); //give name to video data. This data will be embedded into document with this name and referenced from flash variables by this name. //videoName should not contain path to the file; this is rather "key" to access data inside of the PDF document string videoName = "VideoTutorial.mp4"; //also we use skin for video player string skinName = "SkinOverAllNoFullNoCaption.swf"; //compose flash variables line for player. please note that different players may have different format of the flash variables line. Refer to documentation for your player. rma.CustomFlashVariables = String.Format("source={0}&skin={1}", "VideoTutorial.mp4", skinName); //add skin code. rma.AddCustomData(skinName,new FileStream(@"C:\Program Files (x86)\Adobe\Acrobat 11.0\Acrobat\Multimedia Skins\SkinOverAllNoFullNoCaption.swf",FileMode.Open, FileAccess.Read)); //set poster for video rma.SetPoster(new FileStream(myDir + "barcode.jpg",FileMode.Open, FileAccess.Read)); Stream fs = new FileStream(myDir + videoName, FileMode.Open, FileAccess.Read); //set video content rma.SetContent(videoName, fs); //set type of the content (video) rma.Type = RichMediaAnnotation.ContentType.Video; //active player by click rma.ActivateOn = RichMediaAnnotation.ActivationEvent.Click; //update annotation data. This method should be called after all assignments/setup. This method initializes data structure of the annotation and embeds required data. rma.Update(); //add annotation on the page. page.Annotations.Add(rma); doc.Save(myDir + "Output.pdf");
嵌入音频文件(C#)
Aspose.PDF.Document doc = new Aspose.PDF.Document(); Page page = doc.Pages.Add(); //give name to audio data. This data will be embedded into document with this name and referenced from flash variables by this name. string audioName = "test_cbr.mp3"; RichMediaAnnotation rma = new RichMediaAnnotation(page, new Aspose.PDF.Rectangle(100,650, 300, 670)); Stream fs = new FileStream(myDir+audioName, FileMode.Open, FileAccess.Read); //here we should specify stream containing code of the audio player rma.CustomPlayer = new FileStream(@"C:\Program Files (x86)\Adobe\Acrobat 11.0\Acrobat\MultimediaSkins\Players\Audioplayer.swf", FileMode.Open, FileAccess.Read); //compose flash variables line for player. please note that different players may have different format of the flash variables line. Refer to documentation for your player. rma.CustomFlashVariables = String.Format("source={0}", "test_cbr.mp3"); //active player on page open event rma.ActivateOn = RichMediaAnnotation.ActivationEvent.PageOpen; //set audio content rma.SetContent(audioName, fs); //set type of the content (audio) rma.Type = RichMediaAnnotation.ContentType.Audio; //update annotation data. This method should be called after all assignments/setup. This method initializes data structure of the annotation and embeds required data. rma.Update(); //add annotation on the page. page.Annotations.Add(rma); doc.Save(myDir+"39606-2.pdf");
-- 未完待续 --
*想要购买Aspose.PDF for .NET正版授权的朋友可以了解详情哦~
欢迎加入ASPOSE技术交流QQ群,各类资源及时分享,技术问题交流讨论!(扫描下方二维码加入群聊)
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn
文章转载自: