How to use multiple fonts in a PDF table cell using C# and VB.NET?
Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can use multiple fonts in a table cell using C# and VB.NET.
Steps to use multiple fonts in a table cell programmatically:
- Create a new console application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
- Include the following namespaces in the Program.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; using Syncfusion.Pdf.Grid; using System.Drawing;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Graphics Imports Syncfusion.Pdf.Grid Imports System.Drawing
- Use the following code snippet to use multiple fonts in a table cell.
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(3); //Set the value to the specific cell row1.Cells[0].Value = "Employee Details"; row1.Cells[0].ColumnSpan = 3; //Add row PdfGridRow row2 = pdfGrid.Rows.Add(); row2.Cells[0].Value = "Employee ID"; row2.Cells[1].Value = "Employee Name"; row2.Cells[2].Value = "Employee Address"; //Add row PdfGridRow row3 = pdfGrid.Rows.Add(); row3.Cells[0].Value = "E01"; row3.Cells[1].Value = "Simons Bistro"; row3.Height = 50; pdfGrid.BeginCellLayout += PdfGrid_BeginCellLayout; //Create and customize the string formats PdfStringFormat format = new PdfStringFormat(); format.Alignment = PdfTextAlignment.Center; pdfGrid.Rows[0].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 System.Diagnostics.Process.Start("Table.pdf"); private static void PdfGrid_BeginCellLayout(object sender, PdfGridBeginCellLayoutEventArgs args) { //Draw multiple fonts in a single table cell if(args.RowIndex==2 && args.CellIndex ==2) { PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 10, PdfFontStyle.Bold); args.Graphics.DrawString("Phone: 31 12 34 56", font, PdfBrushes.Black, args.Bounds.X, args.Bounds.Y); font = new PdfStandardFont(PdfFontFamily.TimesRoman, 10, PdfFontStyle.Italic); args.Graphics.DrawString("Email: simonsbistro@outlook.com ", font, PdfBrushes.Black, args.Bounds.X, args.Bounds.Y + 12); font = new PdfStandardFont(PdfFontFamily.Courier, 10, PdfFontStyle.Italic); args.Graphics.DrawString("Address:Vinbaeltet 34,US", font, PdfBrushes.Black, args.Bounds.X, args.Bounds.Y + (2 * 12)); } }
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(3) 'Set the value to the specific cell row1.Cells(0).Value = "Employee Details" row1.Cells(0).ColumnSpan = 3 'Add row Dim row2 As PdfGridRow = pdfGrid.Rows.Add() row2.Cells(0).Value = "Employee ID" row2.Cells(1).Value = "Employee Name" row2.Cells(2).Value = "Employee Address" 'Add row Dim row3 As PdfGridRow = pdfGrid.Rows.Add() row3.Cells(0).Value = "E01" row3.Cells(1).Value = "Simons Bistro" row3.Height = 50 AddHandler pdfGrid.BeginCellLayout, AddressOf PdfGrid_BeginCellLayout 'Create and customize the string formats Dim format As PdfStringFormat = New PdfStringFormat() format.Alignment = PdfTextAlignment.Center pdfGrid.Rows(0).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 System.Diagnostics.Process.Start("Table.pdf") Private Shared Sub PdfGrid_BeginCellLayout(ByVal sender As Object, ByVal args As PdfGridBeginCellLayoutEventArgs) 'Draw multiple fonts in a single table cell If args.RowIndex = 2 AndAlso args.CellIndex = 2 Then Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 10, PdfFontStyle.Bold) args.Graphics.DrawString("Phone: 31 12 34 56", font, PdfBrushes.Black, args.Bounds.X, args.Bounds.Y) font = New PdfStandardFont(PdfFontFamily.TimesRoman, 10, PdfFontStyle.Italic) args.Graphics.DrawString("Email: simonsbistro@outlook.com ", font, PdfBrushes.Black, args.Bounds.X, args.Bounds.Y + 12) font = New PdfStandardFont(PdfFontFamily.Courier, 10, PdfFontStyle.Italic) args.Graphics.DrawString("Address:Vinbaeltet 34,US", font, PdfBrushes.Black, args.Bounds.X, args.Bounds.Y + (2 * 12)) End If End Sub
A complete working sample can be downloaded from PDFFormSample.zip.
By executing the program, you will get the PDF document as follows.
Take a moment to peruse the documentation, where you can find the options like creating a table using PdfLightTable and PdfGrid, cell and row customization in PdfLightTable and PdfGrid, built-in table styles to PdfGrid, and pagination in PdfGrid and PdfLightTable with code examples.
Refer 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. Refer to link to learn about generating and registering Syncfusion® license key in your application to use the components without trail message.