简体   繁体   中英

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. 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

  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. 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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