Enhance Your PDFs with Multiple Hyperlinks in a Single Grid Cell in .NET PDF Library
The Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. This guide demonstrates how to add multiple URI annotations within a single PDF grid cell using C# to create interactive links.
Steps to add multiple URI annotations in a PDF Grid Cell:
- Create a Console Application: Set up a new console application project.
- Install Syncfusion® Package: Add the Syncfusion.Pdf.Net.Core NuGet package to your console application.
- Include Required Namespaces: Add the following namespaces in
Program.cs.
C#
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Grid;
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf;
using Syncfusion.Drawing;
- Implementation: Use the following code to add multiple URI annotations within a single PDF grid cell:
C#
// Create a new instance of PdfDocument
PdfDocument document = new PdfDocument();
document.PageSettings.Orientation = PdfPageOrientation.Portrait;
document.PageSettings.Margins.All = 5;
PdfPage currentPage = null;
document.Pages.PageAdded += (sender, args) => { currentPage = args.Page; };
// Add a new page to the document
PdfPage page = document.Pages.Add();
PdfGraphics graphics = page.Graphics;
// Create a new PdfGrid
PdfGrid pdfGrid = new PdfGrid();
pdfGrid.Columns.Add(3);
PdfGridRow row1 = pdfGrid.Rows.Add();
row1.Cells[0].Value = "Employee Details";
row1.Cells[0].ColumnSpan = 3;
PdfGridRow row2 = pdfGrid.Rows.Add();
row2.Cells[0].Value = "Employee ID";
row2.Cells[1].Value = "Employee Name";
row2.Cells[2].Value = "Website";
PdfGridRow row3 = pdfGrid.Rows.Add();
row3.Cells[0].Value = "E01";
row3.Cells[1].Value = "Simons Bistro";
row3.Height = 50;
pdfGrid.BeginCellLayout += PdfGrid_BeginCellLayout;
PdfStringFormat format = new PdfStringFormat();
format.Alignment = PdfTextAlignment.Center;
pdfGrid.Rows[0].Cells[0].StringFormat = format;
// Draw the PdfGrid
pdfGrid.Draw(page, new PointF(10, 10));
// Saving and closing the document
using (MemoryStream stream = new MemoryStream())
{
document.Save(stream);
File.WriteAllBytes("Output.pdf", stream.ToArray());
}
document.Close(true);
void AddHyperlink(PdfPage page, RectangleF bounds, string url)
{
PdfUriAnnotation uriAnnotation = new PdfUriAnnotation(bounds, url);
uriAnnotation.Border.Width = 0;
uriAnnotation.Text = url;
page.Annotations.Add(uriAnnotation);
}
void PdfGrid_BeginCellLayout(object sender, PdfGridBeginCellLayoutEventArgs args)
{
if (args.RowIndex == 2 && args.CellIndex == 2)
{
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 10, PdfFontStyle.Regular | PdfFontStyle.Underline);
string text = "Google";
SizeF textSize = font.MeasureString(text);
RectangleF linkBounds = new RectangleF(args.Bounds.X, args.Bounds.Y, textSize.Width, textSize.Height);
args.Graphics.DrawString(text, font, PdfBrushes.Blue, linkBounds);
AddHyperlink(currentPage, linkBounds, "https://www.google.com");
float positionY = args.Bounds.Y + textSize.Height + 2;
text = "Yahoo";
textSize = font.MeasureString(text);
linkBounds = new RectangleF(args.Bounds.X, positionY, textSize.Width, textSize.Height);
args.Graphics.DrawString(text, font, PdfBrushes.Blue, linkBounds);
AddHyperlink(currentPage, linkBounds, "https://www.yahoo.com");
}
}
A complete working sample can be downloaded from Multiple_URI_annotation_in_a_single_PDF_grid_cell.zip
By executing the program, the output PDF document will be generated as shown below.
Take a moment to explore the documentation on working with tables, where you’ll find additional options such as grid pagination and various levels of grid customization.
Conclusion
I hope you enjoyed learning about how to add multiple URI annotations in a PDF grid cell.
You can refer to our .NET PDF library 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 .NET PDF library Example to understand how to create and manipulate data.
For current customers, you can check out our ASP.NET Core 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!