【发布时间】:2012-03-13 19:49:38
【问题描述】:
我希望能够使用 iTextSharp 4.2.0 将图像大小调整为 159x159 点,但生成的图像需要具有完全指定的尺寸。
我试过了:
Image image = Image.GetInstance(imagePath);
image.ScaleAbsolute(159f, 159f);
但图像不是正方形。它保持纵横比。
示例: 我有这张图片:
结果图像应该看起来像这样:
谢谢。
【问题讨论】:
我希望能够使用 iTextSharp 4.2.0 将图像大小调整为 159x159 点,但生成的图像需要具有完全指定的尺寸。
我试过了:
Image image = Image.GetInstance(imagePath);
image.ScaleAbsolute(159f, 159f);
但图像不是正方形。它保持纵横比。
示例: 我有这张图片:
结果图像应该看起来像这样:
谢谢。
【问题讨论】:
您描述的问题通常是当您尝试通过调用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);
【讨论】:
PdfPCell 具有适合单元格中的图像的属性,因此只需将其设置为 true。
iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance("/test.png");
PdfPCell logocell = new PdfPCell(logo,true); // **PdfPCell(Image,Boolean Fit)**
【讨论】: