简体   繁体   English

使用itextsharp在pdf上设置图像

[英]Set images on Pdf using itextsharp

I am working on WCF service to create a pad file and want to set image on created pdf. 我正在WCF服务上创建填充文件,并希望在创建的pdf上设置图像。 Below is my code. 下面是我的代码。 it gives me error "object reference not set to an object instance" 它给我错误“对象引用未设置为对象实例”

 string str = System.Web.HttpContext.Current.Request.MapPath("App_Data/suc.png");
 Image imgCheckBoxChecked = Image.GetInstance(str); 

The other thing I try and it gives me error :Could not find file 'C:\\Program Files\\Common Files\\Microsoft Shared\\DevServer\\10.0\\suc.png: Below is my other code 我尝试的另一件事,它给了我错误:找不到文件'C:\\ Program Files \\ Common Files \\ Microsoft Shared \\ DevServer \\ 10.0 \\ suc.png:下面是我的其他代码

  Image imgCheckBoxChecked = Image.GetInstance("App_Data/suc.png");

  cell.AddElement(imgCheckBoxChecked);
  cell.Colspan = 4;
  table.AddCell(cell);

Any idea on how to solve this error and set image on pdf. 关于如何解决此错误并在pdf上设置图像的任何想法。 Thanks 谢谢

您可以使用AppDomain.BaseDirectory来获取主dll的目录,此后,您可以使用它来获取映像dll的路径,例如Path.Combine(AppDomain.BaseDirectory, "App_Data\\\\suc.png") ,如果您ASP.NET中的主机服务,并且dll在Bin目录中,您可以使用相对路径,例如Path.Combine(AppDomain.BaseDirectory, "..\\\\App_Data\\\\suc.png")

string pdfPath = "~/PDF/File_1.pdf";
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(Server.MapPath("~/image.JPG"));
img.ScalePercent(100f);
Document doc = new Document();
PdfWriter.GetInstance(doc, new FileStream(Server.MapPath(pdfPath), FileMode.Create));
doc.Open();
doc.Add(new Paragraph(sb.ToString()));
doc.Add(img);
doc.Close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM