Interactive annotations in C++
These examples create URI links, a line with a popup, page navigation buttons, and a print button. Activating links and button actions requires a PDF viewer that supports the corresponding actions.
The snippets retain the example namespaces and include files. They assume the Aspose.PDF for C++ SDK is configured. Functions that open an existing PDF generally use its first page; supply a PDF containing the expected text, annotations, or media resources. Coordinates are taken from the supplied fixtures and should be adjusted for other documents.
Add line annotations
Create a line annotation on page 1, configure its title, color, starting style, ending style, width, border, popup, and save the updated PDF.
- Open the input PDF with Document.
- Get the first Page and create a LineAnnotation with the supplied coordinates.
- Configure the annotation properties shown in the example.
- Add it through AnnotationCollection::Add().
- Save the result with Document::Save().
#include <system/exceptions.h>
#include <Aspose.PDF.Cpp/Color.h>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/AnnotationCollection.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/Border.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/LineAnnotation.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/LineEnding.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/PopupAnnotation.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Point.h>
#include <Aspose.PDF.Cpp/Rectangle.h>
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Annotations;
using namespace Aspose::Pdf::Text;
namespace WorkingWithAnnotationsExamples::Interactive {
void LineAnnotationAdd(System::String inputFile, System::String outputFile)
{
auto document = MakeObject<Document>(inputFile);
auto lineAnnotation = MakeObject<LineAnnotation>(
document->get_Pages()->idx_get(1),
MakeObject<Rectangle>(550, 93, 562, 439, true),
MakeObject<Point>(556, 99),
MakeObject<Point>(556, 443));
lineAnnotation->set_Title(u"John Smith");
lineAnnotation->set_Color(Color::get_Red());
lineAnnotation->set_StartingStyle(LineEnding::OpenArrow);
lineAnnotation->set_EndingStyle(LineEnding::OpenArrow);
auto border = MakeObject<Border>(lineAnnotation);
border->set_Width(3);
lineAnnotation->set_Border(border);
auto popup = MakeObject<PopupAnnotation>(
document->get_Pages()->idx_get(1),
MakeObject<Rectangle>(842, 124, 1021, 266, true));
lineAnnotation->set_Popup(popup);
document->get_Pages()->idx_get(1)->get_Annotations()->Add(lineAnnotation);
document->Save(outputFile);
}
} // namespace WorkingWithAnnotationsExamples::Interactive
Add a URI link
Search for the first occurrence of “file” on page 1 and place a link to https://www.aspose.com over its rectangle. The source page must contain that text.
- Open the input PDF with Document.
- Find text with TextFragmentAbsorber.
- Create a LinkAnnotation over the first matching fragment.
- Assign a GoToURIAction to the link.
- Add the annotation objects with AnnotationCollection::Add().
- Save the result with Document::Save().
#include <system/exceptions.h>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/AnnotationCollection.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/LinkAnnotation.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/GoToURIAction.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Text/TextFragment.h>
#include <Aspose.PDF.Cpp/Text/TextFragmentAbsorber.h>
#include <Aspose.PDF.Cpp/Text/TextFragmentCollection.h>
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Annotations;
using namespace Aspose::Pdf::Text;
namespace WorkingWithAnnotationsExamples::Interactive {
void LinkAdd(System::String inputFile, System::String outputFile)
{
auto document = MakeObject<Document>(inputFile);
auto textFragmentAbsorber = MakeObject<TextFragmentAbsorber>(u"file");
document->get_Pages()->idx_get(1)->Accept(textFragmentAbsorber);
auto phoneNumberFragment = textFragmentAbsorber->get_TextFragments()->idx_get(1);
auto linkAnnotation = MakeObject<LinkAnnotation>(
document->get_Pages()->idx_get(1),
phoneNumberFragment->get_Rectangle());
linkAnnotation->set_Action(MakeObject<GoToURIAction>(u"https://www.aspose.com"));
document->get_Pages()->idx_get(1)->get_Annotations()->Add(linkAnnotation);
document->Save(outputFile);
}
} // namespace WorkingWithAnnotations::Interactive
Get link annotations
Find link annotations on the first page and print their bounding rectangles.
- Open the input PDF with Document.
- Iterate through the first page Annotations collection.
- Filter the annotations using AnnotationType.
- Read the matching Annotation rectangles and print them.
#include <iostream>
#include <system/exceptions.h>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Point.h>
#include <Aspose.PDF.Cpp/Rectangle.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/AnnotationCollection.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/AnnotationType.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Annotations;
using namespace Aspose::Pdf::Text;
namespace WorkingWithAnnotationsExamples::Interactive {
void LinkGet(System::String inputFile)
{
auto document = MakeObject<Document>(inputFile);
for (auto& annotation : document->get_Pages()->idx_get(1)->get_Annotations()) {
if (annotation->get_AnnotationType() == AnnotationType::Link) {
std::cout << annotation->get_Rect()->ToString() << std::endl;
}
}
}
} // namespace WorkingWithAnnotations::Interactive
Delete link annotations
Collect link annotations on the first page, then delete the collected objects. The collection is not modified during enumeration.
- Open the input PDF with Document.
- Iterate through the first page Annotations collection and filter by AnnotationType.
- Collect the matching annotations before modifying the collection.
- Delete each collected annotation with AnnotationCollection::Delete().
- Save the result with Document::Save().
#include <vector>
#include <system/exceptions.h>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Point.h>
#include <Aspose.PDF.Cpp/Rectangle.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/Annotation.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/AnnotationCollection.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/AnnotationType.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Annotations;
using namespace Aspose::Pdf::Text;
namespace WorkingWithAnnotationsExamples::Interactive {
void LinkDelete(System::String inputFile, System::String outputFile)
{
auto document = MakeObject<Document>(inputFile);
std::vector<SharedPtr<Annotation>> toDelete;
for (auto& annotation : document->get_Pages()->idx_get(1)->get_Annotations()) {
if (annotation->get_AnnotationType() == AnnotationType::Link) {
toDelete.push_back(annotation);
}
}
for (auto& annotation : toDelete) {
document->get_Pages()->idx_get(1)->get_Annotations()->Delete(annotation);
}
document->Save(outputFile);
}
} // namespace WorkingWithAnnotations::Interactive
Add page navigation buttons
Append a page, then add Previous Page and Next Page buttons to every page. Each button runs a predefined named action.
- Open the input PDF with Document.
- Append a page through PageCollection::Add().
- Create each ButtonField with its rectangle, label, and appearance.
- Assign a NamedAction for previous or next page navigation.
- Register the buttons with Form::Add().
- Save the result with Document::Save().
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/AnnotationActionCollection.h>
#include <system/exceptions.h>
#include <Aspose.PDF.Cpp/Color.h>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Forms/Form.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/AnnotationCollection.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/Characteristics.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Forms/ButtonField.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Forms/Field.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/NamedAction.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/PredefinedAction.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Rectangle.h>
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Annotations;
using namespace Aspose::Pdf::Text;
using namespace Aspose::Pdf::Forms;
namespace WorkingWithAnnotationsExamples::Interactive {
void NavigationButtonsAdd(System::String inputFile, System::String outputFile)
{
auto document = MakeObject<Document>(inputFile);
document->get_Pages()->Add();
struct ButtonConfig { String name; double xPos; PredefinedAction action; };
const ButtonConfig buttonConfigs[] = {
{u"Previous Page", 120.0, PredefinedAction::PrevPage},
{u"Next Page", 230.0, PredefinedAction::NextPage}
};
for (auto& page : document->get_Pages()) {
for (const auto& config : buttonConfigs) {
auto rect = MakeObject<Rectangle>(config.xPos, 10.0, config.xPos + 100, 40.0, true);
auto button = MakeObject<ButtonField>(page, rect);
button->set_PartialName(config.name);
button->set_Value(config.name);
button->get_Characteristics()->set_Border(Color::get_Red()->ToRgb());
button->get_Characteristics()->set_Background(Color::get_Orange()->ToRgb());
button->get_Actions()->set_OnReleaseMouseBtn(MakeObject<NamedAction>(config.action));
document->get_Form()->Add(StaticCast<Field>(button));
}
}
document->Save(outputFile);
}
} // namespace WorkingWithAnnotations::Interactive
Add a print button
Create a new one-page PDF with a button that requests the viewer print action when released.
- Create a PDF with Document.
- Add a page through PageCollection::Add().
- Create a ButtonField and set its label and border.
- Assign a NamedAction using File_Print.
- Add the field with Form::Add().
- Save the result with Document::Save().
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/AnnotationActionCollection.h>
#include <system/exceptions.h>
#include <Aspose.PDF.Cpp/Color.h>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Point.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Forms/Form.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/AnnotationCollection.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/Border.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/BorderStyle.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/Characteristics.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Forms/ButtonField.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Forms/Field.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/NamedAction.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/PredefinedAction.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Rectangle.h>
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Annotations;
using namespace Aspose::Pdf::Text;
using namespace Aspose::Pdf::Forms;
namespace WorkingWithAnnotationsExamples::Interactive {
void PrintButtonAdd(System::String outputFile)
{
auto document = MakeObject<Document>();
auto page = document->get_Pages()->Add();
auto rect = MakeObject<Rectangle>(72, 748, 164, 768, true);
auto printButton = MakeObject<ButtonField>(page, rect);
printButton->set_AlternateName(u"Print current document");
printButton->set_Color(Color::get_Black());
printButton->set_PartialName(u"printBtn1");
printButton->set_Value(u"Print Document");
printButton->get_Actions()->set_OnReleaseMouseBtn(
MakeObject<NamedAction>(PredefinedAction::File_Print));
auto border = MakeObject<Border>(printButton);
border->set_Style(BorderStyle::Solid);
border->set_Width(2);
printButton->set_Border(border);
printButton->get_Characteristics()->set_Border(Color::get_Blue()->ToRgb());
printButton->get_Characteristics()->set_Background(Color::get_LightBlue()->ToRgb());
document->get_Form()->Add(StaticCast<Field>(printButton));
document->Save(outputFile);
}
} // namespace WorkingWithAnnotations::Interactive