zoukankan      html  css  js  c++  java
  • itextsharp图片生成pdf模糊问题解释

    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             }
  • 相关阅读:
    mysql 查询优化 ~ select count 知多少
    mongodb 案例 ~ 经典故障案例
    printk 驱动调试
    21天学通C++学习笔记(七):函数
    OPC UA
    MQTT
    分库分表
    水平、垂直权限问题(横向越权与纵向越权)
    数据库中的行转列和列转行
    面试知识点
  • 原文地址:https://www.cnblogs.com/yansc/p/7444287.html
Copyright © 2011-2022 走看看