Create Links in PDF file with C++

By adding a link to an application into a document, it is possible to link to applications from a document. This is useful when you want readers to take a certain action at a specific point in a tutorial, for example, or to create a feature-rich document. To create an application link:

  1. Create a Document object.
  2. Get the Page you want to add link to.
  3. Create a LinkAnnotation object using the Page and Rectangle objects.
  4. Set the link attributes using the LinkAnnotation object.
  5. Also, set the to LaunchAction object’s Action property.
  6. When creating the LaunchAction object, specify the application you want to launch.
  7. Add the link to the Page object’s Annotations property.
  8. Finally, save the updated PDF using the Document object’s Save method.

The following code snippet shows how to create a link to an application in a PDF file.

using namespace System;
using namespace Aspose::Pdf;

void CreateLink() 
{
    String _dataDir("C:\\Samples\\");
    // Create Document instance
    auto document = MakeObject<Document>(_dataDir + u"CreateApplicationLink.pdf");

    // Add page to pages collection of PDF file
    auto page = document->get_Pages()->idx_get(1);

    auto link = MakeObject<Aspose::Pdf::Annotations::LinkAnnotation>(page, MakeObject<Rectangle>(100, 200, 300, 300));
    link->set_Color(Aspose::Pdf::Color::get_Green());
    link->set_Action(MakeObject<Aspose::Pdf::Annotations::LaunchAction>(document, _dataDir + u"sample.pdf"));
    page->get_Annotations()->Add(link);

    // Save updated document
    document->Save(_dataDir + u"CreateApplicationLink.pdf");
}

Aspose.PDF for C++ allows you to add a link to an external PDF file so that you can link several documents together. To create a PDF document link:

  1. First, create a Document object.
  2. Then, get the particular Page you want to add the link to.
  3. Create a LinkAnnotation object using the Page and Rectangle objects.
  4. Set the link attributes using the LinkAnnotation object.
  5. Set the Action property to the GoToRemoteAction object.
  6. While creating the GoToRemoteAction object, specify the PDF file that should launch, as well as the page number it should open on.
  7. Add the link to the Page object’s Annotations collection.
  8. Save the updated PDF using the Document object’s Save method.

The following code snippet shows how to create PDF document link in a PDF file.

void CreatePDFDocumentLink() 
{

   String _dataDir("C:\\Samples\\");
   // Create Document instance
   auto document = MakeObject<Document>(_dataDir + u"CreateDocumentLink.pdf");

   // Add page to pages collection of PDF file
   auto page = document->get_Pages()->idx_get(1);


   auto link = MakeObject<Aspose::Pdf::Annotations::LinkAnnotation>(page, MakeObject<Rectangle>(100, 200, 300, 300));
   link->set_Color(Aspose::Pdf::Color::get_Green());

   link->set_Action(MakeObject<Aspose::Pdf::Annotations::GoToRemoteAction>(_dataDir + u"sample.pdf", 1));
   page->get_Annotations()->Add(link);

   // Save updated document
   document->Save(_dataDir + u"CreateDocumentLink_out.pdf");
}