How to insert hyperlink in footer ?

Hello Syncfusion's team,

I used the following code to insert hyperlink in footer :

------------------------ Begin

 PdfPageTemplateElement footer = new PdfPageTemplateElement(bounds);

PdfFont fontPied = new PdfStandardFont(PdfFontFamily.Helvetica, 8);

PdfBrush brush = new PdfSolidBrush(Syncfusion.Drawing.Color.Black);

///Hyperlink My WebSite

//Create the Text Web Link.

PdfTextWebLink textLink = new PdfTextWebLink();

//Set the hyperlink

textLink.Url = "http://www.mywebsite.com/";

//Set the link text

textLink.Text = "Click here to visit my web site";

//Set the font

 textLink.Font = fontPied;

//Draw the hyperlink in PDF page

textLink.DrawTextWebLink(footer.Graphics, new Syncfusion.Drawing.PointF(0, 20));

----------------------- End

But the last instruction < > generates the following exception:

Object reference not set to an instance of an object.

at Syncfusion.Pdf.Interactive.PdfTextWebLink.DrawTextWebLink (Syncfusion.Pdf.Graphics.PdfGraphics graphics, Syncfusion.Drawing.PointF location) [0x000ea] in <023cea152c704135bf5c2f86c6cf38b2>:0


Regards


1 Reply

AG Anantha Gokula Raman Jeyaraman Syncfusion Team August 5, 2021 11:39 AM UTC

Hi Cosypro,

Thank you for contacting Syncfusion support.

Using Syncfusion Essential PDF, we can’t add hyperlinks directly on header, footer and template, instead you can draw the contents in header/footer/template graphics separately and then add a URI annotation to the same location where header/footer/template content is drawn.

Kindly use the following code snippet for reference,

PdfDocument pdfDocument = new PdfDocument();

PdfPage pdfPage = pdfDocument.Pages.Add();

RectangleF bounds = new RectangleF(0, 0, pdfDocument.Pages[0].GetClientSize().Width, 50);

string text = "Click here to visit my web site";

//Add the footer content.

PdfPageTemplateElement footer = new PdfPageTemplateElement(bounds);

PdfFont fontPied = new PdfStandardFont(PdfFontFamily.Helvetica, 8);

footer.Graphics.DrawString(text, fontPied, PdfBrushes.Black, new PointF(0, 0));

pdfDocument.Template.Bottom = footer;

//Measure the text width.

SizeF textSize = fontPied.MeasureString(text);

//Add the URI annotation to the page on the footer location.

RectangleF rect = new RectangleF(0, pdfPage.GetClientSize().Height, textSize.Width, textSize.Height);

PdfUriAnnotation uriAnnotation = new PdfUriAnnotation(rect, "http://www.mywebsite.com/");

uriAnnotation.Border.Width = 0;

pdfPage.Annotations.Add(uriAnnotation);

Kindly let us know if you need any further assistance.

Regards,

Anantha Gokula Raman J


Loader.
Up arrow icon