Create PDF links in C++

Each example creates a link annotation on page 1. The input PDF must contain at least one page. The internal link targets page 4 when available and otherwise the last page; external targets and URLs are preserved from the supplied source.

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.

Add a green link on page 1 with a dashed border of width 5. Its destination is page 4 when available, otherwise the last page.

  1. Open the input PDF with Document.
  2. Create a LinkAnnotation on page 1 with the sample rectangle.
  3. Configure its Border, Dash, and color.
  4. Assign a GoToAction targeting page 4 or the last available page.
  5. Add it with AnnotationCollection::Add().
  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/InteractiveFeatures/Annotations/Border.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/Dash.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/LinkAnnotation.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/GoToAction.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;

void CreateLinkAnnotationGoToAction(System::String inputFile, System::String outputFile)
{
    auto document = MakeObject<Document>(inputFile);
    auto page = document->get_Pages()->idx_get(1);

    auto link = MakeObject<LinkAnnotation>(page, MakeObject<Rectangle>(10, 580, 120, 600, true));
    auto border = MakeObject<Border>(link);
    border->set_Width(5);
    border->set_Dash(MakeObject<Dash>(1, 1));
    link->set_Border(border);
    link->set_Color(Color::get_Green());
    if (document->get_Pages()->get_Count() >= 4) {
        link->set_Action(MakeObject<GoToAction>(document->get_Pages()->idx_get(4)));
    } else {
        link->set_Action(MakeObject<GoToAction>(document->get_Pages()->idx_get(document->get_Pages()->get_Count())));
    }
    page->get_Annotations()->Add(link);
    document->Save(outputFile);
}

Create a green link whose GoToRemoteAction targets page 1 of the file named by inputFile. The source intentionally reuses the input path as the external target.

  1. Open the input PDF with Document.
  2. Create a LinkAnnotation on page 1.
  3. Set its color and assign a GoToRemoteAction using inputFile and page 1.
  4. Add it with AnnotationCollection::Add().
  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/InteractiveFeatures/GoToRemoteAction.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;

void CreateLinkAnnotationGoToRemoteAction(System::String inputFile, System::String outputFile)
{
    auto document = MakeObject<Document>(inputFile);
    auto page = document->get_Pages()->idx_get(1);

    auto link = MakeObject<LinkAnnotation>(page, MakeObject<Rectangle>(10, 580, 120, 600, true));
    link->set_Color(Color::get_Green());
    link->set_Action(MakeObject<GoToRemoteAction>(inputFile, 1));
    page->get_Annotations()->Add(link);
    document->Save(outputFile);
}

Add a green link on page 1 targeting https://docs.aspose.com/pdf/cpp/. The source URL is preserved.

  1. Open the input PDF with Document.
  2. Create a LinkAnnotation with the sample rectangle.
  3. Assign a GoToURIAction with the source URL.
  4. Add it through AnnotationCollection::Add().
  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/InteractiveFeatures/GoToURIAction.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;

void CreateLinkAnnotationGoToUriAction(System::String inputFile, System::String outputFile)
{
    auto document = MakeObject<Document>(inputFile);
    auto page = document->get_Pages()->idx_get(1);

    auto link = MakeObject<LinkAnnotation>(page, MakeObject<Rectangle>(10, 580, 120, 600, true));
    link->set_Color(Color::get_Green());
    link->set_Action(MakeObject<GoToURIAction>(u"https://docs.aspose.com/pdf/python"));
    page->get_Annotations()->Add(link);
    document->Save(outputFile);
}

Add a green dashed link on page 1 using LaunchAction with inputFile as the launch target. Activation depends on the PDF viewer.

  1. Open the input PDF with Document.
  2. Create a LinkAnnotation and configure its Border and color.
  3. Assign a LaunchAction with the document and input file path.
  4. Add the link with AnnotationCollection::Add().
  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/Border.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/Dash.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/LinkAnnotation.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/LaunchAction.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;

void CreateLinkAnnotationLaunchAction(System::String inputFile, System::String outputFile)
{
    auto document = MakeObject<Document>(inputFile);
    auto page = document->get_Pages()->idx_get(1);

    auto link = MakeObject<LinkAnnotation>(page, MakeObject<Rectangle>(10, 580, 120, 600, true));
    auto border = MakeObject<Border>(link);
    border->set_Width(5);
    border->set_Dash(MakeObject<Dash>(1, 1));
    link->set_Border(border);
    link->set_Color(Color::get_Green());
    link->set_Action(MakeObject<LaunchAction>(document, inputFile));
    page->get_Annotations()->Add(link);
    document->Save(outputFile);
}