We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to preserve the aspect ratio of the image in PdfGrid

Platform: WinForms |
Control: PDF |
Published Date: February 27, 2017 |
Last Revised Date: May 6, 2019
Tags: pdf, pdfgrid

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

1)Syncfusion.Compression.Base.dll

2)Syncfusion.Pdf.Base.dll

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.
dataTable.Columns.Add("ID");
 
dataTable.Columns.Add("Name");
 
//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
doc.Save("Output.pdf");
doc.Close(true);

 

The working sample can be downloaded from the below link:

http://www.syncfusion.com/downloads/support/directtrac/general/ze/PdfGrid_Image-1760174287

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon