1

I have a html format string with data loaded from database along with image in the code behind file which i am binding it to aspx pages div from code behind as below

Dim pdfstring as string= "< html>< body>< table width='93%' border='0' cellspacing='0' cellpadding='0' style='margin:0px auto;'><tr><td>&nbsp;</td></tr><tr><td width=100%' align='left' style='padding:10px 20px; background: #005984; text-align:left;' > <img src='D:\Develop_Duke\ra\PortalDev\Portal\Assets\image\pdfimages\logo.png' border='0'/><br/></tr></table>< /body>< /html>"

Can you please let me know how to generate pdf format from the generated html string( i dont wnat to write this string to any .html file externally again) using ITextSharp in asp.net and also it should display the image with the path came in the html along with css.

Ramesh
  • 103
  • 1
  • 10
  • 1
    possible duplicate of [How to convert HTML to PDF using iTextSharp](http://stackoverflow.com/questions/25164257/how-to-convert-html-to-pdf-using-itextsharp) – Chris Haas Dec 03 '14 at 14:20

1 Answers1

1

You should take a look to online documentation, btw a common snippet to achieve your task is follow:

Using stream As New MemoryStream
          Using doc As New iTextSharp.text.Document(PageSize.A4, 0, 0, 0, 0)
Using writer As iTextSharp.text.pdf.PdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(doc, stream)

  writer.CloseStream = False
  Dim xmlWorker = XMLWorkerHelper.GetInstance()

  doc.Open()


  Dim _helperWiter As New StringWriter

  // the page to convert to PDF
  HttpContext.Current.Server.Execute(String.Format("../Reports/Templates/Page.aspx", _helperWiter)
  Using reader As New StringReader(_helperWiter.ToString())
    xmlWorker.ParseXHtml(writer, doc, reader)

  End Using
  doc.Close()
  writer.Close()
  stream.Close()

  Response.ContentType = "pdf/application"
  Response.AddHeader("content-disposition", String.Format("attachment;filename={0}.pdf", filemane))

  Response.BufferOutput = False
  Response.BinaryWrite(stream.ToArray())

  Response.Flush()
  Response.Close()
  Response.End()

    End Using
  End Using
End Using
ale
  • 10,012
  • 5
  • 40
  • 49
  • InvernoMuto , Do i need to write the pdfstring to any document before generation of pdf. I am confused where to call this pdfstring in your code. can you please make it clear by using that string – Ramesh Dec 02 '14 at 11:51
  • You have to obtain the string of your html page, but you can obtain it also from existent html page by Dim _helperWiter As New StringWriter and HttpContext.Current.Server.Execute – ale Dec 02 '14 at 11:53
  • cant we send the html string which is generated from code behind to pdf as i want to send only iframe part which is on aspx page to pdf and not the complete aspx page – Ramesh Dec 02 '14 at 12:59
  • you can generate the pdf by every well formatted html string you want, you have just to pass to ParseXHtml a StringReader populated as you want – ale Dec 02 '14 at 13:27
  • I am facing MyPageEventHandlerHelper and XMLWorkerHelper build errors which seems to be reference with a namespace, may i know what namespaces to use for these two? – Ramesh Dec 02 '14 at 14:16
  • you could remove MyPageEventHandlerHelper because it is necessary only for custom operations inside pdf(I will edit my answer), XMLWorkerHelper is inside iTextSharp.tool.xml namespace and you have to include itextsharp.xmlworker.dll, available via nuget – ale Dec 02 '14 at 14:34