如何使用itextsharp在out pdf中计算正确的图像大小? [英] How to calculate the correct image size in out pdf using itextsharp?

查看:218
本文介绍了如何使用itextsharp在out pdf中计算正确的图像大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用itextsharp将图像添加到pdf,无论图像大小如何,它总是显示为在pdf内映射到不同的更大尺寸?

I' am trying to add an image to a pdf using itextsharp, regardless of the image size it always appears to be mapped to a different greater size inside the pdf ?

我添加的图像是624x500像素(DPI:72):

The image I add is 624x500 pixel (DPI:72):

alt text http://www.freeimagehosting.net/uploads/727711dc70.png

这是输出pdf的屏幕:

And here is a screen of the output pdf:

alt text http://www.freeimagehosting.net/uploads/313d49044d.png

以下是我创建文档的方式:

And here is how I created the document:

Document document = new Document();                
                System.IO.MemoryStream stream = new MemoryStream();
                PdfWriter writer = PdfWriter.GetInstance(document, stream);
                document.Open();


                System.Drawing.Image pngImage = System.Drawing.Image.FromFile("test.png");
                Image pdfImage = Image.GetInstance(pngImage, System.Drawing.Imaging.ImageFormat.Png);


                document.Add(pdfImage);
                document.Close();

                byte[] buffer = stream.GetBuffer();
                FileStream fs = new FileStream("test.pdf", FileMode.Create);
                fs.Write(buffer, 0, buffer.Length);
                fs.Close();

关于如何计算正确尺寸的想法?

Any idea on how to calculate the correct size ?

我试过ScaleAbsolute,图片仍然呈现不正确的尺寸。

I alreay tried ScaleAbsolute and the image still renders with incorrect dimensions.

推荐答案

我忘了提到我是使用itextsharp 5.0.2。

I forget to mention that I' am using itextsharp 5.0.2.

原来,PDF DPI = 110,这意味着每英寸110像素,并且因为itextsharp使用点作为测量单位,所以:

It turned out that PDF DPI = 110, which means 110 pixels per inch, and since itextsharp uses points as measurment unit then :


  • n像素= n / 110英寸。

  • n英寸= n * 72分。

我需要一个帮助方法将像素转换为点数:

Having a helper method to convert pixels to points is all I needed:

public static float PixelsToPoints(float value,int dpi)
{
   return value / dpi * 72;
}

通过使用上面的公式并传递dpi值为110,它完美地工作:

By using the above formula and passing a dpi value of 110 it worked perfectly:

alt text http:// www。 freeimagehosting.net/uploads/1c8287b8d9.png

注意:由于您可以创建任何所需尺寸的pdf文档,因此打印时可能会导致缩放错误出你的文件。要解决这个问题,您需要做的就是在宽度和高度之间保持正确的宽高比[大约1:1.4142](参见:纸张尺寸 - 国际标准:ISO 216

Note: Since you can create pdf documents in any size you want, this may lead to incorrect scaling when printing out your documents. To overcome this issue all you need to do is to have the correct aspect ratio between width and height [approximately 1:1.4142] (see : Paper Size - The international standard: ISO 216 ).

这篇关于如何使用itextsharp在out pdf中计算正确的图像大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆