dan_vitch dan_vitch - 2 months ago 27
C# Question

Add web image to pdf

I am trying to add a web image to a pdf using iTextSharp

I am attempting to use the same code that you would use for a local image

using (var pdfDoc = new Document())
using (var pdfWriter = PdfWriter.GetInstance(pdfDoc, pdfStream)) {
pdfDoc.Open();
Image tif = Image.GetInstance("www.myimage.com");
pdfDoc.Add(tif);
pdfDoc.Close();
}

Answer

Contrary to the other posted answer, you do NOT need to make a separate HTTP request. Just make sure you use an absolute URI when calling Image.GetInstance():

string url = "http://upload.wikimedia.org/wikipedia/commons/thumb/8/8f/Pollinationn.jpg/320px-Pollinationn.jpg";
using (Document document = new Document()) {
  PdfWriter.GetInstance(document, Response.OutputStream);
  document.Open();
  Image img = Image.GetInstance(url);
  document.Add(img);
}

This is clearly documented for the overloaded method call.