Working with Annotations in PDF Files using C++

Annotations are additional objects that can be added to PDF documents. Annotations can be helpful in scenarios such as adding contextual information to the document. As PDF files are not easily editable, annotations provide an option to add additional information to the document. In this article, you will learn how to add, update, and remove annotations in PDF files in C++.

C++ Library to Work with PDF Annotations

Aspose.PDF for C++ is a C++ library that allows you to create, read and update PDF documents. Furthermore, the library supports working with annotations in PDF files. You can either install the library through NuGet or download it directly from the downloads section.

PM> Install-Package Aspose.PDF.Cpp

Add Annotations to PDF in C++

The following are the steps to add annotations to PDF files in C++.

The following is the sample code to add annotations to PDF files using C++.

// Create an instance of the PdfContentEditor class
auto editor = MakeObject<Facades::PdfContentEditor>();
// Load the sample PDF file
editor->BindPdf(u"SourceDirectory\\Sample 1.pdf");
// Create annotation
editor->CreateText(System::Drawing::Rectangle(400, 700, 100, 100), u"Title", u"Welcome to Aspose", true, u"Comment", 1);
// Save PDF file
editor->Save(u"OutputDirectory\\AnnotationSample.pdf");

The following is the image of the file saved by the sample code.

Annotation added to the PDF file

Image showing the annotation

Read Annotations in a PDF in C++

The following are the steps to read annotations in a PDF file in C++.

The following is the sample code to read annotations in PDF files using C++.

// Create an instance of the PdfAnnotationEditor class
System::SharedPtr<Aspose::Pdf::Facades::PdfAnnotationEditor> annotationEditor = System::MakeObject<Aspose::Pdf::Facades::PdfAnnotationEditor>();
// Load the sample PDF file
annotationEditor->BindPdf(u"SourceDirectory\\Sample 1.pdf");
// Create an array of annotation types
System::ArrayPtr<Aspose::Pdf::Annotations::AnnotationType> annotationTypes = System::MakeArray<Aspose::Pdf::Annotations::AnnotationType>({ Aspose::Pdf::Annotations::AnnotationType::Text, Aspose::Pdf::Annotations::AnnotationType::Highlight });
// Extract Annotations
System::SharedPtr<System::Collections::Generic::IList<System::SharedPtr<Aspose::Pdf::Annotations::Annotation>>> annotationList = annotationEditor->ExtractAnnotations(1, 2, annotationTypes);
// Loop through the annotations
for (System::SharedPtr<Aspose::Pdf::Annotations::Annotation> annotation : annotationList)
{
// Display annotation content
Console::WriteLine(annotation->get_Contents());
}

Edit Annotations in PDF in C++

The following are the steps to edit and update annotations in PDF files in C++.

The following is the sample code to modify annotations in PDF files using C++.

// Create an instance of the sample PDF file
auto document = MakeObject <Aspose::Pdf::Document>(u"SourceDirectory\\AnnotationSample.pdf");
// Create an instance of the PdfAnnotationEditor class
System::SharedPtr<Aspose::Pdf::Facades::PdfAnnotationEditor> annotationEditor = System::MakeObject<Aspose::Pdf::Facades::PdfAnnotationEditor>();
// Load the sample PDF document
annotationEditor->BindPdf(document);
// Create an annotation object
System::SharedPtr<Aspose::Pdf::Annotations::TextAnnotation> annotation = System::MakeObject<Aspose::Pdf::Annotations::TextAnnotation>(document->get_Pages()->idx_get(1), MakeObject<Aspose::Pdf::Rectangle>(200, 400, 400, 600));
// Set modified date
annotation->set_Modified(System::DateTime::get_Now());
// Set Title
annotation->set_Title(u"NEW AUTHOR");
// Set Content
annotation->set_Contents(u"NEW CONTENTS");
// Set Subject
annotation->set_Subject(u"NEW SUBJECT");
// Set open flag
annotation->set_Open(true);
// Modify Annotation
annotationEditor->ModifyAnnotations(1, 1, annotation);
// Save the document
annotationEditor->Save(u"OutputDirectory\\AnnotationSample_out.pdf");

C++ Remove PDF Annotations

Aspose.PDF for C++ provides the following options for removing the annotations from PDF files.

Remove a Specific PDF Annotation

The following are the steps to remove a specific annotation from a PDF file.

The following is the sample code to remove a specific annotation from a PDF file using C++.

// Create an instance of the PdfAnnotationEditor class
System::SharedPtr<Aspose::Pdf::Facades::PdfAnnotationEditor> annotationEditor = System::MakeObject<Aspose::Pdf::Facades::PdfAnnotationEditor>();
// Load an existing PDF document
annotationEditor->BindPdf(u"SourceDirectory\\AnnotationSample.pdf");
// Delete Annotation By Name
annotationEditor->DeleteAnnotation(u"4df2cf36-d961-4d83-a39e-4b0069f97e0b");
// Save the document
annotationEditor->Save(u"OutputDirectory\\AnnotationSample_out.pdf");

Removing PDF Annotations by Type

The following are the steps to remove annotations by type from PDF files.

The following is the sample code to remove annotations by type from PDF files using C++.

// Create an instance of the PdfAnnotationEditor class
System::SharedPtr<Aspose::Pdf::Facades::PdfAnnotationEditor> annotationEditor = System::MakeObject<Aspose::Pdf::Facades::PdfAnnotationEditor>();
// Load an existing PDF document
annotationEditor->BindPdf(u"SourceDirectory\\AnnotationSample.pdf");
// Delete Text Annotations
annotationEditor->DeleteAnnotations(u"Text");
// Save the document
annotationEditor->Save(u"OutputDirectory\\AnnotationSample_out.pdf");

Remove All PDF Annotations

The following are the steps to remove all annotations from PDF files.

The following is the sample code to remove all annotations from PDF files using C++.

// Create an instance of the PdfAnnotationEditor class
System::SharedPtr<Aspose::Pdf::Facades::PdfAnnotationEditor> annotationEditor = System::MakeObject<Aspose::Pdf::Facades::PdfAnnotationEditor>();
// Load an existing PDF document
annotationEditor->BindPdf(u"SourceDirectory\\AnnotationSample.pdf");
// Delete All Annotations
annotationEditor->DeleteAnnotations();
// Save the document
annotationEditor->Save(u"OutputDirectory\\AnnotationSample_out.pdf");

Free C++ Library to Add PDF Annotations

You can use Aspose.PDF for C++ without evaluation limitations by getting a free temporary license.

C++ PDF Library

The library provides a bunch of additional features for working with PDF files. You can explore the library in detail by using the official documentation. In case of any questions, please feel free to reach us on our free support forum.

Conclusion

In this article, you have learned how to work with annotations in PDF files using C++. Specifically, you have seen how to read, add, edit and delete annotations from a PDF in C++. Furthermore, you have learned how to delete a specific annotation, annotations by type, or all annotations using Aspose.PDF for C++.

See Also