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.
Change link annotation color
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.
- Open the input PDF with Document.
- Filter page 1 annotations by AnnotationType.
- Cast matching objects to LinkAnnotation.
- Set the annotation Color to red.
- 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);
}
Change text color in link regions
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.
- Open the input PDF with Document.
- Get the rectangle of each Link annotation through Annotation.
- Expand its coordinates through Rectangle properties.
- Create a TextFragmentAbsorber and restrict it with TextSearchOptions.
- Visit page 1 and set each fragment’s TextState foreground color to red.
- 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);
}
Update web link destinations
Change existing GoToURIAction targets on page 1 to https://www.aspose.com. Other link action types are skipped.
- Open the input PDF with Document.
- Filter for LinkAnnotation objects on page 1.
- Check each action for GoToURIAction.
- Set URI to the replacement URL.
- 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);
}