C# 如何在我的代码中调整iTextSharp.text.Image大小?
我对iTextndiTextSharp(iText的C#版本)相当陌生,我有以下问题 我正在我的PDF中插入一些jpg图表。这些图表受到一些jpg的青睐,比如: 我以以下方式将其插入我的PDF表格:C# 如何在我的代码中调整iTextSharp.text.Image大小?,c#,pdf,pdf-generation,itextsharp,itext,C#,Pdf,Pdf Generation,Itextsharp,Itext,我对iTextndiTextSharp(iText的C#版本)相当陌生,我有以下问题 我正在我的PDF中插入一些jpg图表。这些图表受到一些jpg的青睐,比如: 我以以下方式将其插入我的PDF表格: iTextSharp.text.Image img = null; .......................................... .......................................... ............................
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 });
}
chart = iTextSharp.text.Image.GetInstance(_folderImmages + "1.jpg");
chart .ScalePercent(24f);
这是GetPdfChartV2()方法的一部分,我在其中加载了一个图表:
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来说太大了,我得到了一个不好的结果:
因此,我有以下两个问题:
1) 我可以将iTextSharp.text.Image的大小调整到我的代码中,还是必须使用图像编辑器?
如果可能的话,我该在哪里做呢?当我通过以下行将IMAGE加载到GetPdfChartV2()中时:
chart = iTextSharp.text.Image.GetInstance(_folderImmages + "1.jpg");
或者当我将IMAGE放入PDF表格单元格时:
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照片查看器(100%大小)上看到上一张图表时,我会看到它要小得多,或者在StackOverflow页面中看到它?我自己这样解决:
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 });
}
chart = iTextSharp.text.Image.GetInstance(_folderImmages + "1.jpg");
chart .ScalePercent(24f);
更好地解释如下:将
图像添加到PdfPCell
有不同的策略。这些策略在的第4章中进行了解释,并演示了所有可能的选项。如果你不懂Java,你会发现第4章示例中的C#端口
您使用的是:
// we wrap he image in a PdfPCell
PdfPCell cell = new PdfPCell(img[0]);
table.AddCell(cell);
如文档所述,此选项不会缩放图像(这是您想要的)。如果要缩放图像,可以使用以下方法:
// we wrap the image in a PdfPCell and let iText scale it
cell = new PdfPCell(img[1], true);
table.AddCell(cell);
通过添加布尔参数true
,可以要求iText缩放图像
另一个选项是像这样使用addCell()
:
// we add the image with addCell()
table.AddCell(img[2]);
这也将缩放图像,但使用默认单元格的属性。如果不更改这些属性,将有2个用户单位的填充
您还可以选择使用复合模式:
这将确保图像缩放到单元格宽度的100%,除非更改图像的宽度百分比,例如:
img[3].WidthPercentage = 50;
此行将确保图像宽度为单元格可用宽度的50%
最后,您可以在将图像添加到单元格之前缩放图像,如您自己(不完整)的回答中所述
在5个可能的选项中,您选择了一个不缩放图像的选项;-) 在Java中,我可以通过以下方法调整单元格中图像的大小:
Image image = Image.getInstance(fileLocation);
image.scalePercent((float)7.5);
PdfPCell imageCell = new PdfPCell(image,false);
Tnx,现在我来给你举个例子