Image auto resizes in PdfPCell with iTextSharp

后端 未结 5 1351
南笙
南笙 2021-02-20 10:34

I\'m having a weird problem with images in iTextSharp library. I\'m adding the image to the PdfPCell and for some reason it gets scaled up. How do i keep it to original size?

5条回答
  •  离开以前
    2021-02-20 11:22

    So if you have to mantain the size of the image in the PdfPCell you can loock at this code :

                    iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imageFilePath);
    
                     // Save the image width
                    float width = image.Width;
                    PdfPCell cell = new PdfPCell();
                    cell.AddElement(image);
    
    
                    // Now find the Image element in the cell and resize it
                    foreach (IElement element in cell.CompositeElements)
                    {
                        // The inserted image is stored in a PdfPTable, so when you find 
                        // the table element just set the table width with the image width, and lock it.
                        PdfPTable tblImg = element as PdfPTable;
                        if (tblImg != null)
                        {
                            tblImg.TotalWidth = width;
                            tblImg.LockedWidth = true;
                        }
                    }
    

提交回复
热议问题