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
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 add formatted html into a PdfLightTableCell.
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!