2016-11-17 75 views
0

我有一個瀏覽器控件,它將與文本&重複等我看起來不錯,在我的瀏覽器控制。WebBrowser.DocumentText WPF中的屬性

現在我想給瀏覽器的內容一個字導出文件中可以導出爲PDF/HTML /文件等

這工作正常的Windows窗體。我用的是WebBrowser1.DocumentText屬性,並將其寫入文件等,這是命令:

(System.IO.File.WriteAllText(tempFileName, WebBrowser1.DocumentText) 

Unfortunaley的WebBrowser.DocumentText物業沒有在WPF更多可用。我怎麼解決這個問題 ?

Here's是形式的代碼:

Imports System.Drawing 
Imports Microsoft.Office.Interop.Word 
Imports System.Windows.Forms 
Imports System.IO 


Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click 
     ' save the document as html, mhtm (embedded), document or pdf 
     If dlgSaveDoc.ShowDialog = System.Windows.Forms.DialogResult.OK Then 
      ' create a temp file of the web browser document 
      Dim tempFileName As String 
      tempFileName = System.IO.Path.GetTempPath() & "\" & Now.ToString().Replace(":", "") & "_RepGen" & ".html" 
      System.IO.File.WriteAllText(tempFileName, WebBrowser1.DocumentText) 

      ' load temp file into word 
      Dim oWord As Microsoft.Office.Interop.Word.Application 
      Dim oDoc As Microsoft.Office.Interop.Word.Document 
      oWord = CreateObject("Word.Application") 
      Try 
       ' for debug, show word 
       oWord.Visible = True 

       oDoc = oWord.Documents.Open(tempFileName) 
       Try 
        ' depending on the file type (extension), set the save format 
        Dim sf As WdSaveFormat 
        Dim ext As String = Path.GetExtension(dlgSaveDoc.FileName).ToUpper() 
        If ext.StartsWith(".HTM") Then sf = WdSaveFormat.wdFormatHTML 
        If ext.StartsWith(".MHT") Then sf = WdSaveFormat.wdFormatWebArchive 
        If ext.StartsWith(".DOC") Then sf = WdSaveFormat.wdFormatXMLDocument 
        If ext.StartsWith(".PDF") Then sf = WdSaveFormat.wdFormatPDF 

        ' save the file from word 
        oDoc.SaveAs2(dlgSaveDoc.FileName, sf) 
       Finally 
        oDoc.Close() 
        oDoc = Nothing 
       End Try 
       oWord.Quit() 
      Finally 
       oWord = Nothing 
       ' cleanup 
       If File.Exists(tempFileName) Then File.Delete(tempFileName) 
      End Try 
     End If 
    End Sub 

回答

0

首先添加COM參考Microsoft HTML對象庫:

Screenshot of references

然後你就可以看看使用mshtml

Private Function GetPageContent(ByVal wb As WebBrowser) As String 
    Return DirectCast(wb.Document, mshtml.HTMLDocumentClass).body.innerHTML 
End Function 
1

這工作正常.. 。即使圖解等等都沒有問題:

我用於WPF:

Dim tempFileName As String = "E:test.doc 
Dim doc As Object = browser.Document 
Dim htmlText = doc.documentElement.InnerHtml 


System.IO.File.WriteAllText(tempFileName, htmlText) 

代替(爲的WinForms)

System.IO.File.WriteAllText(tempFileName, WebBrowser1.DocumentText)