c# – itextsharp和图像大小

前端之家收集整理的这篇文章主要介绍了c# – itextsharp和图像大小前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在单独的单元格中创建了一个包含6个图像的单个pdf页面,即使我在服务器端设置的图像高度和宽度与ScaleToFit完全相同,但是在pdf页面上的图像大小也不相同.

反正有没有让所有图像完全相同的尺寸?

  1. PdfPTable table = new PdfPTable(3);
  2. table.HorizontalAlignment = Element.ALIGN_CENTER;
  3. table.WidthPercentage = 100;
  4. table.TotalWidth = 698.5f;
  5. table.LockedWidth = true;
  6. table.SetWidths(new float [] {1,1,1});
  7. iTextSharp.text.Image img1 = iTextSharp.text.Image.GetInstance("C:\\Users\\DaNet\\Downloads\\image.jpg");
  8. img1.Alignment = iTextSharp.text.Image.ALIGN_CENTER;
  9. img1.ScaleToFit(120f,155.25f);
  10.  
  11. iTextSharp.text.pdf.PdfPCell imgCell1 = new iTextSharp.text.pdf.PdfPCell(img1);
  12. imgCell1.HorizontalAlignment = Element.ALIGN_CENTER;
  13. imgCell1.BackgroundColor = new BaseColor(255,255,255);
  14. imgCell1.Border = iTextSharp.text.Rectangle.NO_BORDER;
  15. table.AddCell(imgCell1);

解决方法

两件事情.

首先,请参阅this post关于将图像包装在块中.基本上:

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

其次,如果你想要完全相同的大小,那么你想使用ScaleAbsolute而不是ScaleToFit.后者保持图像的纵横比,因此缩放到50×50的100×200图像将显示为25×50.

  1. img1.ScaleAbsolute(120f,155.25f);

猜你在找的C#相关文章