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. Image for the cookie policy date

Fitting jpg image to page in pdf without loosing quality

Hi, 

I am new to your tools and hoping for some direction.  I have read many posts on there regarding images in pdf's and resolution, etc.  I am trying to convert a jpg file into a pdf file.  The jpg files are 3,165 pixels wide by 1,856 pixels tall.  I want them to fill a landscape pdf page while retaining as much quality as possible.  

I read on this page (https://blog.idrsolutions.com/2012/03/there-are-several-versions-of-each-image-inside-your-pdf-file/) that images can be stored in pdf's in multiple formats so that you can display it in a resolution that fits the page but you can export it at its native resolution.  

I have tried this but I end up only seeing the top left corner of the image showing up on the page in the pdf and I loose the rest of the image.

Could you by chance share some code that accomplishes my goal?

Thanks.

3 Replies

SK Surya Kumar Syncfusion Team March 5, 2019 10:13 AM UTC

Hi Bruce, 

Greetings from Syncfsuion. 

In order to draw the image without loss of quality you need to draw the image to fit the page bounds. Please refer the below code snippet to do the same: 
//Create a new PDF document. 
PdfDocument doc = new PdfDocument(); 
 
doc.PageSettings.Orientation = PdfPageOrientation.Landscape; 
 
//Add a page to the document. 
PdfPage page = doc.Pages.Add(); 
 
//Create PDF graphics for the page 
PdfGraphics graphics = page.Graphics; 
 
//Load the image from the disk. 
PdfBitmap image = new PdfBitmap(imagePath); 
 
SizeF pageSize = page.GetClientSize(); 
 
//Setting image bounds 
RectangleF imageBounds = new RectangleF(0, 0, pageSize.Width, pageSize.Height); 
 
//Draw the image 
graphics.DrawImage(image, imageBounds); 
 
//Save the document. 
doc.Save(DataPathOutput+"Output.pdf"); 
 
//Close the document. 
doc.Close(true); 



Using the above code snippet will scale the image to fit in the provided bounds. We have created a simple sample for the same and it can be found in below link: 

You can refer our UG documentation to know more about the same. 

Please try the same let us know if you need any further information in this. 

Regards, 
Surya Kumar 



BG Bruce Groen March 6, 2019 10:00 AM UTC

Thanks for getting back to me so quickly.  I apologize, I should have specified that I am developing in vb.net.  I am having some problems converting a couple of the lines you provided into vb.net.  Would you mind please posting sample code in vb.net form?




KC Karthikeyan Chandrasekar Syncfusion Team March 7, 2019 06:43 AM UTC

Hi Bruce, 
As per your request we have created the sample which we have provided in VB.NET. Please find the code snippet and sample for the same from below, 
'Create PDF document 
Dim doc As PdfDocument = New PdfDocument() 
'Set orientation  
doc.PageSettings.Orientation = PdfPageOrientation.Landscape 
'Add page 
Dim page As PdfPage = doc.Pages.Add() 
'Create graphics 
Dim graphics As PdfGraphics = page.Graphics 
'Load the image from disk 
Dim image As PdfBitmap = New PdfBitmap(imagePath); 
'Get page size 
Dim pageSize As SizeF = page.GetClientSize() 
'Setting image bounds 
Dim imageBounds As RectangleF = New RectangleF(0, 0, pageSize.Width, pageSize.Height) 
'Draw the image 
graphics.DrawImage(image, imageBounds) 
'Save the document 
doc.Save("Output.pdf") 
'Close the document 
doc.Close(True) 
Process.Start("Output.pdf") 
 
Please let us know if you need any further assistance on this. 
 
Regards, 
Karthikeyan  


Loader.
Live Chat Icon For mobile
Up arrow icon