Update PDF links in C++

These examples modify existing links on page 1. Use a PDF containing link annotations; the URI update applies only to links with GoToURIAction.

Configure the Aspose.PDF for C++ SDK and create the output directory before running examples that save a file. The snippets retain the source includes, function parameters, coordinates, and action settings, with the project runner header omitted.

Set each link annotation on page 1 to red. Despite the filename, the sample changes Color only; it does not change border width or dash settings.

  1. Open the input PDF with Document.
  2. Filter page 1 annotations by AnnotationType.
  3. Cast matching objects to LinkAnnotation.
  4. Set the annotation Color to red.
  5. Save the result with Document::Save().
#include <Aspose.PDF.Cpp/Color.h>
#include <Aspose.PDF.Cpp/Document.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/InteractiveFeatures/Annotations/LinkAnnotation.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;

void LinkAnnotationUpdateBorder(System::String inputFile, System::String outputFile)
{
    auto document = MakeObject<Document>(inputFile);
    for (const auto& annotation : document->get_Pages()->idx_get(1)->get_Annotations()) {
        if (annotation->get_AnnotationType() == AnnotationType::Link && System::DynamicCast_noexcept<LinkAnnotation>(annotation)) {
            auto linkAnnotation = System::DynamicCast<LinkAnnotation>(annotation);
            linkAnnotation->set_Color(Color::get_Red());
        }
    }

    document->Save(outputFile);
}

For each page 1 link, expand its rectangle by 2 units on every side and set matching text fragments to red. The code modifies the annotation rectangle itself and can include nearby text.

  1. Open the input PDF with Document.
  2. Get the rectangle of each Link annotation through Annotation.
  3. Expand its coordinates through Rectangle properties.
  4. Create a TextFragmentAbsorber and restrict it with TextSearchOptions.
  5. Visit page 1 and set each fragment’s TextState foreground color to red.
  6. Save the result with Document::Save().
#include <Aspose.PDF.Cpp/Color.h>
#include <Aspose.PDF.Cpp/Document.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>
#include <Aspose.PDF.Cpp/Rectangle.h>
#include <Aspose.PDF.Cpp/Text/TextFragment.h>
#include <Aspose.PDF.Cpp/Text/TextFragmentState.h>
#include <Aspose.PDF.Cpp/Text/TextFragmentAbsorber.h>
#include <Aspose.PDF.Cpp/Text/TextFragmentCollection.h>
#include <Aspose.PDF.Cpp/Text/TextOptions/TextSearchOptions.h>
#include <Aspose.PDF.Cpp/Text/TextState.h>

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Annotations;
using namespace Aspose::Pdf::Text;

void LinkAnnotationUpdateTextColor(System::String inputFile, System::String outputFile)
{
    auto document = MakeObject<Document>(inputFile);
    for (const auto& annotation : document->get_Pages()->idx_get(1)->get_Annotations()) {
        if (annotation->get_AnnotationType() == AnnotationType::Link) {
            auto absorber = MakeObject<TextFragmentAbsorber>();
            auto rect = annotation->get_Rect();
            rect->set_LLX(rect->get_LLX() - 2);
            rect->set_LLY(rect->get_LLY() - 2);
            rect->set_URX(rect->get_URX() + 2);
            rect->set_URY(rect->get_URY() + 2);
            absorber->set_TextSearchOptions(MakeObject<TextSearchOptions>(rect));
            absorber->Visit(document->get_Pages()->idx_get(1));
            for (const auto& textFragment : absorber->get_TextFragments()) {
                textFragment->get_TextState()->set_ForegroundColor(Color::get_Red());
            }
        }
    }

    document->Save(outputFile);
}

Change existing GoToURIAction targets on page 1 to https://www.aspose.com. Other link action types are skipped.

  1. Open the input PDF with Document.
  2. Filter for LinkAnnotation objects on page 1.
  3. Check each action for GoToURIAction.
  4. Set URI to the replacement URL.
  5. Save the result with Document::Save().
#include <Aspose.PDF.Cpp/Document.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/InteractiveFeatures/Annotations/LinkAnnotation.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/GoToURIAction.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;

void LinkAnnotationUpdateWebDestination(System::String inputFile, System::String outputFile)
{
    auto document = MakeObject<Document>(inputFile);
    for (const auto& annotation : document->get_Pages()->idx_get(1)->get_Annotations()) {
        if (annotation->get_AnnotationType() == AnnotationType::Link && System::DynamicCast_noexcept<LinkAnnotation>(annotation)) {
            auto linkAnnotation = System::DynamicCast<LinkAnnotation>(annotation);
            if (System::DynamicCast_noexcept<GoToURIAction>(linkAnnotation->get_Action())) {
                auto action = System::DynamicCast<GoToURIAction>(linkAnnotation->get_Action());
                action->set_URI(u"https://www.aspose.com");
            }
        }
    }
    document->Save(outputFile);
}