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.
Create an internal page link
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.
- Open the input PDF with Document.
- Create a LinkAnnotation on page 1 with the sample rectangle.
- Configure its Border, Dash, and color.
- Assign a GoToAction targeting page 4 or the last available page.
- Add it with AnnotationCollection::Add().
- 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 remote PDF link
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.
- Open the input PDF with Document.
- Create a LinkAnnotation on page 1.
- Set its color and assign a GoToRemoteAction using inputFile and page 1.
- Add it with AnnotationCollection::Add().
- 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);
}
Create a web link
Add a green link on page 1 targeting https://docs.aspose.com/pdf/cpp/. The source URL is preserved.
- Open the input PDF with Document.
- Create a LinkAnnotation with the sample rectangle.
- Assign a GoToURIAction with the source URL.
- Add it through AnnotationCollection::Add().
- 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);
}
Create a file launch link
Add a green dashed link on page 1 using LaunchAction with inputFile as the launch target. Activation depends on the PDF viewer.
- Open the input PDF with Document.
- Create a LinkAnnotation and configure its Border and color.
- Assign a LaunchAction with the document and input file path.
- Add the link with AnnotationCollection::Add().
- 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);
}