How to add formatted html into a PdfLightTableCell?
In WinForms PDF, you can use PdfHTMLTextElement to add the formatted HTML tags in PdfLightTable.
The HTML text cannot be added directly. However, this can be done using the BeginCellLayout event handler.
Please refer to the following code example how to draw the HTML element into the PdfLightTableCell:
C#
//create the document PdfDocument document = new PdfDocument(); //Add the page PdfPage page = document.Pages.Add(); //Create the table PdfLightTable table = new PdfLightTable(); //Load the data table.DataSource = DataTable(); //Handle the row events table.BeginRowLayout += table_BeginRowLayout; //Handle the cell events table.BeginCellLayout += table_BeginCellLayout; //Draw the table table.Draw(page, new PointF(0, 20)); // Save and close the document. document.Save("Sample.pdf"); document.Close(true); private DataTable DataTable() { //Create a data table. DataTable table = new DataTable(); for (int i = 0; i < 5; i++) table.Columns.Add(); table.Rows.Add(new object[] { " ", "Cell2", "cell 3" }); table.Rows.Add(new object[] { " ", "Cell2", "cell 3" }); table.Rows.Add(new object[] { " ", "Cell2", "cell 3" }); table.Rows.Add(new object[] { " ", "Cell2", "cell 3" }); table.Rows.Add(new object[] { " ", "Cell2", "cell 3" }); table.Rows.Add(new object[] { " ", "Cell2", "cell 3" }); return table; } private void table_BeginCellLayout(object sender, BeginCellLayoutEventArgs args) { // Create the PdfTextElement PdfHTMLTextElement textElement = new PdfHTMLTextElement(); textElement.HTMLText = "<html> <head>Html </head><body><i> Formatted Text</i></body></html>"; textElement.Font = new PdfStandardFont(PdfFontFamily.Courier, 10); //Draw the Html Element in first cells if (args.CellIndex == 0) textElement.Draw(args.Graphics, args.Bounds); } private void table_BeginRowLayout(object sender, BeginRowLayoutEventArgs args) { //Set the height of the row args.MinimalHeight = 30; }
Sample link:
https://www.syncfusion.com/downloads/support/directtrac/general/ze/PdfLightTableSample1029052034.zip
Conclusion
I hope you enjoyed learning about how to add formatted html into a PdfLightTableCell.
You can refer to our WinForms PDF feature tour 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 WinForms PDF example 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 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!