How to draw HTML tags in PDF table header 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 tags in PDF table header using C# and VB.NET.
Steps to draw HTML tags in PDF table header 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 tags in PDF table header.
C#
//Create a new PDF document PdfDocument pdfDocument = new PdfDocument(); //Add a page to the PDF document PdfPage pdfPage = pdfDocument.Pages.Add(); //Create a new PdfGrid PdfGrid pdfGrid = new PdfGrid(); //Add three columns pdfGrid.Columns.Add(3); //Add header pdfGrid.Headers.Add(1); PdfGridRow pdfGridHeader = pdfGrid.Headers[0]; //Set value as HTML styled text string htmlText = "<font color='#0000F8'><b>Employee ID</b></font>"; PdfHTMLTextElement richTextElement = new PdfHTMLTextElement(htmlText, new PdfStandardFont(PdfFontFamily.TimesRoman, 12), PdfBrushe pdfGridHeader.Cells[0].Value = richTextElement; htmlText = "<b><i>Employee Name</i></b>"; richTextElement = new PdfHTMLTextElement(htmlText, new PdfStandardFont(PdfFontFamily.TimesRoman, 12), PdfBrushes.Black); pdfGridHeader.Cells[1].Value = richTextElement; htmlText = "<u><i>Salary</i></u>"; richTextElement = new PdfHTMLTextElement(htmlText, new PdfStandardFont(PdfFontFamily.TimesRoman, 12), PdfBrushes.Black); pdfGridHeader.Cells[2].Value = richTextElement; //Add rows PdfGridRow pdfGridRow = pdfGrid.Rows.Add(); pdfGridRow.Cells[0].Value = "E01"; pdfGridRow.Cells[1].Value = "Clay"; pdfGridRow.Cells[2].Value = "$10,000"; //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");
VB.NET
'Create a new PDF document Dim pdfDocument As PdfDocument = New PdfDocument() 'Add a page to the PDF document Dim pdfPage As PdfPage = pdfDocument.Pages.Add() 'Create a new PdfGrid Dim pdfGrid As PdfGrid = New PdfGrid() 'Add three columns pdfGrid.Columns.Add(3) 'Add header pdfGrid.Headers.Add(1) Dim pdfGridHeader As PdfGridRow = pdfGrid.Headers(0) 'Set value as HTML styled text Dim htmlText As String = "<font color='#0000F8'><b>Employee ID</b></font>" Dim richTextElement As PdfHTMLTextElement = New PdfHTMLTextElement(htmlText, New PdfStandardFont(PdfFontFamily.TimesRoman, 12), PdfBrushes.Black) pdfGridHeader.Cells(0).Value = richTextElement htmlText = "<b><i>Employee Name</i></b>" richTextElement = New PdfHTMLTextElement(htmlText, New PdfStandardFont(PdfFontFamily.TimesRoman, 12), PdfBrushes.Black) pdfGridHeader.Cells(1).Value = richTextElement htmlText = "<u><i>Salary</i></u>" richTextElement = New PdfHTMLTextElement(htmlText, New PdfStandardFont(PdfFontFamily.TimesRoman, 12), PdfBrushes.Black) pdfGridHeader.Cells(2).Value = richTextElement 'Add rows Dim pdfGridRow As PdfGridRow = pdfGrid.Rows.Add() pdfGridRow.Cells(0).Value = "E01" pdfGridRow.Cells(1).Value = "Clay" pdfGridRow.Cells(2).Value = "$10,000" '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")
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 and adding a HTML styled text with code example.
Refer here to learn more about PDF Tables in Essential PDF library.
Note:
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.
Conclusion:
I hope you enjoyed learning about how to draw HTML tags in PDF table header using C# and VB.NET.
You can refer to our Flutter PDF feature tour page to learn about its other groundbreaking features and documentation, and how to quickly get started with configuration specifications. You can also explore our Flutter PDF Flutter PDF examples to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to explore 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 or feedback portal. We are always happy to assist you!