PDF轉換控件Aspose.PDF for .Net使用教程(八):設置FreeTextAnnotation格式化和刪除單詞
Aspose.PDF for .NET是一種高PDF處理和解析API,用于在跨平臺應用程序中執行文檔管理和操作任務。API可以輕松用于生成、修改、轉換、渲染、保護和打印PDF文檔,而無需使用Adobe Acrobat。此外,API還提供PDF壓縮選項,表格創建和操作,圖形和圖像功能,廣泛的超鏈接功能,印章和水印任務,擴展的安全控制和自定義字體處理。
在接下來的系列教程中,將為開發者帶來Aspose.PDF for .NET的一系列使用教程,例如進行文檔間的轉換,如何標記PDF文件,如何使用表單和圖表等等。
第二章:使用注釋
▲第四節:設置FreeTextAnnotation格式化和刪除單詞
設置FreeTextAnnotation的格式
注釋包含在Page對象的AnnotationCollection集合中。 將FreeTextAnnotation添加到PDF文檔時,可以使用DefaultAppearance類指定格式信息,如字體,大小,顏色等。 也可以使用TextStyle屬性指定格式信息。
TextStyle類支持使用默認樣式條目。 此類允許您設置顏色,字體大小和字體名稱:
- FontName屬性獲取或設置字體名稱(字符串)。
- FontSize屬性獲取并設置默認文本大小(double)。
- System.Drawing.Color屬性獲取并設置文本顏色(顏色)。
- TextAlignment屬性獲取并設置注釋的文本對齊方式(對齊方式)。
以下代碼段顯示了如何添加具有特定文本格式的FreeTextAnnotation:
//文檔目錄的路徑。 string dataDir = RunExamples.GetDataDir_AsposePdf_Annotations(); //打開文檔 Document pdfDocument = new Document(dataDir + "SetFreeTextAnnotationFormatting.pdf"); //實例化DefaultAppearance對象 DefaultAppearance default_appearance = new DefaultAppearance("Arial", 28, System.Drawing.Color.Red); // 創建注釋 FreeTextAnnotation freetext = new FreeTextAnnotation(pdfDocument.Pages[1], new Aspose.Pdf.Rectangle(200, 400, 400, 600), default_appearance); //指定注釋的內容 freetext.Contents = "Free Text"; //將注釋添加到頁面的注釋集合中 pdfDocument.Pages[1].Annotations.Add(freetext); dataDir = dataDir + "SetFreeTextAnnotationFormatting_out.pdf"; //保存更新后的文檔 pdfDocument.Save(dataDir);
使用刪除注釋將單詞刪除
spose.PDF for .NET允許您在PDF文檔中添加,刪除和更新注釋。 其中一個類允許您刪除注釋。 當您想要刪除文檔中的一個或多個文本片段時,這非常有用。 注釋保存在Page對象的AnnotationCollection集合中。 名為StrikeOutAnnotation的類可用于向PDF文檔添加刪除注釋。
要刪除某個TextFragment:
- 在PDF文件中搜索TextFragment。
- 獲取TextFragment對象的坐標。
- 使用坐標實例化StrikeOutAnnotation對象。
以下代碼段顯示了如何搜索特定的TextFragment并向該對象添加StrikeOutAnnotation:
//文檔目錄的路徑。 string dataDir = RunExamples.GetDataDir_AsposePdf_Annotations(); //打開文檔 Document document = new Document(dataDir + "input.pdf"); //創建TextFragment Absorber實例以搜索特定文本片段 Aspose.Pdf.Text.TextFragmentAbsorber textFragmentAbsorber = new Aspose.Pdf.Text.TextFragmentAbsorber("Estoque"); //遍歷PDF文檔頁面 for (int i = 1; i <= document.Pages.Count; i++) { //獲取PDF文檔的第一頁 Page page = document.Pages[1]; page.Accept(textFragmentAbsorber); } // 創建吸收文本的集合 Aspose.Pdf.Text.TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments; //迭代上面的集合 for (int j = 1; j <= textFragmentCollection.Count; j++) { Aspose.Pdf.Text.TextFragment textFragment = textFragmentCollection[j]; //獲取TextFragment對象的矩形尺寸 Aspose.Pdf.Rectangle rect = new Aspose.Pdf.Rectangle( (float)textFragment.Position.XIndent, (float)textFragment.Position.YIndent, (float)textFragment.Position.XIndent + (float)textFragment.Rectangle.Width, (float)textFragment.Position.YIndent + (float)textFragment.Rectangle.Height); // 實例化StrikeOut Annotation實例 StrikeOutAnnotation strikeOut = new StrikeOutAnnotation(textFragment.Page, rect); // 為注釋設置不透明度 strikeOut.Opacity = .80f; //設置注釋實例的邊框 strikeOut.Border = new Border(strikeOut); //設置注釋的顏色 strikeOut.Color = Aspose.Pdf.Color.Red; //將注釋添加到TextFragment的注釋集合中 textFragment.Page.Annotations.Add(strikeOut); } dataDir = dataDir + "StrikeOutWords_out.pdf"; document.Save(dataDir);
*想要購買Aspose.PDF for .NET正版授權的朋友可以了解詳情哦~
歡迎加入ASPOSE技術交流QQ群,各類資源及時分享,技術問題交流討論!(掃描下方二維碼加入群聊)