Articles in this section
Category / Section

Embed Clickable Web Links in PDF Headers Using Syncfusion Essential PDF in C#

3 mins read

The Syncfusion Essential® PDF is a powerful .NET PDF library for creating, reading, and editing PDF documents. This guide shows you how to add a PdfTextWebLink in the PDF header using C# to create interactive, clickable links without Adobe dependencies.

Steps to add a text web link to the PDF header:

  1. Create a Console Application: Set up a new console application project.
    Screenshot (1337).png
  2. Install Syncfusion® Package: Add the Syncfusion.Pdf.Net.Core NuGet package to your console application.
    Screenshot.png
  3. Include Required Namespaces: Add the following namespaces in Program.cs.

C#

using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf;
using Syncfusion.Drawing;
using Syncfusion.Pdf.Interactive;
  1. Code Implementation: Use the code below to embed a web link in the PDF header:

C#

// Create a new PDF document
PdfDocument document = new PdfDocument();
// Define the header text
string headerText = "Google";
// Create a font using the Helvetica typeface, size 12
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);
// Measure the header text to get its size
SizeF headerTextSize = font.MeasureString(headerText);
// Create the header template element
PdfPageTemplateElement header = CreateHeader(headerText, font);
// Assign the header to the document's top template
document.Template.Top = header;
// Handle the PageAdded event to add a URI annotation to each new page
document.Pages.PageAdded += (sender, args) =>
{
   // Create a URI annotation with the specified rectangle dimensions
   PdfUriAnnotation uriAnnotation = new PdfUriAnnotation(new RectangleF(0, 0 - (header.Height - 10), headerTextSize.Width, headerTextSize.Height));
   uriAnnotation.Text = "Google";
   uriAnnotation.Uri = "https://www.google.com";
   uriAnnotation.Border = new PdfAnnotationBorder(0);
   args.Page.Annotations.Add(uriAnnotation);
};
// Add a new page to the document
PdfPage page = document.Pages.Add();
// Add another new page to the document
page = document.Pages.Add();
// Save the document to a memory stream
MemoryStream stream = new MemoryStream();
document.Save(stream);
document.Close(true);
// Write the contents of the memory stream to a file
File.WriteAllBytes("HyperLinkPDFHeader.pdf", stream.ToArray());

// Function to create the header
static PdfPageTemplateElement CreateHeader(string headerText, PdfFont font)
{
   PdfPageTemplateElement header = new PdfPageTemplateElement(new RectangleF(0, 0, PdfPageSize.A4.Width, 50));
   PdfBrush brush = new PdfSolidBrush(Color.Blue);
   header.Graphics.DrawString(headerText, font, brush, new PointF(0, 10));
   return header;
}

A complete working sample can be downloaded from PdfTextWebLink_in_PDF_Header.zip

By executing the program, the output PDF document will be generated as shown below:

Screenshot (1556).png

Take a moment to explore the documentation, where you can find detailed guidance and code examples for adding headers and footers to a PDF document.

Conclusion
I hope you enjoyed learning about how to add a text web link to a PDF header.

You can refer to our ASP.NET Core PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core PDF example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion;, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied