Software Technology Tips

The following code snippet demonstrates how to convert a html file into PDF format using iText library in VB.NET.
First we need to add reference to iTextSharp library.
' Add references
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf

' Read html file to a string
 Dim sr As StreamReader = New StreamReader("YOUR HTML FILE PATH WIIH NAME")
 Dim line As String
 line = sr.ReadToEnd
' Code to convert to pdf
  Dim doc As New Document(PageSize.LETTER, 80, 50, 30, 65)
  Dim fsNew As New StringReader(line)
  Dim document As New Document(PageSize.A4, 80, 50, 30, 65)
        Using fs As New FileStream("YOUR PDF FILE PATH WITH NAME", FileMode.Create)
            PdfWriter.GetInstance(document, fs)
            Using stringReader As New StringReader(line)
                Dim parsedList As System.Collections.Generic.List(Of IElement) = html.simpleparser.HTMLWorker.ParseToList(stringReader, Nothing)
                ' parse each html object and add it to the pdf document
                For Each item As Object In parsedList
                    document.Add(DirectCast(item, IElement))
            End Using
        End Using


Related Tags:

HTML, PDF, Conversion, iTextSharp

Author: Amrita Dash


Let us Connect!

Awards and Achievements

Red Herringcolor DeloitteFast50 DB ZInnov1   Nascome  DB Stpi

This site uses cookies. We respect your privacy.copyright (c) Mindfire Solutions 2007-2015. Login