1
votes

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.

1

1 Answers

1
votes

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