[英]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. 我正在使用PDF4NET库将用户上传的图像转换为pdf以进行打印。 What i want to achieve is draw the exact same size of image into the pdf.
我要实现的是将完全相同大小的图像绘制到pdf中。 For example, user uploaded a 16px x 16px image, and the pdf will show the same size image at the center.
例如,用户上传了一个16px x 16px的图片,而pdf会在中心显示相同大小的图片。
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. 问题是当用户上传小图像时,它将在绘制图像时拉伸以适合pdf。
** The result must be align in center of the pdf **结果必须与pdf的中心对齐
When you draw an image on the PDF page, the drawing size is specified in PDF points. 在PDF页面上绘制图像时,图纸尺寸以PDF点为单位指定。 PDF files do not use pixels.
PDF文件不使用像素。
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). 对于您的情况,您应该测试“比率”,如果它大于1(页面大于图像),则应按原样绘制图像(不乘以比率)。
Disclaimer: I work for the company that develops the PDF4NET library. 免责声明:我为开发PDF4NET库的公司工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.