I use this code to convert my aspx page to pdf using itextsharp 5.0.6:
Using ms = New MemoryStream()
Dim Html As String = vbCr & vbLf & "<h1>h1</h1>" & vbCr & vbLf & "<p class=""bo"">A paragraph</p> " & vbCr & vbLf & "<ul> " & vbCr & vbLf & "<li>one</li> " & vbCr & vbLf & "<li>two</li> " & vbCr & vbLf & "<li>three</li> " & vbCr & vbLf & "</ul>"
Dim Html1 As String = RenderControlToString(Page)
Dim styles As New StyleSheet()
styles.LoadStyle("bo", "size", "10")
styles.LoadTagStyle(HtmlTags.H1, HtmlTags.FONT, "59")
styles.LoadTagStyle(HtmlTags.H1, HtmlTags.COLOR, "#ff0000")
styles.LoadTagStyle(HtmlTags.UL, HtmlTags.INDENT, "10")
styles.LoadTagStyle(HtmlTags.LI, HtmlTags.LEADING, "16")
Using document As New Document(PageSize.A4, 10.0F, 10.0F, 100.0F, 0.0F)
document.SetPageSize(iTextSharp.text.PageSize.A4.Rotate())
PdfWriter.GetInstance(document, ms)
document.Open()
document.Add(New Paragraph("this is atest"))
document.Add(New Paragraph("this is a test"))
Dim strB As New StringBuilder(Html1)
Using sReader As TextReader = New StringReader(Html1.ToString())
Dim list As List(Of IElement) = HTMLWorker.ParseToList(sReader, styles)
For Each elm As IElement In list
document.Add(elm)
Next
End Using
End Using
End Using
However I kept getting error on this line saying object reference not set to an instance of an object:
Dim list As List(Of IElement) = HTMLWorker.ParseToList(sReader, styles)
If I changed from variable Html1 to Html in this line of code, it is working fine.
Using sReader As TextReader = New StringReader(Html1.ToString())
Any idea how I can fix this error? Here is the function:
Private Function RenderControlToString(control As Control) As String
Dim sb As New StringBuilder()
Dim sw As New StringWriter(sb)
Dim writer As New HtmlTextWriter(sw)
control.RenderControl(writer)
Return sb.ToString()
End Function
Thanks for your help.
` in it by chance? – Chris Haas Apr 28 '14 at 01:10
. Is it the reason? Thanks – ryan Apr 28 '14 at 04:01
and I removed it. Now it is working fine. I would love to vote up your answer but there is no place to do that. – ryan Apr 30 '14 at 03:58