ItextSharp Help: converting html to pdf RRS feed

  • Question

  • User-1325905290 posted

    'Create Document class object and set its size to letter and give space left, right, Top, Bottom Margin



    Dim doc As New Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35)Try



    Dim wri As PdfWriter = PdfWriter.GetInstance(doc, New FileStream(Server.MapPath("~\Documents\Quotation.pdf"), FileMode.Create))'Open Document to write







    Dim paragraph As New Paragraph(msgText.ToString)Catch dex As DocumentException'Handle document exception



    Catch ioex As IOException'Handle IO exception



    Catch ex As Exception'Handle Other Exception




    'Close document



    End Try



    detsQuotations.Rows(0).Visible =

    New Net.Mail.Attachment(Server.MapPath("~\Documents\Quotation.pdf")))True

    Sunday, October 3, 2010 10:12 PM

All replies

  • User113288491 posted


    You can take help from:





    Monday, October 4, 2010 12:03 AM
  • User-1325905290 posted

    I have reviewed your options. Thanks very much for your help.

    It looks like I have to re-code the whole sub routine.

    I'm surprised that itext doesn't have the ability to render the html text directly into pdf unless the html text String is 

    coded in multiple chunks and paragraphs.

    I will have to do it the long way but if any one has any tip for me please share. 

    Monday, October 4, 2010 8:14 AM
  • User-1199946673 posted

    I'm surprised that itext doesn't have the ability to render the html text directly into pdf

    Where did you hear this? iTextSharp does have a build in HTML parser, although it is not very powerfull, it only supports a limited amount of tags. And why are you surprised if it was true? If you think is it easy to convert HTML into a pdf, create a pdf creator yourself...

    Imports iTextSharp.text
    Imports iTextSharp.text.html
    Imports System.IO


        Public Sub HTMLtoPDF(ByVal html As String)
            Dim ms As New MemoryStream()
            Dim document As New Document()
            Dim writer As PdfWriter = PdfWriter.GetInstance(document, ms)
            Dim reader As New StringReader(html)
            Dim parser As New simpleparser.HTMLWorker(document)
            Response.ContentType = "application/pdf"
            Response.AddHeader("content-disposition", "attachment;filename=file.pdf")
            Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length)
        End Sub


    Monday, October 4, 2010 2:18 PM
  • User-1325905290 posted

    Sorry Hans,

    It was not my intention to give Itextsharp a negative press. I was simply searching for a simple ways of solving a problem.

    Of course, I respect the fact that it is even free for all to share and use.

    Sorry if I offended you.

    Thanks for your effort any way.



    Monday, October 4, 2010 3:03 PM
  • User-1325905290 posted

    Hi Hans,

                It works like magic. You are a star!!


    Vriendelijk bedankt!


    Daniel Morkla 

    Monday, October 4, 2010 4:27 PM