How to Add Hyperlink in PDF using Python

This quick tutorial explains how to add hyperlink in PDF using Python. It describes the details to write the application by sharing the steps and a runnable sample code to insert link in PDF using Python. You will also learn different options to customize the hyperlink and place it at any desired location on the page.

  1. Set the environment to use Aspose.PDF for Python via .NET to create a hyperlink
  2. Load the PDF using the Document class object and get access to the target page
  3. Create a LinkAnnotation object in a particular rectangular area and set its properties
  4. Create a FreeTextAnnotation object in the same rectangular area and set the relevant properties
  5. Add the link and free Text annotations to the annotations collection on the page
  6. Save the output PDF on the disk

The aforementioned steps describe how to add link in PDF using Python. A detailed program flow is presented where first the target page is accessed from a PDF file and then a link annotation is added with specified attributes. In the next step, a text annotation is added at the same location where link annotation is added, and its basic attributes are set like the text color is set to Blue, the font is set and other relevant properties are set before adding these annotations to the page.

This code demonstrates how to insert hyperlink in PDF using Python where the link annotation is added by setting the rectangle area, border width, and link action. On the other hand for user assistance, a free text annotation is added by setting the font name, size, text color, and contents. Both the annotations are added to the annotations collection of the page and then the PDF is saved on the disk.

This article has taught us how to create a link to a PDF using Python. If you want to learn the process to insert a table in the PDF, refer to the article on how to insert table in PDF using Python.

 English