How to add image and text in the 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 add image and text in the PDF table cell using C# and VB.NET.
Steps to add image and text in the PDF table cell programmatically:
- Create a new C# Windows Forms 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 Form1.Designer.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; using Syncfusion.Pdf.Tables; using System.Drawing;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Graphics Imports Syncfusion.Pdf.Tables Imports System.Drawing
- Use the following code snippet in the click event of the button to add image and text in the PDF table cell.
C#
//Create a new PDF document PdfDocument document = new PdfDocument(); //Add a page PdfPage page = document.Pages.Add(); //Create a PdfLightTable PdfLightTable pdfLightTable = new PdfLightTable(); //Create a data table DataTable dataTable = new DataTable(); //Add columns to data table dataTable.Columns.Add(""); dataTable.Columns.Add(""); //Add rows to data table for (int i = 0; i < 3; i++) dataTable.Rows.Add(); //Assign data source pdfLightTable.DataSource = dataTable; pdfLightTable.BeginCellLayout += pdfLightTable_BeginCellLayout; pdfLightTable.BeginRowLayout += PdfLightTable_BeginRowLayout; //Draw light table to the page of PDF document pdfLightTable.Draw(page, new RectangleF(0, 50, page.GetClientSize().Width, page.GetClientSize().Height)); //Draw text page.Graphics.DrawString("Essential Studio Reporting Edition", new PdfStandardFont(PdfFontFamily.Helvetica, 15, PdfFontStyle.Bold), PdfBrushes.Orange, new PointF(0, 20)); //Save the document document.Save("Table.pdf"); //close the document document.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 document As New PdfDocument() 'Add a page Dim page As PdfPage = document.Pages.Add() 'Create a PdfLightTable Dim pdfLightTable As New PdfLightTable() 'Create a data table Dim dataTable As New DataTable() 'Add columns to data table dataTable.Columns.Add("") dataTable.Columns.Add("") 'Add rows to data table For i As Integer = 0 To 2 dataTable.Rows.Add() Next 'Assign data source pdfLightTable.DataSource = dataTable AddHandler pdfLightTable.BeginRowLayout, AddressOf PdfLightTable_BeginRowLayout AddHandler pdfLightTable.BeginCellLayout, AddressOf pdfLightTable_BeginCellLayout 'Draw light table to the page of PDF document pdfLightTable.Draw(page, New RectangleF(0, 50, page.GetClientSize().Width, page.GetClientSize().Height)) 'Draw text page.Graphics.DrawString("Essential Studio Reporting Edition", New PdfStandardFont(PdfFontFamily.Helvetica, 15, PdfFontStyle.Bold), PdfBrushes.Orange, New PointF(0, 20)) 'Save the document document.Save("Table.pdf") 'close the document document.Close(True) 'This will open the PDF file so, the result will be seen in default PDF viewer Process.Start("Table.pdf")
- Use the following code in event handler.
C#
private void PdfLightTable_BeginRowLayout(object sender, BeginRowLayoutEventArgs args) { args.MinimalHeight = 100; } private void pdfLightTable_BeginCellLayout(object sender, BeginCellLayoutEventArgs args) { PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 10, PdfFontStyle.Bold); //Draw string and draw image in the same cell if (args.CellIndex == 0) { string[] files = Directory.GetFiles(path); //Draw string args.Graphics.DrawString(topic[args.RowIndex], font, PdfBrushes.Black, args.Bounds.X, args.Bounds.Y); //Draw image PdfImage image = PdfImage.FromFile(files[args.RowIndex]); args.Graphics.DrawImage(image, args.Bounds.X, args.Bounds.Y + font.Height, args.Bounds.Width, args.Bounds.Height - font.Height); } else { args.Graphics.DrawString(content[args.RowIndex], new PdfStandardFont(PdfFontFamily.TimesRoman, 10), PdfBrushes.Black, args.Bounds); } }
VB.NET
Private Sub PdfLightTable_BeginRowLayout(sender As Object, args As BeginRowLayoutEventArgs) args.MinimalHeight = 100 End Sub Private Sub pdfLightTable_BeginCellLayout(sender As Object, args As BeginCellLayoutEventArgs) Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 10, PdfFontStyle.Bold) 'Draw string and draw image in the same cell If args.CellIndex = 0 Then Dim files As String() = Directory.GetFiles(path) 'Draw string args.Graphics.DrawString(topic(args.RowIndex), font, PdfBrushes.Black, args.Bounds.X, args.Bounds.Y) 'Draw image Dim image As PdfImage = PdfImage.FromFile(files(args.RowIndex)) args.Graphics.DrawImage(image, args.Bounds.X, args.Bounds.Y + font.Height, args.Bounds.Width, args.Bounds.Height - font.Height) Else args.Graphics.DrawString(content(args.RowIndex), New PdfStandardFont(PdfFontFamily.TimesRoman, 10), PdfBrushes.Black, args.Bounds) End If End Sub
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 options like creating a table using PdfLightTable and PdfGrid, cell and row customization in PdfLightTable and PdfGrid, built-in table styles to PdfGrid, pagination in PdfGrid and PdfLightTable and features like inserting an image in a new PDF document with code examples.
Refer here to explore the rich set of Syncfusion Essential PDF features.
An online sample link to create richly formatted table.
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.