2011-11-22 127 views
5

我创建了单独的单元格中的表格中的6个图像,即使我设置图像的高度和宽度在服务器端完全相同与ScaleToFit图像大小是不一样的在pdf页面上。itextsharp和图像大小

有无论如何获得所有的图像完全相同的大小?

PdfPTable table = new PdfPTable(3); 
table.HorizontalAlignment = Element.ALIGN_CENTER; 
table.WidthPercentage = 100; 
table.TotalWidth = 698.5f; 
table.LockedWidth = true; 
table.SetWidths(new float [] {1,1,1}); 
iTextSharp.text.Image img1 = iTextSharp.text.Image.GetInstance("C:\\Users\\DaNet\\Downloads\\image.jpg"); 
img1.Alignment = iTextSharp.text.Image.ALIGN_CENTER; 
img1.ScaleToFit(120f, 155.25f); 

iTextSharp.text.pdf.PdfPCell imgCell1 = new iTextSharp.text.pdf.PdfPCell(img1); 
imgCell1.HorizontalAlignment = Element.ALIGN_CENTER; 
imgCell1.BackgroundColor = new BaseColor(255, 255, 255); 
imgCell1.Border = iTextSharp.text.Rectangle.NO_BORDER; 
table.AddCell(imgCell1); 

回答

14

两件事。

首先,请参阅this post关于将Image包装在Chunk中。基本上是:

iTextSharp.text.pdf.PdfPCell imgCell1 = new iTextSharp.text.pdf.PdfPCell(); 
imgCell1.AddElement(new Chunk(img1, 0, 0)); 

第二,如果你想确切相同的大小,那么你要使用ScaleAbsolute而不是ScaleToFit。后者保持图像的高宽比,因此100x200图像缩放到50x50的比例将为25x50。

img1.ScaleAbsolute(120f, 155.25f); 
+0

它看起来很喜欢图像是适当的大小,现在谢谢!现在我必须将它定位在细胞中,即使它是细胞的一个元素。最后,我认为这不是我想要做的最好的选择。 – DaNet

+0

只需使用img1.ScaleAbsolute(120f,155.25f);图像完美贴合细胞。干杯克里斯! – DaNet

+1

如果我想要我的图像35x35毫米,那么我该怎么办?如何计算它在毫米? –