locked
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

    doc.Open()

     

    doc.Add(paragraph)

    Response.Close()

     

     

    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

     

    Finally

     

    'Close document

    doc.Close()

     

    End Try

    msg.Attachments.Add(

    mailclient.Send(msg)

    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

    Hi,

    You can take help from:

    http://www.mikesdotnetting.com/Article/81/iTextSharp-Working-with-Fonts

    http://www.mikesdotnetting.com/Article/80/Create-PDFs-in-ASP.NET-getting-started-with-iTextSharp

    http://www.mikesdotnetting.com/Article/82/iTextSharp-Adding-Text-with-Chunks-Phrases-and-Paragraphs

    :)

    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)
            document.Open()
            Dim reader As New StringReader(html)
            Dim parser As New simpleparser.HTMLWorker(document)
            parser.Parse(reader)
            document.Close()
            Response.ContentType = "application/pdf"
            Response.AddHeader("content-disposition", "attachment;filename=file.pdf")
            Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length)
            Response.OutputStream.Flush()
        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.

    Regards,

    Daniel  

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

    Hi Hans,

                It works like magic. You are a star!!

     

    Vriendelijk bedankt!

    Regards,

    Daniel Morkla 

    Monday, October 4, 2010 4:27 PM