Articles in this section
Category / Section

How to preserve the aspect ratio of the image in PdfGrid

1 min read

Preserve the aspect ratio of the image in PdfGrid

The aspect ratio of an image can be preserved in PdfGrid by using ImagePosition property which must be set to “fit” in the PdfGrid cell.

Assemblies Required



The below code snippet illustrates how to preserve the aspect ratio of the image in PDF Grid.

Code Snippet:

//Creates a new PDF document.
PdfDocument doc = new PdfDocument();
//Adds a page.
PdfPage page = doc.Pages.Add();
//Creates a PdfGrid.
PdfGrid pdfGrid = new PdfGrid();
//Creates a DataTable.
DataTable dataTable = new DataTable();
//Adds columns to the DataTable.
//Adds rows to the DataTable.
 dataTable.Rows.Add(new object[] { " ", "Clay" });
dataTable.Rows.Add(new object[] { "E02", "Thomas" });
//Assigns data source.
pdfGrid.DataSource = dataTable;
//Gets the Image
PdfBitmap pBmp = new PdfBitmap(@"..\..\Data\logo.png");
pdfGrid.Rows[0].Cells[0].Style.BackgroundImage = pBmp;
//Fit the image inorder to preserve the aspect ratio.
pdfGrid.Rows[0].Cells[0].ImagePosition = PdfGridImagePosition.Fit;
   // pdfGrid.Rows[0].Cells[0].ImagePosition = PdfGridImagePosition.Center;
      // pdfGrid.Rows[0].Cells[0].ImagePosition = PdfGridImagePosition.Stretch;
   // pdfGrid.Rows[0].Cells[0].ImagePosition = PdfGridImagePosition.Tile
//Draws grid to the page of PDF document.
pdfGrid.Draw(page, new PointF(10, 10));
//Saves the document and dispose it


The working sample can be downloaded from the below link:

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