How to Insert Images in WinForms PDF Table 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 insert images into a PDF table in C# and VB.NET.
Steps to insert images into a PDF table programmatically:
- Create a new C# Windows Forms application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from NuGet.org.
Caption
- Include the following namespaces in the Form1.Designer.cs file.
C#
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Tables;
using System.Data;
using System.Drawing;
using System.IO;
VB.NET
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics
Imports Syncfusion.Pdf.Tables
Imports System.Data
Imports System.Drawing
Imports System.IO
- Use the following code snippet to insert images to PDF table.
C#
// Create a new PDF document
PdfDocument doc = new PdfDocument();
// Add a page
PdfPage page = doc.Pages.Add();
// Create a PdfLightTable
PdfLightTable pdfLightTable = new PdfLightTable();
// Initialize DataTable to assign as DataSource to the light table
DataTable table = new DataTable();
// Include columns to the DataTable
table.Columns.Add();
table.Columns.Add();
// Include rows to the DataTable
for (int i = 0; i < 3; i++)
table.Rows.Add();
// Assign data source
pdfLightTable.DataSource = table;
pdfLightTable.BeginRowLayout += pdfLightTable_BeginRowLayout;
pdfLightTable.BeginCellLayout += pdfLightTable_BeginCellLayout;
// Create and customize the string formats
PdfLightTableLayoutFormat format = new PdfLightTableLayoutFormat();
format.Break = PdfLayoutBreakType.FitPage;
format.Layout = PdfLayoutType.Paginate;
// Draw PdfLightTable
pdfLightTable.Draw(page, new RectangleF(0, 50, page.GetClientSize().Width, page.GetClientSize().Height), format);
// 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
doc.Save("Table.pdf");
// Close the document
doc.Close(true);
// This will open the PDF file so the result will be seen in the default PDF viewer
Process.Start("Table.pdf");
private void pdfLightTable_BeginRowLayout(object sender, BeginRowLayoutEventArgs args)
{
args.MinimalHeight = 100;
}
private void pdfLightTable_BeginCellLayout(object sender, BeginCellLayoutEventArgs args)
{
if (args.CellIndex == 0)
{
string[] files = Directory.GetFiles("");
// Draw image
PdfImage image = PdfImage.FromFile(files[args.RowIndex]);
args.Graphics.DrawImage(image, args.Bounds);
}
else
{
args.Graphics.DrawString(content[args.RowIndex], new PdfStandardFont(PdfFontFamily.TimesRoman, 10), PdfBrushes.Black, args.Bounds);
}
}
VB.NET
'Create a new PDF document
Dim doc As PdfDocument = New PdfDocument()
'Add a page
Dim page As PdfPage = doc.Pages.Add()
'Create a PdfLightTable
Dim pdfLightTable As PdfLightTable = New PdfLightTable()
'Initialize DataTable to assign as DataSource to the light table
Dim table As DataTable = New DataTable()
'Include columns to the DataTable
table.Columns.Add()
table.Columns.Add()
'Include rows to the DataTable
For i As Integer = 0 To 3 - 1
table.Rows.Add()
Next
'Assign data source
pdfLightTable.DataSource = table
AddHandler pdfLightTable.BeginRowLayout, AddressOf pdfLightTable_BeginRowLayout
AddHandler pdfLightTable.BeginCellLayout, AddressOf pdfLightTable_BeginCellLayout
'Create and customize the string formats
Dim format As PdfLightTableLayoutFormat = New PdfLightTableLayoutFormat()
format.Break = PdfLayoutBreakType.FitPage
format.Layout = PdfLayoutType.Paginate
pdfLightTable.Draw(page, New RectangleF(0, 50, page.GetClientSize().Width, page.GetClientSize().Height), format)
'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
doc.Save("Table.pdf")
'Close the document
doc.Close(True)
'This will open the PDF file so the result will be seen in the default PDF viewer
Process.Start("Table.pdf")
Private Sub pdfLightTable_BeginRowLayout(ByVal sender As Object, ByVal args As BeginRowLayoutEventArgs)
args.MinimalHeight = 100
End Sub
Private Sub pdfLightTable_BeginCellLayout(ByVal sender As Object, ByVal args As BeginCellLayoutEventArgs)
If args.CellIndex = 0 Then
Dim files As String() = Directory.GetFiles("")
Dim image As PdfImage = PdfImage.FromFile(files(args.RowIndex))
'Draw image
args.Graphics.DrawImage(image, args.Bounds)
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 PDFTable.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 for PdfGrid, pagination in PdfGrid and PdfLightTable and features like inserting an image in a new PDF document, image pagination, and image extraction with code examples.
Refer here to explore the rich set of Syncfusion Essential PDF features.
An online sample link to create a richly formatted table.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion license key in your application to use the components without a trial message.