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.

  1. Open the input PDF with Document.
  2. Get the first Page and create a LineAnnotation with the supplied coordinates.
  3. Configure the annotation properties shown in the example.
  4. Add it through AnnotationCollection::Add().
  5. 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

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.

  1. Open the input PDF with Document.
  2. Find text with TextFragmentAbsorber.
  3. Create a LinkAnnotation over the first matching fragment.
  4. Assign a GoToURIAction to the link.
  5. Add the annotation objects with AnnotationCollection::Add().
  6. 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

Find link annotations on the first page and print their bounding rectangles.

  1. Open the input PDF with Document.
  2. Iterate through the first page Annotations collection.
  3. Filter the annotations using AnnotationType.
  4. 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

Collect link annotations on the first page, then delete the collected objects. The collection is not modified during enumeration.

  1. Open the input PDF with Document.
  2. Iterate through the first page Annotations collection and filter by AnnotationType.
  3. Collect the matching annotations before modifying the collection.
  4. Delete each collected annotation with AnnotationCollection::Delete().
  5. 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.

  1. Open the input PDF with Document.
  2. Append a page through PageCollection::Add().
  3. Create each ButtonField with its rectangle, label, and appearance.
  4. Assign a NamedAction for previous or next page navigation.
  5. Register the buttons with Form::Add().
  6. 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.

  1. Create a PDF with Document.
  2. Add a page through PageCollection::Add().
  3. Create a ButtonField and set its label and border.
  4. Assign a NamedAction using File_Print.
  5. Add the field with Form::Add().
  6. 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