itextsharp图片生成pdf模糊问题解释

本文介绍了使用iTextSharp 5.0.2版本时遇到的PDF图片模糊问题,关键在于理解PDF的DPI与点单位之间的转换。通过公式n pixels = n/110 inches和n inches = n * 72 points,可以将像素转换为点,以确保清晰度。例如,将653*1636像素的图片转换为PDF时,尺寸应调整为427.4 * 1070.8点,以保持正确的比例。注意,不同PDF尺寸可能影响打印效果,建议保持宽度和高度的正确比例以符合ISO 216标准。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

  • n pixels = n/110 inches.
  • n inches = n * 72 points.

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;
}

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

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

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 ).

举个例子,有图片653*1636大小,生成PDF:所使用的尺寸应为,653/110 * 72 = 427.4,  1636/110 * 72 = 1070.8

 1 iTextSharp.text.Rectangle Rec = new iTextSharp.text.Rectangle(427.5f, 1070.8f);
 2             //设置页面颜色
 3             //Rec.BackgroundColor = new iTextSharp.text.BaseColor(0, 0, 0);
 4 
 5             Document doc = new Document(Rec, 0, 0, 0, 0);
 6 
 7             try
 8             {
 9                 PdfWriter writer = PdfWriter.GetInstance(doc,
10                     new FileStream(@"C:\Us***\Images.pdf", FileMode.Create));
11                 doc.Open();
12                 doc.AddTitle("****相关电子资料");//标题
13                 doc.AddSubject("申请审批文档");//主题
14                 doc.AddKeywords("***表");//关键字
15                 doc.AddAuthor("**软件");//作者
16                 doc.AddCreationDate();//创建时间
17                 doc.AddCreator("***管理系统");
18                 writer.Info.Put(new PdfName("Producer"), new PdfString("江苏省**科技有限责任公司"));
19                 Paragraph para = new Paragraph();
20                 //para.IndentationLeft = 0f; //左缩进
21                 doc.Add(para);
22                 //foreach (string file in listBox1.Items)
23                 {
24                     doc.NewPage();
25                     iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(@"G:\***\Snap11.jpg");
26                     //img.ScalePercent(100f);
27                     img.ScaleAbsolute(Rec);
28                     //img.SetDpi(200, 200);
29                     //img.ScalePercent(72.0F / 96.0F * 100);
30 
31                     img.Alignment = iTextSharp.text.Image.UNDERLYING;
32                     img.SetAbsolutePosition(0, 0);
33                     doc.Add(img);
34                 }
35             }
36             catch (DocumentException dex)
37             {
38                 MessageBox.Show(dex.Message);
39             }
40             catch (IOException ioex)
41             {
42                 MessageBox.Show(ioex.Message);
43             }
44             catch (Exception ex)
45             {
46                 MessageBox.Show(ex.Message);
47             }
48             finally
49             {
50                 doc.Close();
51             }

 

posted on 2017-08-28 16:06 Anders' Yan 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/yansc/p/7444287.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值