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 work with annotations in PDF files using C++.
- C++ API for Working with Annotations in PDF Files
- Read Annotations in PDF Files
- Add Annotations to PDF Files
- Modify Annotations in PDF Files using C++
- Remove Annotations from PDF Files
- Get a Free License
C++ API for Working with Annotations in PDF Files
Aspose.PDF for C++ is a C++ library that allows you to create, read and update PDF documents. Furthermore, the API supports working with annotations in PDF files. You can either install the API through NuGet or download it directly from the downloads section.
PM> Install-Package Aspose.PDF.Cpp
Read Annotations in PDF Files
The following are the steps to read annotations in PDF files.
- Firstly, create an instance of the PdfAnnotationEditor class.
- Load the PDF file using the PdfAnnotationEditor->BindPdf (System::SharedPtr<Aspose::Pdf::Document> srcDoc) method.
- Retrieve the annotations using the PdfAnnotationEditor->ExtractAnnotations (int32_t start, int32_t end, System::ArrayPtr<Aspose::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++.
Add Annotations to PDF Files
The following are the steps to add annotations to PDF files.
- Firstly, create an instance of the PdfContentEditor class.
- Load the PDF file using the PdfContentEditor->BindPdf (System::SharedPtr<Aspose::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.
Modify Annotations in PDF Files using C++
The following are the steps to modify annotations in PDF files.
- Firstly, create an instance of the PdfAnnotationEditor class.
- Load the PDF file using the PdfAnnotationEditor->BindPdf (System::SharedPtr<Aspose::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::SharedPtr<Aspose::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++.
Remove Annotations from PDF Files
Aspose.PDF for C++ provides the following options for removing the annotations from PDF files.
Remove a Specific 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::SharedPtr<Aspose::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 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::SharedPtr<Aspose::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 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::SharedPtr<Aspose::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++.
Get a Free License
You can try the API without evaluation limitations by requesting a free temporary license.
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 PDF files. Furthermore, you have learned how to delete a specific annotation, annotations by type, or all annotations using Aspose.PDF for C++ API. The API provides a bunch of additional features for working with PDF files. You can explore the API in detail by using the official documentation. In case of any questions, please feel free to reach us on our free support forum.