Image resizes when inserted in PdfCell

淺唱寂寞╮ 提交于 2019-12-11 00:29:13

问题


I'm trying to create Pdf using itextsharp. I have added one table conataining two columns one containing text and other image. I want to have constant image size

  1. My Image automatically resizes if the text present in another cell increases and image present in other cell has different sizes

      for (int i = 0; i < visitInfo.VisitsiteComplience.Count; ++i)
        {
    
            cellprop.Colspan = 1;
            cellprop.Pharse = visitInfo.VisitsiteComplience[i].Compliencedescription;
            cellprop.BaseColor = null;
            table.AddCell(AddCelltoTable(cellprop));
            yesicon.ScaleAbsolute(35f, 35f);
            noicon.ScaleAbsolute(35f, 35f);
    
            if (visitInfo.VisitsiteComplience[i].Status == "1")
            {
    
                statuscell.AddElement(new Chunk(noicon, 0, 0));
    
            }
            else
            {
    
               // statuscell.AddElement(new Chunk(noicon, 0, 0));
            }
    
    
           statuscell.FixedHeight = 10;
    
    
            //headerLeftCell.Border = PdfPCell.NO_BORDER;
            table.AddCell(statuscell);
        }
    

2. Then I changed the code but now Image size increases and occupies full cell
     for (int i = 0; i < visitInfo.VisitsiteComplience.Count; ++i)
        {

            cellprop.Colspan = 1;
            cellprop.Pharse = visitInfo.VisitsiteComplience[i].Compliencedescription;
            cellprop.BaseColor = null;
            table.AddCell(AddCelltoTable(cellprop));
            yesicon.ScaleAbsolute(35f, 35f);
            noicon.ScaleAbsolute(35f, 35f);

            if (visitInfo.VisitsiteComplience[i].Status == "1")
            {

                statuscell.AddElement(new Chunk(noicon, 0, 0));

            }
            else
            {

               // statuscell.AddElement(new Chunk(noicon, 0, 0));
            }





            //headerLeftCell.Border = PdfPCell.NO_BORDER;
            table.AddCell(statuscell);
        }


回答1:


I think you're scaling the image yourself like this: noicon.ScaleAbsolute(35f, 35f);

It also puzzles me why you're wrapping the image inside a Chunk. You can create a PdfPCell that takes an Image as parameter as well as a Bool to defines whether or not iText should scale the Image. See page 109 of the book iText in Action (of which I'm the author) and take a look at the XMen example of chapter 4.




回答2:


Image image = Image.getInstance("D:/star.png");

PdfPCell cell = new PdfPCell();

cell.setFixedHeight(40f);

cell.addElement(image);

table.addCell(cell);


来源:https://stackoverflow.com/questions/15741980/image-resizes-when-inserted-in-pdfcell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!