How to resize an iTextSharp.text.Image size into my code?

爷,独闯天下 提交于 2019-12-01 08:04:26

There are different strategies for adding an Image to a PdfPCell. These strategies are explained in chapter 4 of my book, and the XMen example demonstrates all possible options. If you don't understand Java, you'll find the C# port of the examples of chapter 4 here.

You are using this:

// we wrap he image in a PdfPCell
PdfPCell cell = new PdfPCell(img[0]);
table.AddCell(cell);

As documented, this option doesn't scale the image (which is what you want). If you want to scale the image, you could use this:

// we wrap the image in a PdfPCell and let iText scale it
cell = new PdfPCell(img[1], true);
table.AddCell(cell);

By adding the boolean parameter true, you ask iText to scale the image.

Another option is to use addCell() like this:

// we add the image with addCell()
table.AddCell(img[2]);

This will also scale the image, but use the properties of the default cell. If you don't change these properties, there will be a padding of 2 user units.

You also have the option to use composite mode:

cell = new PdfPCell();
cell.AddElement(img[3]);
table.AddCell(cell);

This will make sure the image is scaled to fill 100 percent of the cell width, unless you change the width percentage of the image, for instance:

img[3].WidthPercentage = 50;

This line will make sure that the width of the image is 50% of the available width of the cell.

Finally, you can scale the image before adding it to the cell as explained in your own (incomplete) answer.

Out of 5 possible options, you picked the single option that doesn't scale the image ;-)

Solved by myself in this way:

chart = iTextSharp.text.Image.GetInstance(_folderImmages + "1.jpg");
chart .ScalePercent(24f);

As better explainet here: http://www.mikesdotnetting.com/Article/87/iTextSharp-Working-with-images

In Java, I was able to resize an image in a cell with this approach:

    Image image = Image.getInstance(fileLocation);
    image.scalePercent((float)7.5);
    PdfPCell imageCell = new PdfPCell(image,false);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!