PDF轉(zhuǎn)換控件Aspose.PDF for .Net使用教程(五):在現(xiàn)有PDF文件中添加注釋
Aspose.PDF for .NET是一種高PDF處理和解析API,用于在跨平臺(tái)應(yīng)用程序中執(zhí)行文檔管理和操作任務(wù)。API可以輕松用于生成、修改、轉(zhuǎn)換、渲染、保護(hù)和打印PDF文檔,而無(wú)需使用Adobe Acrobat。此外,API還提供PDF壓縮選項(xiàng),表格創(chuàng)建和操作,圖形和圖像功能,廣泛的超鏈接功能,印章和水印任務(wù),擴(kuò)展的安全控制和自定義字體處理。
【下載體驗(yàn)Aspose.PDF for .NET最新版】
在接下來(lái)的系列教程中,將為開發(fā)者帶來(lái)Aspose.PDF for .NET的一系列使用教程,例如進(jìn)行文檔間的轉(zhuǎn)換,如何標(biāo)記PDF文件,如何使用表單和圖表等等。
第二章:使用注釋
▲第二節(jié):添加,刪除和獲取注釋
在現(xiàn)有PDF文件中添加注釋
注釋包含在Annotations特定的集合中Page。此集合僅包含該單個(gè)頁(yè)面的注釋; 每個(gè)頁(yè)面都有自己的Annotations集合。要向特定頁(yè)面添加注釋,請(qǐng)Annotations使用該Add方法將其添加到該頁(yè)面的集合中。
- 首先創(chuàng)建要添加到PDF的注釋。
- 然后打開輸入PDF。
- 將注釋添加到Page對(duì)象的Annotations集合中。
以下代碼段顯示如何在PDF頁(yè)面中添加注釋:
//文檔目錄的路徑。 string dataDir = RunExamples.GetDataDir_AsposePdf_Annotations(); //打開文檔 Document pdfDocument = new Document(dataDir + "AddAnnotation.pdf"); //創(chuàng)建注釋 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); //在頁(yè)面的注釋集合中添加注釋 pdfDocument.Pages[1].Annotations.Add(textAnnotation); dataDir = dataDir + "AddAnnotation_out.pdf"; //保存輸出文件 pdfDocument.Save(dataDir);
●隱形注釋
有時(shí),有必要?jiǎng)?chuàng)建在查看時(shí)在文檔中不可見(jiàn)的水印,但在打印文檔時(shí)應(yīng)該可見(jiàn)。為此目的使用注釋標(biāo)志。以下代碼段顯示了如何操作:
//文檔目錄的路徑。 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
InkAnnotation表示由一個(gè)或多個(gè)不相交點(diǎn)組成的徒手涂鴉。請(qǐng)嘗試使用以下代碼段在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);
●設(shè)置InkAnnotation的線寬
可以使用LineInfo和Border對(duì)象更改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);
●添加WatermarkAnnotation
可以在PDF頁(yè)面的特定位置使用WaterarkAnnotation添加水印文本,也可以使用不透明度屬性控制水印的不透明度。請(qǐng)檢查以下代碼段以添加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");
●添加RichMediaAnnotation
當(dāng)您需要在PDF文檔中添加外部視頻鏈接時(shí),您可以使用MovieAnnotaiton。但是當(dāng)需要在PDF文檔中嵌入媒體時(shí),您需要使用RichMediaAnnotation。此注釋允許將媒體文件嵌入PDF文檔中并設(shè)置視頻/音頻播放器,實(shí)現(xiàn)為Flash應(yīng)用程序。由于許可限制,我們不能在產(chǎn)品中包含第三方Flash腳本,因此您應(yīng)該提供播放視頻或音頻的腳本。您應(yīng)該提供Flash應(yīng)用程序代碼。例如,您可以使用隨Adobe Acrobat一起分發(fā)的videoplayer.swf和audioplayer.swf,可以在Acrobat文件夾的Multimedia Skins / Players子文件夾中找到。其他選擇是使用StrobeMediaPLayback.swf播放器或在flash中實(shí)現(xiàn)的任何其他視頻播放器。
RichMediaAnnotation 可以使用以下類的方法/屬性:
- Stream CustomPlayer { set; }:允許設(shè)置用于播放視頻的播放器。
- string CustomFlashVariables { set; }:允許設(shè)置傳遞給Flash應(yīng)用程序的變量。該行是“key = value”對(duì)的集合,用“&”分隔;
- void AddCustomData(strig name, Stream data):為播放器添加其他數(shù)據(jù)。例如,source = video.mp4&autoPlay = true&scale = 100
- ActivationEvent ActivateOn { get; set}:事件激活玩家; 可能的值有Click,PageOpen,PageVisible
- void SetContent(Stream stream, string name):設(shè)置要播放的視頻/音頻數(shù)據(jù)
- void Update():創(chuàng)建注釋的數(shù)據(jù)結(jié)構(gòu)。應(yīng)該最后調(diào)用此方法
- void SetPoster(Stream):設(shè)置視頻的海報(bào),即播放器未激活時(shí)顯示的圖片
嵌入視頻文件(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");
-- 未完待續(xù) --
*想要購(gòu)買Aspose.PDF for .NET正版授權(quán)的朋友可以聯(lián)系在線客服了解詳情哦~
歡迎加入ASPOSE技術(shù)交流QQ群,各類資源及時(shí)分享,技術(shù)問(wèn)題交流討論!(掃描下方二維碼加入群聊)