سؤال

I am pretty new in iText nd iTextSharp (the C# version of iText) and I have the following problem.

I am inserting some jpg charts inside my PDF. These charts are rapresented by some jpg like this one:

enter image description here

I insert it into a table of my PDF in this way:

iTextSharp.text.Image img = null;

..........................................
..........................................
..........................................
 if (currentVuln.UrgencyRating > 0)
                {
                    img = ChartHelper.GetPdfChartV2((int)currentVuln.UrgencyRating * 10, _folderImages);
                    vulnerabilityDetailsTable.AddCell(new PdfPCell(img) { Border = PdfPCell.RIGHT_BORDER, BorderColor = new BaseColor(79, 129, 189), BorderWidth = 1, Padding = 5, MinimumHeight = 30, PaddingTop = 10 });
                }

And this is a section of the GetPdfChartV2() method in which I load a chart immage:

    public static iTextSharp.text.Image GetPdfChartV2(int percentage, string _folderImmages)
    {
        iTextSharp.text.Image chart = null;
        string folderImmages = _folderImmages;

        if (percentage == 0)
        {
            return null;
        }
        else if (percentage == 10)
        {
            chart = iTextSharp.text.Image.GetInstance(_folderImmages + "1.jpg");
        }

        else if (percentage == 20)
        {
            chart = iTextSharp.text.Image.GetInstance(_folderImmages + "2.jpg");
        }
        ....................................................
        ....................................................
        ....................................................
        else if (percentage == 100)
        {
            chart = iTextSharp.text.Image.GetInstance(_folderImmages + "10.jpg");
        }

        return chart;
    }

}

The problem is that the chart immage is too big for my PDF and I obtain this orrible result:

enter image description here

So I have the following 2 questions:

1) Can I resize the iTextSharp.text.Image size into my code or have I to do it with an image editor? If is it possible where have I to do it? when I load the immage into GetPdfChartV2() by the lines as:

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

or when I put the immage into my PDF table cell:

vulnerabilityDetailsTable.AddCell(new PdfPCell(img) { Border = PdfPCell.RIGHT_BORDER, BorderColor = new BaseColor(79, 129, 189), BorderWidth = 1, Padding = 5, MinimumHeight = 30, PaddingTop = 10 });

Can you help me to solve this issue?

2) Why when I see the previous chart immage on my Windows Photo Viewer (100% of the size) I see it much smaller or here in the StackOverflow page?

هل كانت مفيدة؟

المحلول

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);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top