我需要您的帮助将图像添加到PDF。如何使用C#和iTextSharp在pdf中添加图像?
我使用:
string imgPath2 = localPath + "\\TempChartImages\\" + LegendPath;
img2.Save(imgPath2);
ith.WriteImage(imgPath2, 80);
但这个代码给我的错误:
Use of unassigned local variable img2
我怎样才能解决这个问题?
我需要您的帮助将图像添加到PDF。如何使用C#和iTextSharp在pdf中添加图像?
我使用:
string imgPath2 = localPath + "\\TempChartImages\\" + LegendPath;
img2.Save(imgPath2);
ith.WriteImage(imgPath2, 80);
但这个代码给我的错误:
Use of unassigned local variable img2
我怎样才能解决这个问题?
您需要一些第三方工具。
这是关于images的iTextSharp教程。如果没有看到更多的代码,就很难判断你需要哪些代码。
当你声明一个变量,在你的情况下img2,没有分配一个值,它指向什么都没有。确保你在使用它之前初始化img2。
我想你想你的img2.Save
线改变什么:
Image img2 = Image.FromFile(yourInitialImageHere); // You could be reading from memory as well.
img2.Save(imgPath2);
我可能是遥远,仿佛你的代码片断是相当模糊的。
这是一种预感,但是如果您在Try-Catch块内分配img2
的值,您可能会遇到妨碍分配发生的异常。例如:
var img2;
try
{
var x = 5/0; // Generate a DivideByZero exception
img2 = GetImage(); // <-- the above exception will prevent this code from executing
}
catch
{
}
img2.Save(imgPath2); <-- img2 wasn't assigned, so another exception will occur
我相信你必须首先实例化图像。
Image img2 = new Image();
它解决了我的问题。希望它能解决你的问题。
你必须创建一个图像的getinstance。
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance("path of the image");
我不确定你的代码是干什么的(因为它不是很清晰),但你是否想要添加图片到PDF?如果是这样,请使用iTextSharp。 – David 2009-06-18 13:54:21
你能发布定义img2的代码吗?您将需要一个库来执行PDF中的插入操作,因为在.NET框架中没有任何操作PDF的操作。 – Kelsey 2009-06-18 22:49:13