itextsharp图像不会调整大小 - 占用大部分页面

时间:2013-09-27 14:31:15

标签: c# asp.net itextsharp

我有一个通过itextsharp放在PDF上的图像。当我使用ScalePercent或ScaleAbsoluteWidth或ScaleAbsoluteHeight时,它没有任何效果。在PDF上呈现的图像总是占用页面上可用的任何空间(它变得很大)。

这是我的代码。

var chartImage = new System.IO.MemoryStream();
Chart1.SaveImage(chartImage, ChartImageFormat.Png);
iTextSharp.text.Image imageForChart = iTextSharp.text.Image.GetInstance(chartImage.GetBuffer());

imageForChart.ScaleAbsoluteHeight(5f);
imageForChart.ScaleAbsoluteWidth(5f);

Dictionary<string, string> dictionary = new Dictionary<string, string>();
string pageTitle = "Event Recap Report";
dictionary.Add("Started", tbStartDate.Text);
dictionary.Add("Ended", tbEndDate.Text);
PDFDocument x = new PDFDocument(pageTitle, PageSize.LETTER, 15, 15, 40, 15);

x.UserName = _currentUser.FullName;
x.WritePageHeader(1, ref dictionary);
x.WriteImage(ref imageForChart);
x.WriteGrid(ref grdEventRecap);
x.WritePageFooter();
x.Finish(false);

3 个答案:

答案 0 :(得分:0)

您可以尝试像这样设置绝对位置:

var imageForChart = Image.GetInstance(chartImage.GetBuffer());
imageForChart.SetAbsolutePosition(positionX, positionY);
image.ScaleAbsoluteHeight(5f);
image.ScaleAbsoluteWidth(5f);

答案 1 :(得分:0)

希望这有帮助

string imagePath = Server.MapPath("~/Content/images");
iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance(imagePath + "/myimage.png");
png.ScaleToFit(520, 74);
doc1.Add(png);

使用ScaleToFit()调整图片大小。

答案 2 :(得分:0)

我很尴尬地说我发现我们实际上有一个围绕itextsharp的包装器,并且该包装器中的WriteImage方法正在缩放以适应。我的解决方案是添加一个新的方法,它不会自动缩放以适应。