iTextSharp:如何调整图像大小以适应修正大小?

问题描述:

我希望能够使用iTextSharp 4.2.0将图像大小调整为159x159的尺寸,但生成的图像需要准确指定尺寸。iTextSharp:如何调整图像大小以适应修正大小?

我已经试过这样:

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

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

例子: 我有这样的形象:

enter image description here

和结果图像看起来应该魔神此:

enter image description here

感谢。

您所描述的问题通常是当你试图直接通过调用AddCell(),这总是缩放以适应PdfPCell图像添加ImagePdfPTable会发生什么。所以,如果您要添加的图像到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); 
+0

这是一个非常有用的答案。谢谢。 – Emanuel 2012-02-23 15:14:43

+0

认真有用..谢谢。 – 2014-05-12 12:34:13

+1

还要小心不要通过'new PdfPCell().setImage(img)'添加图像,这会添加图像作为单元格的背景,它会自动缩放以适合单元格的宽度和高度。 – 2014-07-04 08:36:24

PdfPCell拥有财产,以适应在细胞图像,只需将它设置为true。

iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance("/test.png"); 

    PdfPCell logocell = new PdfPCell(logo,true); // **PdfPCell(Image,Boolean Fit)**