关于c#:如何将iTextSharp.text.Image大小调整为我的代码?

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

我在iText和iTextSharp(iText的C#版本)中还很陌生,并且遇到以下问题。

我在PDF内插入了一些jpg图表。这些图表由这样的jpg呈现:

enter image description here

我以这种方式将其插入到PDF表中:

1
2
3
4
5
6
7
8
9
10
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 });
                }

这是GetPdfChartV2()方法的一部分,在其中我加载了图表图像:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    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;
    }

}

问题是图表的尺寸对于我的PDF太大,我得到了这个糟糕的结果:

enter image description here

所以我有以下两个问题:

1)我可以将iTextSharp.text.Image大小调整为代码大小还是使用图像编辑器进行调整?
如果可以在哪里做?当我通过以下行将图像加载到GetPdfChartV2()中时:

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

或将图片放入PDF表格单元格中时:

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

您能帮我解决这个问题吗?

2)为什么当我在Windows Photo Viewer中看到前一个图表尺寸(尺寸的100%)时,却看到它很小或者在StackOverflow页面上看到了?


Image添加到PdfPCell的策略多种多样。这些策略在我的书的第4章中进行了解释,XMen示例演示了所有可能的选择。如果您不懂Java,则可以在这里找到第4章示例的C#端口。

您正在使用此:

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

如记录所示,此选项不会缩放图像(这是您想要的)。如果要缩放图像,可以使用以下方法:

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

通过添加布尔参数true,您要求iText缩放图像。

另一种选择是像这样使用addCell()

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

这也将缩放图像,但使用默认单元格的属性。如果您不更改这些属性,则将填充2个用户单元。

您还可以选择使用复合模式:

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

这将确保缩放图像以填充单元格宽度的100%,除非您更改图像的宽度百分比,例如:

1
img[3].WidthPercentage = 50;

这条线将确保图像的宽度是单元格的可用宽度的50%。

最后,您可以缩放图像,然后再将其添加到单元格中,如您自己的(不完整)答案中所述。

在5个可能的选项中,您选择了不缩放图像的单个选项;-)


在Java中,我可以使用这种方法来调整单元格中图像的大小:

1
2
3
    Image image = Image.getInstance(fileLocation);
    image.scalePercent((float)7.5);
    PdfPCell imageCell = new PdfPCell(image,false);

我自己这样解决的:

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

作为更好的解释在这里:http://www.mikesdotnetting.com/Article/87/iTextSharp-Working-with-images