How to draw HTML style text in PDF table cell using C# and VB.NET
Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can draw HTML style text in PDF table cell using C# and VB.NET.
Steps to draw HTML style text in PDF table cell programmatically:
- Create a new C# 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 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 draw HTML style text in PDF 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 columns pdfGrid.Columns.Add(2); //Add row PdfGridRow row1 = pdfGrid.Rows.Add(); row1.Cells[0].Value = "Product Name"; row1.Cells[1].Value = "Description"; PdfGridRow row2 = pdfGrid.Rows.Add(); row2.Cells[0].Value = "Essential PDF"; string htmlText = "<font color='#0000F8'><b>Essential PDF</b></font> is a <u><i>.NET library</i></u> " + "with the capability to produce <i>Adobe PDF files </i>."; //Render HTML text PdfHTMLTextElement richTextElement = new PdfHTMLTextElement(htmlText, new PdfStandardFont(PdfFontFamily.TimesRoman, 12), PdfBrushes.Black); //Set HTML styled text value to the table cell row2.Cells[1].Value = richTextElement; //Draw 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");
VB.NET
'Create a new PDF document Dim pdfDocument As New PdfDocument() 'Create the page Dim pdfPage As PdfPage = pdfDocument.Pages.Add() 'Create new PdfGrid Dim pdfGrid As New PdfGrid() 'Add columns pdfGrid.Columns.Add(2) 'Add row Dim row1 As PdfGridRow = pdfGrid.Rows.Add() row1.Cells(0).Value = "Product Name" row1.Cells(1).Value = "Description" Dim row2 As PdfGridRow = pdfGrid.Rows.Add() row2.Cells(0).Value = "Essential PDF" Dim htmlText As String = "<font color='#0000F8'><b>Essential PDF</b></font> is a <u><i>.NET library</i></u> " + "with the capability to produce <i>Adobe PDF files </i>." 'Render HTML text Dim richTextElement As New PdfHTMLTextElement(htmlText, New PdfStandardFont(PdfFontFamily.TimesRoman, 12), PdfBrushes.Black) 'Set HTML styled text value to the table cell row2.Cells(1).Value = richTextElement 'Draw 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")
A complete working sample can be downloaded from PDFTableSample.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, adding a HTML styled text with code example.
Refer here to learn more about PDF Tables in Essential PDF library.
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.