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++ API Add, Update, and Remove Annotations in PDF
- Read Annotations in PDF in C++
- Add Annotations to PDF in C++
- Edit Annotations in PDF in C++
- Remove Annotations from PDF in C++
- Get a Free License
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++.
- Firstly, create an instance of the PdfContentEditor class.
- Load the PDF file using the PdfContentEditor->BindPdf (System::SharedPtrAspose::Pdf::Document srcDoc) method.
- Create the annotation using the PdfContentEditor->CreateText (System::Drawing::Rectangle rect, System::String title, System::String contents, bool open, System::String icon, int32_t page) method.
- Finally, save the PDF using the PdfContentEditor->Save (System::String destFile) method.
The following is the sample code to add annotations to PDF files using C++.
The following is the image of the file saved by the sample code.
Read Annotations in a PDF in C++
The following are the steps to read annotations in a PDF file in C++.
- Firstly, create an instance of the PdfAnnotationEditor class.
- Load the PDF file using the PdfAnnotationEditor->BindPdf (System::SharedPtrAspose::Pdf::Document srcDoc) method.
- Retrieve the annotations using the PdfAnnotationEditor->ExtractAnnotations (int32_t start, int32_t end, System::ArrayPtrAspose::Pdf::Annotations::AnnotationType annotTypes) method.
- Loop through the annotations and print their content.
The following is the sample code to read annotations in PDF files using C++.
Edit Annotations in PDF in C++
The following are the steps to edit and update annotations in PDF files in C++.
- Firstly, create an instance of the PdfAnnotationEditor class.
- Load the PDF file using the PdfAnnotationEditor->BindPdf (System::SharedPtrAspose::Pdf::Document srcDoc) method.
- Create an instance of the TextAnnotation class.
- Set the content of the annotation.
- Use the PdfAnnotationEditor->ModifyAnnotations (int32_t start, int32_t end, System::SharedPtrAspose::Pdf::Annotations::Annotation annotation) method to modify the annotation.
- Finally, save the updated PDF file using the PdfAnnotationEditor->Save (System::String destFile) method.
The following is the sample code to modify annotations in PDF files using C++.
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.
- Firstly, create an instance of the PdfAnnotationEditor class.
- Load the PDF file using the PdfAnnotationEditor->BindPdf (System::SharedPtrAspose::Pdf::Document srcDoc) method.
- Delete the annotation by name using the PdfAnnotationEditor->DeleteAnnotation (System::String annotName) method.
- Finally, save the updated PDF file using the PdfAnnotationEditor->Save (System::String destFile) method.
The following is the sample code to remove a specific annotation from a PDF file using C++.
Removing PDF Annotations by Type
The following are the steps to remove annotations by type from PDF files.
- Firstly, create an instance of the PdfAnnotationEditor class.
- Load the PDF file using the PdfAnnotationEditor->BindPdf (System::SharedPtrAspose::Pdf::Document srcDoc) method.
- Delete the annotation by type using the PdfAnnotationEditor->DeleteAnnotations (System::String annotType) method.
- Finally, save the updated PDF file using the PdfAnnotationEditor->Save (System::String destFile) method.
The following is the sample code to remove annotations by type from PDF files using C++.
Remove All PDF Annotations
The following are the steps to remove all annotations from PDF files.
- Firstly, create an instance of the PdfAnnotationEditor class.
- Load the PDF file using the PdfAnnotationEditor->BindPdf (System::SharedPtrAspose::Pdf::Document srcDoc) method.
- Delete all the annotations using the PdfAnnotationEditor->DeleteAnnotations() method.
- Finally, save the updated PDF file using the PdfAnnotationEditor->Save (System::String destFile) method.
The following is the sample code to remove all annotations from PDF files using C++.
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++.