问题描述
我用这个code。使用iTextSharp的5.0.6到我的aspx页面转换为PDF格式:
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)
如果我从变量HTML1在此行中code的改变为HTML,它工作正常。
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
感谢您的帮助。
推荐答案
(评论搬到接听)
iTextSharp的的 HTMLWorker
已经知道问题&LT;小时&GT;
标记。短期的解决方案是只删除这些标签,但长期的解决方案是切换到 XMLWorker
支持这些标签,并且正在开发和维护。
iTextSharp's HTMLWorker
has known problems with <hr>
tags. The short term solution is to just remove those tags but the long term solution is to switch to XMLWorker
which supports those tags and is actively being developed and maintained.
这篇关于错误转换aspx页面为HTML(iTextSharp的5.0.6)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!