PDF批注是用于标记PDF内容的其他对象。由于PDF文件不容易编辑,因此可使用注释使用注释,弹出窗口和各种其他图形对象来添加有关内容的注释。
在本文中,将学习如何以编程方式使用PDF文档中的注释。特别是,本文将介绍如何使用C#在PDF文件中添加和删除注释。
- 使用C#将注释添加到PDF
- 在C#中从PDF删除注释
为了在.NET应用程序中使用PDF文档,Aspose提供了.NET的Aspose.PDF。该API可让您无缝创建新文件以及处理现有PDF文件。它还允许您在几行代码中添加和删除PDF注释。
点击下载最新版Aspose.PDF(qun:761297826)https://www.evget.com/product/565/download
使用C#将注释添加到PDF
PDF格式支持各种类型的注释,例如文本,线条,圆形,正方形,修订等。为了与每个PDF注释一起使用,Aspose.PDF for .NET提供了单独的类。例如,LineAnnotation类用于添加行,而HighlightAnnotation类用于添加突出显示注释。让我们看一下在PDF文档中添加任何类型的注释的步骤。
使用C#在PDF中添加注释的步骤
- 使用Document类加载PDF文件。
- 创建所需注释类的实例,即HighlightAnnotation或LineAnnotation。
- 设置注释的属性,例如位置,颜色,大小等。
- 使用Document.Pages [1] .Annotations.Add(Annotation)方法将注释添加到特定PDF页面的Annotations集合中。
- 使用Document.Save(String)方法保存PDF文档。
为了演示,下面的代码示例演示如何使用C#将行注释添加到PDF。
// Load the PDF file Document document = new Document("Input.pdf"); // Create Line Annotation var lineAnnotation = new LineAnnotation( document.Pages[1], new Rectangle(550, 93, 562, 439), new Point(556, 99), new Point(556, 443)) { Title = "John Smith", Color = Color.Red, Width = 3, StartingStyle = LineEnding.OpenArrow, EndingStyle = LineEnding.OpenArrow, Popup = new PopupAnnotation(document.Pages[1], new Rectangle(842, 124, 1021, 266)) }; // Add annotation to the page document.Pages[1].Annotations.Add(lineAnnotation); // Save PDF document.Save("output.pdf");
在C#中从PDF删除注释
可以使用Aspose.PDF for .NET从现有PDF文档中删除PDF批注。您可以从PDF删除全部或特定注释。以下是删除PDF注释的步骤。
- 使用Document类加载PDF文件。
- 使用Document.Pages [index] .Annotations.Delete()方法删除特定页面上的所有注释。
- 或使用Document.Pages [index] .Annotations.Delete(Int index)或Document.Pages [index] .Annotations.Delete(Annotation)方法删除特定的注释。
- 使用Document.Save(String)方法保存文档。
下面的代码示例演示如何使用C#从PDF文件中删除注释。
// Open document Document pdfDocument = new Document("DeleteAnnotationsFromPage.pdf"); // Delete particular annotation pdfDocument.Pages[1].Annotations.Delete(); // Or delete particular annotation // pdfDocument.Pages[1].Annotations.Delete(1); // Save updated document pdfDocument.Save("output.pdf");