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.
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 two images in one PDF table cell using C# and VB.NET.
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!