2013-06-19 63 views
0

我试图用VB.NET和iTextSharp将网页转换为PDF。我尝试了许多不同的例子,似乎没有任何工作(至少对我来说)。这是我得到的最接近的,但XMLWorkerHelper总是抛出异常Invalid nested tag head found, expected closing tag script,无论分析哪个网页。找到无效的嵌套标签头

Dim webClient As New System.Net.WebClient  
Dim result As String = webClient.DownloadString("http://google.com")   
Dim doc As New Document(PageSize.A4) 
Dim writer As PdfWriter = PdfWriter.GetInstance(doc, New System.IO.FileStream("c:\test.pdf", System.IO.FileMode.Create)) 
Dim sr As New System.IO.StringReader(result) 
Try 
    XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, sr) 
Catch ex As Exception 

End Try 

我使用iTextSharp的5.4.2.0和.NET Framework 4

感谢

回答

1

使用

var HtmlNode = new HtmlAgilityPack.HtmlDocument(); 
HtmlNode.OptionOutputAsXml = true; 
HtmlNode.Save(your directory); // save a new HTML file converted 

然后在新的HTML转换。