PDF processing control Aspose.PDF function demonstration: add or delete comments in PDF files using C#

PDF annotations are other objects used to mark PDF content. Since PDF files are not easily editable, annotations are used to add notes about the content using annotations, pop-ups, and various other graphical objects.

In this article, you will learn how to use comments in PDF documents programmatically. In particular, this article will introduce how to add and remove comments in PDF files using C#.

  • Add annotations to PDF using C#
  • Remove comments from PDF in C#

For working with PDF documents in .NET applications, Aspose provides Aspose.PDF for .NET . The API allows you to seamlessly create new files as well as process existing PDF files. It also allows you to add and remove PDF comments in a few lines of code.

Click to download the latest version of Aspose.PDF (qun: 761297826) icon-default.png?t=N7T8https://www.evget.com/product/565/download


Add annotations to PDF using C#

PDF format supports various types of annotations such as text, lines, circles, squares, revisions, etc. To work with each PDF annotation, Aspose.PDF for .NET provides separate classes. For example, LineAnnotation class is used to add lines, while HighlightAnnotation class is used to add highlight annotations. Let's take a look at the steps to add any type of annotation in a PDF document.

Steps to add annotations in PDF using C#

  • Load PDF files using the Document class.
  • Create an instance of the required annotation class, either HighlightAnnotation or LineAnnotation.
  • Set the annotation's properties such as position, color, size, etc.
  • Use the Document.Pages[1].Annotations.Add(Annotation) method to add annotations to the Annotations collection of a specific PDF page.
  • Save PDF documents using the Document.Save(String) method.

To demonstrate, the following code example shows how to add line comments to a PDF using C#.

// 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");

Remove comments from PDF in C#

PDF annotations can be removed from existing PDF documents using Aspose.PDF for .NET. You can remove all or specific annotations from PDF. Here are the steps to remove PDF annotations.

  • Load PDF files using the Document class.
  • Use the Document.Pages[index].Annotations.Delete() method to delete all annotations on a specific page.
  • Or use Document.Pages[index].Annotations.Delete(Int index) or Document.Pages[index].Annotations.Delete(Annotation) method to delete a specific annotation.
  • Save the document using the Document.Save(String) method.

The code example below demonstrates how to remove comments from a PDF file using C#.

// 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");

Guess you like

Origin blog.csdn.net/m0_67129275/article/details/132805244