iTextSharp的和图像尺寸 [英] itextsharp and images sizes
问题描述
我在单独的细胞中的表创建具有6幅图像的单个PDF页面,即使我设置在服务器侧的图像的高度和宽度正好与ScaleToFit同一图像大小不pdf的页面上的同一
反正是有得到所有的图像完全相同的大小?
PdfPTable表=新PdfPTable(3);
table.HorizontalAlignment = Element.ALIGN_CENTER;
table.WidthPercentage = 100;
table.TotalWidth = 698.5f;
table.LockedWidth = TRUE;
table.SetWidths(新浮法[] {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 =新iTextSharp.text.pdf.PdfPCell(IMG1);
imgCell1.HorizontalAlignment = Element.ALIGN_CENTER;
imgCell1.BackgroundColor =新BaseColor(255,255,255);
imgCell1.Border = iTextSharp.text.Rectangle.NO_BORDER;
table.AddCell(imgCell1);
两件事情。
首先,看看这篇文章了解包裹在
。基本上是:块图像
iTextSharp.text.pdf.PdfPCell imgCell1 =新iTextSharp.text.pdf.PdfPCell();
imgCell1.AddElement(新块(IMG1,0,0));
第二,如果你想在的确切的同样的大小,然后你想使用 ScaleAbsolute
而不是 ScaleToFit
。后者保持图像的显示比例,使一个100×200的图像缩放到适合的50x50会出来为25x50。
img1.ScaleAbsolute (120F,155.25f);
I am creating a single pdf page with 6 images in a table in separate cells, even though I am setting the images height and width on the server side exactly the same with ScaleToFit the images sizes are not the same on the pdf page.
Is there anyway to get all the images the exact same size?
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);
Two things.
First, see this post about wrapping the Image
in a Chunk
. Basically:
iTextSharp.text.pdf.PdfPCell imgCell1 = new iTextSharp.text.pdf.PdfPCell();
imgCell1.AddElement(new Chunk(img1, 0, 0));
Second, if you want the exact same size then you want to use ScaleAbsolute
instead of ScaleToFit
. The latter keeps the aspect ratio of the image so a 100x200 image scaled to fit 50x50 would come out as 25x50.
img1.ScaleAbsolute(120f, 155.25f);
这篇关于iTextSharp的和图像尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!