Image auto resizes in PdfPCell with iTextSharp

后端 未结 5 1360
南笙
南笙 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:13

    @Stewbob's answer does work, but it's only incidently related to the methods of the table.

    The thing with iTextSharp is that it will behave differently depending on which constructor you use. This will (annoyingly) scale up the image to fill the cell:

    PdfPCell c = new PdfPCell();
    c.Add(image);
    c.setHorizontalAlignment(Element.ALIGN_CENTER); // this will be ignored
    

    But this will leave the image at the size you set it (and allow for alignment):

    PdfPCell c = new PdfPCell(image);  
    c.setHorizontalAlignment(Element.ALIGN_CENTER);
    

    I don't know exactly why this is, it's got something to do with the cell being in 'text mode' if you add the image in the constructor versus 'composite mode' if you add it later (in which case each object is supposed to look after it's own alignment).

    Some more info (in Java, but still applies) http://tutorials.jenkov.com/java-itext/table.html#cell-modes

提交回复
热议问题