Articles in this section
Category / Section

How to add a PdfTextWebLink in the PDF Header using Syncfusion Essential PDF in a C#?

2 mins read

The Syncfusion Essential® PDF is a feature-rich and high performance .NET PDF library used to create, read, and edit PDF documents programmatically without Adobe dependencies. Using this library, you can add a PdfTextWebLink in the PDF Header using C#.

Steps to add a PdfTextWebLink in PDF the Header programmatically:

  1. Create a new console application project.Screenshot (1337).png
  2. Install the Syncfusion.Pdf.Net.Core NuGet package as a reference to your console application from Nuget.org.
    Screenshot (2157).png
  3. Include the following necessary namespaces in your Program.cs file.

C#

using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf;
using Syncfusion.Drawing;
using Syncfusion.Pdf.Interactive;
  1. Add the following code to your Program.cs to create a PDF with a header containing a web link:

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));
   // Set the text of the annotation to "Google"
   uriAnnotation.Text = "Google";
   // Set the URI of the annotation to the Google homepage
   uriAnnotation.Uri = "https://www.google.com";
   // Set the border width of the annotation to 0 (no border)
   uriAnnotation.Border = new PdfAnnotationBorder(0);
   // Add the annotation to the page's annotations collection
   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);
// Close the document and release all resources
document.Close(true);
// Write the contents of the memory stream to a file
File.WriteAllBytes("HyperLinkPDFHeader.pdf", stream.ToArray());

static PdfPageTemplateElement CreateHeader(string headerText, PdfFont font)
{
   //Create header template
   PdfPageTemplateElement header = new PdfPageTemplateElement(new RectangleF(0, 0, PdfPageSize.A4.Width, 50));
   //Create brush
   PdfBrush brush = new PdfSolidBrush(Color.Blue);
   //Draw the hyper link text
   header.Graphics.DrawString(headerText, font, brush, new PointF(0, 10));
   //Add the header template at the top.
   return header;
}

A complete working sample can be downloaded from PdfTextWebLink_in_PDF_Header.zip

By following these steps and using the provided code, you will create a PDF document with a header that includes a clickable web link as follows.Screenshot (1556).png

Take a moment to peruse the documentation, where you can find adding header and footer in a PDF document with code example.

Refer here to explore the rich set of Syncfusion Essential® PDF features.

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