Manage PDF annotations in C++
Use PdfAnnotationEditor to delete, extract, flatten, modify, and transfer annotations and exchange annotation data through XFDF.
Annotation import examples create blank target pages before importing annotations; they do not copy the original page content. The flattening example enables ApplyRedactions.
The snippets are independent example functions with their SDK includes and example namespaces. Configure the Aspose.PDF for C++ SDK, supply the input resources referenced by the sample, and choose writable output paths. The examples retain the source field names, page indexes, coordinates, and options.
PdfAnnotationEditor workflows
- Delete all annotations by type
- Delete all annotations
- Delete annotation by name
- Extract annotations
- Flatten annotations
- Import annotations
- Import export all annotations XFDF
- Import export selected annotations XFDF
- Modify annotations author
- Modify annotations
Delete all annotations by type
Delete Text and FreeText annotations from the bound PDF.
- Create PdfAnnotationEditor.
- Bind the PDF input with Facade::BindPdf().
- Delete the selected annotations with PdfAnnotationEditor::DeleteAnnotations().
- Save the resulting PDF with PdfAnnotationEditor::
Save().
#include <Aspose.PDF.Cpp/Facades/PdfAnnotationEditor.h>
#include <system/string.h>
namespace Facades::Examples::PDF_AnnotationEditor
{
using namespace System;
using namespace Aspose::Pdf;
using Aspose::Pdf::Facades::PdfAnnotationEditor;
void DeleteAllAnnotationsByType(String inputFile, String outputFile)
{
auto editor = MakeObject<PdfAnnotationEditor>();
editor->BindPdf(inputFile);
editor->DeleteAnnotations(u"Text");
editor->DeleteAnnotations(u"FreeText");
editor->Save(outputFile);
}
} // namespace Facades::Examples::PDF_AnnotationEditor
Delete all annotations
Delete all annotations from the bound PDF.
- Create PdfAnnotationEditor.
- Bind the PDF input with Facade::BindPdf().
- Delete the selected annotations with PdfAnnotationEditor::DeleteAnnotations().
- Save the resulting PDF with PdfAnnotationEditor::
Save().
#include <Aspose.PDF.Cpp/Facades/PdfAnnotationEditor.h>
#include <system/string.h>
namespace Facades::Examples::PDF_AnnotationEditor
{
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;
void DeleteAllAnnotations(String inputFile, String outputFile)
{
auto editor = MakeObject<PdfAnnotationEditor>();
editor->BindPdf(inputFile);
editor->DeleteAnnotations();
editor->Save(outputFile);
}
} // namespace Facades::Examples::PDF_AnnotationEditor
Delete annotation by name
Delete the annotation named sample-note-1.
- Create PdfAnnotationEditor.
- Bind the PDF input with Facade::BindPdf().
- Delete the named annotation with PdfAnnotationEditor::DeleteAnnotation().
- Save the resulting PDF with PdfAnnotationEditor::
Save().
#include <Aspose.PDF.Cpp/Facades/PdfAnnotationEditor.h>
#include <system/string.h>
namespace Facades::Examples::PDF_AnnotationEditor
{
using namespace System;
using namespace Aspose::Pdf;
using Aspose::Pdf::Facades::PdfAnnotationEditor;
void DeleteAnnotationByName(String inputFile, String outputFile)
{
auto editor = MakeObject<PdfAnnotationEditor>();
editor->BindPdf(inputFile);
editor->DeleteAnnotation(u"sample-note-1");
editor->Save(outputFile);
}
} // namespace Facades::Examples::PDF_AnnotationEditor
Extract annotations
Extract Text and FreeText annotation contents across all pages and write them to a text file.
- Create PdfAnnotationEditor.
- Bind the PDF input with Facade::BindPdf().
- Extract the selected annotation types and page range with PdfAnnotationEditor::ExtractAnnotations().
- Write the collected values to the output text file.
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Facades/PdfAnnotationEditor.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/Annotation.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/AnnotationType.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <system/array.h>
#include <system/io/file.h>
#include <system/io/file_stream.h>
#include <system/string.h>
namespace Facades::Examples::PDF_AnnotationEditor
{
using namespace System;
using namespace Aspose::Pdf;
using Aspose::Pdf::Facades::PdfAnnotationEditor;
void ExtractAnnotations(String inputFile, String outputFile)
{
auto types = MakeArray<Annotations::AnnotationType>(
{Annotations::AnnotationType::FreeText, Annotations::AnnotationType::Text});
auto editor = MakeObject<PdfAnnotationEditor>();
editor->BindPdf(inputFile);
String contents;
for (auto annotation : editor->ExtractAnnotations(1, editor->get_Document()->get_Pages()->get_Count(), types))
contents += annotation->get_Contents() + u"\n";
System::IO::File::WriteAllText(outputFile, contents);
}
} // namespace Facades::Examples::PDF_AnnotationEditor
Flatten annotations
Flatten annotations with redaction application enabled, events disabled, buttons hidden, and appearances updated.
- Configure Form::FlattenSettings for redaction, events, buttons, and appearance handling.
- Create PdfAnnotationEditor.
- Bind the PDF input with Facade::BindPdf().
- Apply the flattening settings with PdfAnnotationEditor::FlatteningAnnotations().
- Save the resulting PDF with PdfAnnotationEditor::
Save().
#include <Aspose.PDF.Cpp/Facades/PdfAnnotationEditor.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Forms/Form.h>
#include <system/string.h>
namespace Facades::Examples::PDF_AnnotationEditor
{
using namespace System;
using namespace Aspose::Pdf;
using Aspose::Pdf::Facades::PdfAnnotationEditor;
void FlattenAnnotations(String inputFile, String outputFile)
{
auto editor = MakeObject<PdfAnnotationEditor>();
editor->BindPdf(inputFile);
auto settings = MakeObject<Forms::Form::FlattenSettings>();
settings->set_ApplyRedactions(true);
settings->set_CallEvents(false);
settings->set_HideButtons(true);
settings->set_UpdateAppearances(true);
editor->FlatteningAnnotations(settings);
editor->Save(outputFile);
}
} // namespace Facades::Examples::PDF_AnnotationEditor
Import annotations
Create a blank destination with the same page count as the source and import its annotations.
- Create PdfAnnotationEditor.
- Create or open the Document and prepare its pages as shown.
- Bind the PDF input with Facade::BindPdf().
- Import annotations into the target PDF with PdfAnnotationEditor::ImportAnnotations().
- Save the resulting PDF with PdfAnnotationEditor::
Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Facades/PdfAnnotationEditor.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <system/array.h>
#include <system/string.h>
namespace Facades::Examples::PDF_AnnotationEditor
{
using namespace System;
using namespace Aspose::Pdf;
using Aspose::Pdf::Facades::PdfAnnotationEditor;
void ImportAnnotations(String inputFile, String outputFile)
{
auto source = MakeObject<Document>(inputFile);
auto target = MakeObject<Document>();
for (int i = 0; i < source->get_Pages()->get_Count(); ++i)
target->get_Pages()->Add();
auto editor = MakeObject<PdfAnnotationEditor>();
editor->BindPdf(target);
editor->ImportAnnotations(MakeArray<String>({inputFile}));
editor->Save(outputFile);
}
} // namespace Facades::Examples::PDF_AnnotationEditor
Import export all annotations XFDF
Export annotations to outputFile.xfdf, then import that file into a blank document with matching page count.
- Create PdfAnnotationEditor.
- Create or open the Document and prepare its pages as shown.
- Bind the PDF input with Facade::BindPdf().
- Export all annotations with PdfAnnotationEditor::ExportAnnotationsToXfdf().
- Import the XFDF annotations with PdfAnnotationEditor::ImportAnnotationsFromXfdf().
- Save the resulting PDF with PdfAnnotationEditor::
Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Facades/PdfAnnotationEditor.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <system/io/file.h>
#include <system/io/file_stream.h>
#include <system/string.h>
namespace Facades::Examples::PDF_AnnotationEditor
{
using namespace System;
using namespace Aspose::Pdf;
using Aspose::Pdf::Facades::PdfAnnotationEditor;
void ImportExportAllAnnotationsXfdf(String inputFile, String outputFile)
{
String xfdfFile = outputFile + u".xfdf";
auto editor = MakeObject<PdfAnnotationEditor>();
editor->BindPdf(inputFile);
int count = editor->get_Document()->get_Pages()->get_Count();
auto stream = System::IO::File::Create(xfdfFile);
editor->ExportAnnotationsToXfdf(stream);
stream->Close();
auto target = MakeObject<Document>();
for (int page = 0; page < count; ++page)
{
target->get_Pages()->Add();
}
auto importer = MakeObject<PdfAnnotationEditor>();
importer->BindPdf(target);
importer->ImportAnnotationsFromXfdf(xfdfFile);
importer->Save(outputFile);
}
} // namespace Facades::Examples::PDF_AnnotationEditor
Import export selected annotations XFDF
Export only Text and FreeText annotations across the source page range, then import the XFDF into blank target pages.
- Create PdfAnnotationEditor.
- Create or open the Document and prepare its pages as shown.
- Bind the PDF input with Facade::BindPdf().
- Export the selected annotation types with PdfAnnotationEditor::ExportAnnotationsXfdf().
- Import the XFDF annotations with PdfAnnotationEditor::ImportAnnotationsFromXfdf().
- Save the resulting PDF with PdfAnnotationEditor::
Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Facades/PdfAnnotationEditor.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/AnnotationType.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <system/array.h>
#include <system/io/file.h>
#include <system/io/file_stream.h>
#include <system/string.h>
namespace Facades::Examples::PDF_AnnotationEditor
{
using namespace System;
using namespace Aspose::Pdf;
using Aspose::Pdf::Facades::PdfAnnotationEditor;
void ImportExportSelectedAnnotationsXfdf(String inputFile, String outputFile)
{
String xfdfFile = outputFile + u".xfdf";
auto editor = MakeObject<PdfAnnotationEditor>();
editor->BindPdf(inputFile);
int count = editor->get_Document()->get_Pages()->get_Count();
auto stream = System::IO::File::Create(xfdfFile);
auto types = MakeArray<Annotations::AnnotationType>(
{Annotations::AnnotationType::FreeText, Annotations::AnnotationType::Text});
editor->ExportAnnotationsXfdf(stream, 1, count, types);
stream->Close();
auto target = MakeObject<Document>();
for (int page = 0; page < count; ++page)
{
target->get_Pages()->Add();
}
auto importer = MakeObject<PdfAnnotationEditor>();
importer->BindPdf(target);
importer->ImportAnnotationsFromXfdf(xfdfFile);
importer->Save(outputFile);
}
} // namespace Facades::Examples::PDF_AnnotationEditor
Modify annotations author
Replace the author Aspose User with Aspose.PDF user across the document page range.
- Create PdfAnnotationEditor.
- Bind the PDF input with Facade::BindPdf().
- Replace matching annotation authors with PdfAnnotationEditor::ModifyAnnotationsAuthor().
- Save the resulting PDF with PdfAnnotationEditor::
Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Facades/PdfAnnotationEditor.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <system/string.h>
namespace Facades::Examples::PDF_AnnotationEditor
{
using namespace System;
using namespace Aspose::Pdf;
using Aspose::Pdf::Facades::PdfAnnotationEditor;
void ModifyAnnotationsAuthor(String inputFile, String outputFile)
{
auto editor = MakeObject<PdfAnnotationEditor>();
editor->BindPdf(inputFile);
editor->ModifyAnnotationsAuthor(1, editor->get_Document()->get_Pages()->get_Count(), u"Aspose User",
u"Aspose.PDF user");
editor->Save(outputFile);
}
} // namespace Facades::Examples::PDF_AnnotationEditor
Modify annotations
Use a TextAnnotation template to update annotation title, subject, and contents on page 1.
- Create PdfAnnotationEditor.
- Bind the PDF input with Facade::BindPdf().
- Apply the annotation template with PdfAnnotationEditor::ModifyAnnotations().
- Save the resulting PDF with PdfAnnotationEditor::
Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Facades/PdfAnnotationEditor.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/TextAnnotation.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Rectangle.h>
#include <system/string.h>
namespace Facades::Examples::PDF_AnnotationEditor
{
using namespace System;
using namespace Aspose::Pdf;
using Aspose::Pdf::Facades::PdfAnnotationEditor;
void ModifyAnnotations(String inputFile, String outputFile)
{
auto editor = MakeObject<PdfAnnotationEditor>();
editor->BindPdf(inputFile);
auto annotation = MakeObject<Annotations::TextAnnotation>(editor->get_Document()->get_Pages()->idx_get(1),
MakeObject<Rectangle>(200, 400, 400, 600));
annotation->set_Title(u"Updated title");
annotation->set_Subject(u"Updated subject");
annotation->set_Contents(u"Updated sample contents for the annotation");
editor->ModifyAnnotations(1, 1, annotation);
editor->Save(outputFile);
}
} // namespace Facades::Examples::PDF_AnnotationEditor