How to Insert an Image to PDF Grid Cell in C#

This sample demo has demonstrated how to draw nested grid in PDF document and set grid row&cell format. In the following section, we are going to create a simple PDF grid and show you how to insert an image to a specific PDF grid cell in C#. Before we can follow the code snippet below to accomplish the task, we have to prepare the environment first.

Download Spire.PDF and install it on system, create or open a .NET class application in Visual Studio 2005 or above versions, add Spire.PDF.dll to your .NET project assemblies.Then let's code step by step to make a better understanding about the whole procedure.

Step 1: Create a PDF document and add a new page.

PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();

Step 2: Create a 2×2 grid to PDF.

PdfGrid grid = new PdfGrid();
PdfGridRow row = grid.Rows.Add();
           row = grid.Rows.Add();
                 grid.Columns.Add(2);

Step 3: Set the cell padding of the PDF grid.

grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1);

Step 4: Set the width of the columns.

float width = page.Canvas.ClientSize.Width - (grid.Columns.Count + 1);
      grid.Columns[0].Width = width * 0.25f;
      grid.Columns[1].Width = width * 0.25f;

Step 5: Load an image from disk.

PdfGridCellContentList lst = new PdfGridCellContentList();
PdfGridCellContent textAndStyle = new PdfGridCellContent();
textAndStyle.Image = PdfImage.FromFile("..\\..\\image1.jpg");

Step 6: Set the size of image and insert it to the first cell.

textAndStyle.ImageSize = new SizeF(50, 50);
lst.List.Add(textAndStyle);
          
grid.Rows[0].Cells[0].Value = lst;
grid.Rows[1].Height = grid.Rows[0].Height;

Step 7: Draw PDF grid into page at the specific location.

PdfLayoutResult result = grid.Draw(page, new PointF(10, 30));

Step 8: Save to a PDF file and launch the file.

doc.SaveToFile(outputFile, FileFormat.PDF);
System.Diagnostics.Process.Start(outputFile);

Result:

Insert an Image to PDF Grid Cell in C#

Full C# Code:

static void Main(string[] args)
        {
            string outputFile = @"..\..\output.pdf";
            //Create a pdf document
            PdfDocument doc = new PdfDocument();
            //Add a page for the pdf document
            PdfPageBase page = doc.Pages.Add();
            //Create a pdf grid
            PdfGrid grid = new PdfGrid();
            //Set the cell padding of pdf grid
            grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1);
            //Add a row for pdf grid
            PdfGridRow row = grid.Rows.Add();
            //Add two columns for pdf grid 
            grid.Columns.Add(2);
            float width = page.Canvas.ClientSize.Width - (grid.Columns.Count + 1);
            //Set the width of the first column
            grid.Columns[0].Width = width * 0.25f;
            grid.Columns[1].Width = width * 0.25f;
            //Add a image
            PdfGridCellContentList lst = new PdfGridCellContentList();
            PdfGridCellContent textAndStyle = new PdfGridCellContent();
            textAndStyle.Image = PdfImage.FromFile("..\\..\\image1.jpg");
            //Set the size of image
            textAndStyle.ImageSize = new SizeF(50, 50);
            lst.List.Add(textAndStyle);
            //Add a image into the first cell. 
            row.Cells[0].Value = lst;
            //Draw pdf grid into page at the specific location
            PdfLayoutResult result = grid.Draw(page, new PointF(10, 30));
            //Save to a pdf file 
            doc.SaveToFile(outputFile, FileFormat.PDF);
            System.Diagnostics.Process.Start(outputFile);
        }