How to add two images in one 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 two images in one PDF table cell using C# and VB.NET.
Steps to add two images in one PDF 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.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 in the click event of the button to add two images in one PDF cell.
C#
//Create a new PDF document
PdfDocument pdfDocument = new PdfDocument();
//Create the page
PdfPage pdfPage = pdfDocument.Pages.Add();
//Create new PdfGrid
PdfGrid pdfGrid = new PdfGrid();
//Add row
PdfGridRow row1 = pdfGrid.Rows.Add();
//Add columns
pdfGrid.Columns.Add(1);
//Set the value to the specific cell
row1.Cells[0].Value = "Two images in one PDF table cell";
//Add row
PdfGridRow row2 = pdfGrid.Rows.Add();
row2.Height = 400;
pdfGrid.BeginCellLayout += PdfGrid_BeginCellLayout;
//Create and customize the string formats
PdfStringFormat format = new PdfStringFormat();
format.Alignment = PdfTextAlignment.Center;
pdfGrid.Rows[0].Cells[0].StringFormat = format;
//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
System.Diagnostics.Process.Start("Table.pdf");
VB.NET
'Create a new PDF document
Dim pdfDocument As New PdfDocument()
'Create the page
Dim pdfPage As PdfPage = pdfDocument.Pages.Add()
'Create new PdfGrid
Dim pdfGrid As New PdfGrid()
'Add row
Dim row1 As PdfGridRow = pdfGrid.Rows.Add()
'Add columns
pdfGrid.Columns.Add(1)
'Set the value to the specific cell
row1.Cells(0).Value = "Two images in one PDF table cell"
'Add row
Dim row2 As PdfGridRow = pdfGrid.Rows.Add()
row2.Height = 400
AddHandler pdfGrid.BeginCellLayout, AddressOf PdfGrid_BeginCellLayout
'Create and customize the string formats
Dim format As New PdfStringFormat()
format.Alignment = PdfTextAlignment.Center
pdfGrid.Rows(0).Cells(0).StringFormat = format
'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
System.Diagnostics.Process.Start("Table.pdf")
- Use the following code in event handler.
C#
private static void PdfGrid_BeginCellLayout(object sender, PdfGridBeginCellLayoutEventArgs args)
{
//Draw images one after another in single PDF cell
if(args.RowIndex==1 && args.CellIndex ==0)
{
//Load the first image from the disk
PdfBitmap image1 = new PdfBitmap("Image1.jpg");
//Draw first image
args.Graphics.DrawImage(image1, new RectangleF(args.Bounds.X,args.Bounds.Y,args.Bounds.Width,200));
//Load the second image from the disk
PdfBitmap image2 = new PdfBitmap("Image2.jpg");
//Draw second image
args.Graphics.DrawImage(image2, new RectangleF(args.Bounds.X,args.Bounds.Y+200,args.Bounds.Width,200));
}
}
VB.NET
Private Shared Sub PdfGrid_BeginCellLayout(sender As Object, args As PdfGridBeginCellLayoutEventArgs)
'Draw images one after another in single PDF cell
If args.RowIndex = 1 AndAlso args.CellIndex = 0 Then
'Load the first image from the disk
Dim image1 As New PdfBitmap("Image1.jpg")
'Draw first image
args.Graphics.DrawImage(image1, New RectangleF(args.Bounds.X, args.Bounds.Y, args.Bounds.Width, 200))
'Load the second image from the disk
Dim image2 As New PdfBitmap("Image2.jpg")
'Draw second image
args.Graphics.DrawImage(image2, New RectangleF(args.Bounds.X, args.Bounds.Y + 200, args.Bounds.Width, 200))
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 Esxsential PDF features.
An online sample link to create richly formatted table in PDF document.
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.