Articles in this section
Category / Section

How to draw the background picture in PDF table cell using C# and VB.NET

2 mins read

Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can draw background picture in PDF table cell using C# and VB.NET.

Steps to draw background picture in PDF table cell programmatically:

  1. Create a new C# console application project. Create a console application project
  2. Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org. NuGet package reference
  3. Include the following namespaces in Program.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

 

  1. Use the following code snippet to draw the background picture in PDF table cell.

C#

//Create a new PDF document
PdfDocument pdfDocument = new PdfDocument();
//Add page to the PDF document
PdfPage pdfPage = pdfDocument.Pages.Add();
//Create a new PdfGrid
PdfGrid pdfGrid = new PdfGrid();
//Add three columns
pdfGrid.Columns.Add(3);
//Add header
pdfGrid.Headers.Add(1);
PdfGridRow pdfGridHeader = pdfGrid.Headers[0];
pdfGridHeader.Cells[0].Value = "Employee ID";
pdfGridHeader.Cells[1].Value = "Employee Name";
pdfGridHeader.Cells[2].Value = "Salary";
//Add rows
PdfGridRow pdfGridRow = pdfGrid.Rows.Add();
pdfGridRow.Cells[0].Value = "E01";
pdfGridRow.Cells[1].Value = "Clay";
pdfGridRow.Cells[2].Value = "$10,000";
//Specify the style for the PdfGridCell
PdfGridCellStyle pdfGridCellStyle = new PdfGridCellStyle();
//Set background image
pdfGridCellStyle.BackgroundImage = new PdfBitmap("image.jpg");
pdfGridCellStyle.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 10, PdfFontStyle.Bold);
//Apply style
PdfGridCell pdfGridCell = pdfGrid.Rows[0].Cells[0];
pdfGridCell.Style = pdfGridCellStyle;
pdfGrid.Rows[0].Height = 50;
//Set image position for the background image in the style
pdfGridCell.ImagePosition = PdfGridImagePosition.Stretch;
//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
Process.Start("Table.pdf");

 

VB.NET

'Create a new PDF document
Dim pdfDocument As PdfDocument = New PdfDocument()
'Add page to the PDF document
Dim pdfPage As PdfPage = pdfDocument.Pages.Add()
'Create a new PdfGrid
Dim pdfGrid As PdfGrid = New PdfGrid()
'Add three columns
pdfGrid.Columns.Add(3)
pdfGrid.Headers.Add(1)
'Add header
Dim pdfGridHeader As PdfGridRow = pdfGrid.Headers(0)
pdfGridHeader.Cells(0).Value = "Employee ID"
pdfGridHeader.Cells(1).Value = "Employee Name"
pdfGridHeader.Cells(2).Value = "Salary"
'Add rows
Dim pdfGridRow As PdfGridRow = pdfGrid.Rows.Add()
pdfGridRow.Cells(0).Value = "E01"
pdfGridRow.Cells(1).Value = "Clay"
pdfGridRow.Cells(2).Value = "$10,000"
'Specify the style for the PdfGridCell
Dim pdfGridCellStyle As PdfGridCellStyle = New PdfGridCellStyle()
'Set background image
pdfGridCellStyle.BackgroundImage = New PdfBitmap("image.jpg")
pdfGridCellStyle.Font = New PdfStandardFont(PdfFontFamily.Helvetica, 10, PdfFontStyle.Bold)
'Apply style
Dim pdfGridCell As PdfGridCell = pdfGrid.Rows(0).Cells(0)
pdfGridCell.Style = pdfGridCellStyle
pdfGrid.Rows(0).Height = 50
'Set image position for the background image in the style
pdfGridCell.ImagePosition = PdfGridImagePosition.Stretch
'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
Process.Start("Table.pdf")

 

A complete working sample can be downloaded from PDFTableSample.zip.

By executing the program, you will get the PDF document as follows. Output document screenshot

Take a moment to peruse the documentation, where you can find other 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.

An online sample link to draw background picture in PDF table cell.

Refer here to learn more about PDF Tables.

Note:

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.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied