How to insert hyperlink in table of a PDF document
The Syncfusion® PDF library is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can insert hyperlink in table of PDF document.
Steps to insert hyperlink in table of PDF document programmatically:
- Create a new C# console application.
- Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from nuget.org.
- Include the following namespaces in Program.cs file.
C#
using Syncfusion.Pdf; using System.Drawing; using Syncfusion.Pdf.Grid; using Syncfusion.Pdf.Graphics; using Syncfusion.Pdf.Interactive;
VB.NET
Imports Syncfusion.Pdf Imports System.Drawing Imports Syncfusion.Pdf.Grid Imports Syncfusion.Pdf.Graphics Imports Syncfusion.Pdf.Interactive
- Use the following code snippet to insert hyperlink in table of PDF document.
C#
//Create a new PDF document PdfDocument pdfDocument = new PdfDocument(); //Create the page PdfPage pdfPage = pdfDocument.Pages.Add(); //Create new PdfGrid PdfGrid pdfGrid = new PdfGrid(); //Add row PdfGridRow row1 = pdfGrid.Rows.Add(); //Add columns pdfGrid.Columns.Add(2); //Set the value to the specific cell row1.Cells[0].Value = "Text"; row1.Cells[1].Value = "Hyperlink"; //Add row PdfGridRow row2 = pdfGrid.Rows.Add(); row2.Cells[0].Value = "Example to insert hyperlink in PDF table"; row2.Height = 20; pdfGrid.BeginCellLayout += PdfGrid_BeginCellLayout; //Create and customize the string formats PdfStringFormat format = new PdfStringFormat(); format.Alignment = PdfTextAlignment.Center; format.LineAlignment = PdfVerticalAlignment.Middle; //Add string format to respective cell pdfGrid.Rows[0].Cells[0].StringFormat = format; pdfGrid.Rows[0].Cells[1].StringFormat = format; pdfGrid.Rows[1].Cells[0].StringFormat = format; //Draw the PdfGrid pdfGrid.Draw(pdfPage, PointF.Empty); //Save the document pdfDocument.Save("Table.pdf"); //Close the document pdfDocument.Close(true); //This will open the PDF file so, the result will be seen in default PDF viewer Process.Start("Table.pdf"); /// <summary> /// Insert hyperlink event handler /// </summary> /// <param name="sender"></param> /// <param name="args"></param> private static void PdfGrid_BeginCellLayout(object sender, PdfGridBeginCellLayoutEventArgs args) { if (args.RowIndex == 1 && args.CellIndex == 1) { //Initialize the font PdfStandardFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12, PdfFontStyle.Italic); //Create the Text Web Link PdfTextWebLink textLink = new PdfTextWebLink(); //Set the hyperlink textLink.Url = "http://www.syncfusion.com"; //Set the link text textLink.Text = "Syncfusion .NET components and controls"; //Set the font textLink.Font = font; //Draw the hyperlink in PDF page textLink.DrawTextWebLink(pdfPage, new PointF(args.Bounds.X+5, args.Bounds.Y+3)); } }
VB.NET
'Create a new PDF document Dim pdfDocument As PdfDocument = New PdfDocument() 'Create the page Dim PdfPage As PdfPage = pdfDocument.Pages.Add() 'Create new PdfGrid Dim pdfGrid As PdfGrid = New PdfGrid() 'Add row Dim row1 As PdfGridRow = pdfGrid.Rows.Add() 'Add columns pdfGrid.Columns.Add(2) 'Set the value to the specific cell row1.Cells(0).Value = "Text" row1.Cells(1).Value = "Hyperlink" 'Add row Dim row2 As PdfGridRow = pdfGrid.Rows.Add() row2.Cells(0).Value = "Example to insert hyperlink in PDF table" row2.Height = 20 AddHandler pdfGrid.BeginCellLayout, AddressOf PdfGrid_BeginCellLayout 'Create and customize the string formats Dim format As PdfStringFormat = New PdfStringFormat() format.Alignment = PdfTextAlignment.Center format.LineAlignment = PdfVerticalAlignment.Middle 'Add string format to respective cell pdfGrid.Rows(0).Cells(0).StringFormat = format pdfGrid.Rows(0).Cells(1).StringFormat = format pdfGrid.Rows(1).Cells(0).StringFormat = format 'Draw the PdfGrid pdfGrid.Draw(pdfPage, PointF.Empty) 'Save the document pdfDocument.Save("Table.pdf") 'Close the document pdfDocument.Close(True) 'This will open the PDF file so, the result will be seen in default PDF Viewer Process.Start("Table.pdf") ''' <summary> ''' Insert hyperlink event handler ''' </summary> ''' <param name="sender"></param> ''' <param name="args"></param> Private Sub PdfGrid_BeginCellLayout(ByVal sender As Object, ByVal args As PdfGridBeginCellLayoutEventArgs) If args.RowIndex = 1 AndAlso args.CellIndex = 1 Then 'Initialize the font Dim font As PdfStandardFont = New PdfStandardFont(PdfFontFamily.Helvetica, 12, PdfFontStyle.Italic) 'Create the Text Web Link Dim textLink As PdfTextWebLink = New PdfTextWebLink() 'Set the hyperlink textLink.Url = "http://www.syncfusion.com" 'Set the link text textLink.Text = "Syncfusion .NET components and controls" 'Set the font textLink.Font = font 'Draw the hyperlink in PDF page textLink.DrawTextWebLink(pdfPage, New PointF(args.Bounds.X + 5, args.Bounds.Y + 3)) End If End Sub
A complete working sample can be download from InserHyperlink.zip.
By executing the program, you will get the PDF document as follows.
Take a moment to peruse the documentation, where you will find the options such as creating table using PdfLightTable and PdfGrid, cell and row customization in PdfLightTable and PdfGrid, buit-in table styles to PdfGrid, pagination in PdfGrid and PdfLightTable and features like web navigation, internal document navigation and external document navigation.
Click here to explore the rich set of Syncfusion Essential® PDF features.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, include a license key in your projects. Click this link to learn about generating and registering Syncfusion® license key in your application to use the components without trail message.
See Also:
Insert image and text in PDF table cell
Insert HTML table in PDF table