简体   繁体   中英

How to draw image to pdf and still maintain its original size?

I'm using PDF4NET library to convert image that uploaded by user into a pdf for printing. What i want to achieve is draw the exact same size of image into the pdf. For example, user uploaded a 16px x 16px image, and the pdf will show the same size image at the center.

The code that I'm using is like below:

 var canvas = page.Canvas;
 var hRatio = page.Width / objImage.Width;
 var vRatio = page.Height / objImage.Height;
 var ratio = Math.Min(hRatio, vRatio);
 page.Canvas.DrawImage(decodedPath, 0, 0, objImage.Width * ratio, objImage.Height * ratio, 0, PDFKeepAspectRatio.KeepWidth);

The problem is when user upload a small image, it will stretch to fit the pdf when draw image.

** The result must be align in center of the pdf

When you draw an image on the PDF page, the drawing size is specified in PDF points. PDF files do not use pixels.
For your situation you should test the 'ratio' and if it is greater than 1 (page is greater than the image) then you should draw the image as it is (no multiplication by ratio).

Disclaimer: I work for the company that develops the PDF4NET library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM