Python: Add Hyperlinks to PDF

Hyperlinks in PDF are interactive elements that, when clicked, can jump to a specific location in the document, to an external website, or to other resources. By inserting hyperlinks in a PDF document, you can provide supplementary information and enhance the overall integrity of the document. This article will demonstrate how to add hyperlinks to PDF files in Python using Spire.PDF for Python.

Install Spire.PDF for Python

This scenario requires Spire.PDF for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.

pip install Spire.PDF

If you are unsure how to install, please refer to this tutorial: How to Install Spire.PDF for Python on Windows

Add Hyperlinks to a PDF Document in Python

With Spire.PDF for Python, you can add web links, email links and file links to a PDF document. The following are the detailed steps:

  • Create a pdf document and add a page to it.
  • Specify a URL address and draw it directly on the page using PdfPageBase.Canvas.DrawString() method.
  • Create a PdfTextWebLink object.
  • Set the link's display text, URL address, and the font and brush used to draw it using properties of PdfTextWebLink class.
  • Draw the link on the page using PdfTextWebLink.DrawTextWebLink() method.
  • Create a PdfFileLinkAnnotation object and with a specified file.
  • Add the file link to the page annotations using PdfPageBase.AnnotationsWidget.Add(PdfFileLinkAnnotation) method.
  • Draw hypertext of the file link using PdfPageBase.Canvas.DrawString() method.
  • Save the result file using PdfDocument.SaveToFile() method.
  • Python
from spire.pdf.common import *
from spire.pdf import *

# Create a PdfDocument instance
pdf = PdfDocument()

# Add a page
page = pdf.Pages.Add()

# Initialize x, y coordinates
y = 30.0
x = 10.0

# Create true type fonts
font = PdfTrueTypeFont("Arial", 14.0,PdfFontStyle.Regular,True)
font1 = PdfTrueTypeFont("Arial", 14.0, PdfFontStyle.Underline,True)

# Add a simply link
label = "Simple Text Link: "
format = PdfStringFormat()
format.MeasureTrailingSpaces = True
page.Canvas.DrawString(label, font, PdfBrushes.get_Orange(), 0.0, y, format)
x = font.MeasureString(label, format).Width
url = "http://www.e-iceblue.com"
page.Canvas.DrawString(url, font1, PdfBrushes.get_Blue(), x, y)
y = y + 28

# Add a hypertext link
label = "Hypertext Link: "
page.Canvas.DrawString(label, font, PdfBrushes.get_Orange(), 0.0, y, format)
x = font.MeasureString(label, format).Width
webLink = PdfTextWebLink()
webLink.Text = "Home Page"
webLink.Url = url
webLink.Font = font1
webLink.Brush = PdfBrushes.get_Blue()
webLink.DrawTextWebLink(page.Canvas, PointF(x, y))
y = y + 28

# Add an Email link
label = "Email Link:  "
page.Canvas.DrawString(label, font, PdfBrushes.get_Orange(), 0.0, y, format)
x = font.MeasureString(label, format).Width
link = PdfTextWebLink()
link.Text = "Contact Us"
link.Url = "mailto:support@e-iceblue.com"
link.Font = font1
link.Brush = PdfBrushes.get_Blue()
link.DrawTextWebLink(page.Canvas, PointF(x, y))
y = y + 28

# Add a file link
label = "Document Link: "
page.Canvas.DrawString(label, font, PdfBrushes.get_Orange(), 0.0, y, format)
x = font.MeasureString(label, format).Width
text = "Open File"
location = PointF(x, y)
size = font1.MeasureString(text)
linkBounds = RectangleF(location, size)
fileLink = PdfFileLinkAnnotation(linkBounds,"C:\\Users\\Administrator\\Desktop\\Report.xlsx")
fileLink.Border = PdfAnnotationBorder(0.0)
page.AnnotationsWidget.Add(fileLink)
page.Canvas.DrawString(text, font1, PdfBrushes.get_Blue(), x, y)

#Save the result pdf file
pdf.SaveToFile("AddLinkstoPDF.pdf")
pdf.Close()

Python: Add Hyperlinks to PDF

Insert Hyperlinks into Existing Text in PDF in Python

Adding a hyperlink to existing text in a PDF document requires locating the text first. Once the location has been obtained, an object of PdfUriAnnotation class with the link can be created and added to the position. The following are the detailed steps:

  • Create a PdfDocument instance.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Get the first page using PdfDocument.Pages property.
  • Find all occurrences of the specified text on the page using PdfPageBase.FindText() method.
  • Loop through all occurrences of the found text and create a PdfUriAnnotation instance based on the text bounds of each occurrence.
  • Set the hyperlink URL, border, and border color using properties under PdfUriAnnotation class.
  • Insert the hyperlink to the page annotations using PdfPageBase.AnnotationsWidget.Add(PdfUriAnnotation) method.
  • Save the PDF file using PdfDocument.SaveToFile() method.
  • Python
from spire.pdf.common import *
from spire.pdf import *

# Create a PdfDocument instance
pdf = PdfDocument()

# Load a PDF file
pdf.LoadFromFile("input.pdf")

# Get the first page
page = pdf.Pages[0]

# Find all occurrences of the specified text on the page
collection = page.FindText("big O notation", TextFindParameter.IgnoreCase)

# Loop through all occurrences of the specified text
for find in collection.Finds:
    # Create a hyperlink annotation
    uri = PdfUriAnnotation(find.Bounds)
    # Set the URL of the hyperlink
    uri.Uri = "https://en.wikipedia.org/wiki/Big_O_notation"
    # Set the border of the hyperlink annotation
    uri.Border = PdfAnnotationBorder(1.0)
    # Set the color of the border
    uri.Color = PdfRGBColor(Color.get_Blue())
    # Add the hyperlink annotation to the page
    page.AnnotationsWidget.Add(uri)

#Save the result file
pdf.SaveToFile("SearchTextAndAddHyperlink.pdf")
pdf.Close()

Python: Add Hyperlinks to PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.