Extract PDF links in C++

These read-only examples inspect the first page and print results. They do not scan all pages or save a modified PDF.

Configure the Aspose.PDF for C++ SDK before running these examples. The snippets retain the source includes, function parameters, coordinates, and action settings, with the project runner header omitted.

Inspect page 1 and print each link annotation’s page index and rectangle. This example does not extract the visible text of the link.

  1. Open the input PDF with Document.
  2. Iterate through the first page Annotations.
  3. Filter by AnnotationType and cast matching objects to LinkAnnotation.
  4. Print the page index and rectangle from each LinkAnnotation.
#include <iostream>
#include <Aspose.PDF.Cpp/Rectangle.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 ExtractLinkAnnotation(System::String inputFile)
{
    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);
            std::cout << "Page: " << linkAnnotation->get_PageIndex()
            << ", location: " << linkAnnotation->get_Rect()->ToString() << std::endl;
        }
    }
}

Extract web destinations

Inspect page 1 links and print only those whose action is GoToURIAction, including each URI and page index.

  1. Open the input PDF with Document.
  2. Filter the first page Annotations for LinkAnnotation objects.
  3. Check and cast the action to GoToURIAction.
  4. Print the URI from get_URI() and the link page index.
#include <iostream>
#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 ExtractHyperlinks(System::String inputFile)
{
    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());
                std::cout << "Page " << linkAnnotation->get_PageIndex() << ", URI:" << action->get_URI() << std::endl;
            }
        }
    }
}