【问题标题】:iTextSharp: How to resize an image to fit a fix size?iTextSharp:如何调整图像大小以适应固定大小?
【发布时间】:2012-03-13 19:49:38
【问题描述】:

我希望能够使用 iTextSharp 4.2.0 将图像大小调整为 159x159 点,但生成的图像需要具有完全指定的尺寸。

我试过了:

Image image = Image.GetInstance(imagePath);
image.ScaleAbsolute(159f, 159f);

但图像不是正方形。它保持纵横比。

示例: 我有这张图片:

结果图像应该看起来像这样:

谢谢。

【问题讨论】:

    标签: c# image resize itext


    【解决方案1】:

    您描述的问题通常是当您尝试通过调用AddCell()Image 直接添加到PdfPTable 时发生的情况,这总是 缩放图像以适合PdfPCell .因此,如果您像这样将图像添加到Document

    Image img = Image.GetInstance(imagePath);
    img.ScaleAbsolute(159f, 159f);
    PdfPTable table = new PdfPTable(1);
    table.AddCell(img);
    document.Add(table);
    

    您的ScaleAbsolute() 呼叫被忽略。要获得您想要的缩放比例:

    PdfPTable table = new PdfPTable(1);
    table.AddCell(new PdfPCell(img));
    document.Add(table);
    

    【讨论】:

    • 这是一个非常有用的答案。谢谢。
    • 非常有用.. 谢谢。
    • 还要注意不要通过'new PdfPCell().setImage(img)'添加图像,这将添加图像作为单元格的背景,它会自动缩放以适应单元格的宽度和高度.
    • 设置 fit 属性对我有用:table.AddCell(new PdfPCell(img,true));
    【解决方案2】:

    PdfPCell 具有适合单元格中的图像的属性,因此只需将其设置为 true。

      iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance("/test.png");
    
      PdfPCell logocell = new PdfPCell(logo,true); //  **PdfPCell(Image,Boolean Fit)**
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-12
      • 2015-07-16
      • 2014-05-05
      • 2012-09-15
      • 1970-01-01
      • 2020-08-26
      相关资源
      最近更新 更多