首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >WebBrowser,手柄pdf加载完成

WebBrowser,手柄pdf加载完成
EN

Stack Overflow用户
提问于 2013-09-10 21:23:00
回答 1查看 2.1K关注 0票数 0

我想知道是否有人知道让.pdf文件在加载时触发就绪状态的简单方法。我正在构建一个程序来打开url并拍摄截图,然后将它们放到excel中。

web浏览器将正确加载html文档,但在加载While Not pageready文件时会卡在.pdf中。浏览器控件正确地呈现.pdf

代码语言:javascript
运行
复制
Private Sub btngo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btngo.Click
    Dim file As String
    Dim Obj As New Object
    Dim result As String
    Dim sheet As String = "sheet1"
    Dim xlApp As New Excel.Application

    If lblpath.Text <> "" Then
        file = lblpath.Text
        Dim xlWorkBook = xlApp.Workbooks.Open(file)
        Dim xlWorkSheet = xlWorkBook.Worksheets(sheet)
        Dim range = xlWorkSheet.UsedRange

        ProgressBar1.Value = 0

        For rCnt = 4 To range.Rows.Count
            'url cell
            Obj = CType(range.Cells(rCnt, 2), Excel.Range)
            ' Obj.value now contains the value in the cell.. 
            Try
                ' Creates an HttpWebRequest with the specified URL. 
                Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(Obj.value), HttpWebRequest)
                ' Sends the request and waits for a response. 
                Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
                If myHttpWebResponse.StatusCode = HttpStatusCode.OK Then
                    result = myHttpWebResponse.StatusCode
                    WebBrowser1.ScrollBarsEnabled = False
                    WebBrowser1.Navigate(myHttpWebRequest.RequestUri)

                    WaitForPageLoad()

                    CaptureWebBrowser(WebBrowser1)
                End If
                ' Release the resources of the response.
                myHttpWebResponse.Close()

            Catch ex As WebException
                result = (ex.Message)
            Catch ex As Exception
                result = (ex.Message)
            End Try


            RichTextBox1.AppendText(result & "    " & Obj.value & vbNewLine)

            If radpre.Checked = True Then
                range.Cells(rCnt, 3).value = result
            ElseIf radcob.Checked = True Then
                range.Cells(rCnt, 4).value = result
            ElseIf radpost.Checked = True Then
                range.Cells(rCnt, 5).value = result

            End If


            ProgressBar1.Value = rCnt / range.Rows.Count * 100
        Next

        With xlApp
            .DisplayAlerts = False
            xlWorkBook.SaveAs(lblpath.Text.ToString)
            .DisplayAlerts = True
        End With

        xlWorkBook.Close()
        xlApp.Quit()

        'reclaim memory
        Marshal.ReleaseComObject(xlApp)
        xlApp = Nothing
    End If
End Sub

Private Function CaptureWebBrowser(ByVal wb As WebBrowser) As Image
    Try
        Dim hBitmap As Bitmap = New Bitmap(wb.Width, wb.Height)
        wb.DrawToBitmap(hBitmap, wb.Bounds)
        Dim img As Image = hBitmap
        Return img
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
    Return Nothing
End Function


Private Sub WaitForPageLoad()
    AddHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
    While Not pageready
        Application.DoEvents()
    End While
    pageready = False
End Sub

Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
    If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
        pageready = True
        RemoveHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
    End If
End Sub

更新到已解决

我对你的反馈很满意。我非常喜欢诺塞雷肖提供的答案。我不知道如何在最佳实践中使用代码模式。当打开.pdf或任何其他非基于web的文档时,readyState将不会从0中更改。看到这个程序对我来说仅仅是一种不工作的方式,我对只捕获.html.htm感到满意。

我的要求是

  1. 打开excel文档
  2. 解析位于excel文档中的链接
  3. 确定响应码
  4. 编写响应代码,并在可能的情况下截图到excel。

该程序分析和检索反馈的速度远远快于我能够手动完成的工作。.html.htm的屏幕截图提供了excel文件的非技术查看器,证明它们成功地从产品迁移到COB,并返回到生产环境。

诺塞雷肖声明的这段代码既不遵循最佳实践,也不符合高质量。这是一个快速而肮脏的实现。

代码语言:javascript
运行
复制
Option Infer On
Imports Microsoft.Office.Interop
Imports System.Net
Imports System.Runtime.InteropServices

Public Class Form1


Public Property pageready As Boolean

Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
    OpenFileDialog1.ShowDialog()
End Sub

Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
    lblpath.Text = OpenFileDialog1.FileName.ToString
End Sub

Private Sub btngo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btngo.Click
    Dim file As String
    Dim Obj As New Object
    Dim result As String
    Dim sheet As String = "sheet1"
    Dim xlApp As New Excel.Application
    Dim img As Bitmap
    Dim path As String = "C:\Documents and Settings\user\My Documents\Visual Studio 2010\Projects\COB-HTML-Tool\COB-HTML-Tool\bin\Debug\tmp.bmp"
    If lblpath.Text <> "" Then
        file = lblpath.Text
        Dim xlWorkBook = xlApp.Workbooks.Open(file)
        Dim xlWorkSheet = xlWorkBook.Worksheets(sheet)
        Dim range = xlWorkSheet.UsedRange

        ProgressBar1.Value = 0

        For rCnt = 4 To range.Rows.Count
            'url cell
            Obj = CType(range.Cells(rCnt, 2), Excel.Range)
            ' Obj.value now contains the value in the cell.. 
            Try
                ' Creates an HttpWebRequest with the specified URL. 
                Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(Obj.value), HttpWebRequest)
                ' Sends the request and waits for a response. 
                Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
                If myHttpWebResponse.StatusCode = HttpStatusCode.OK Then
                    result = myHttpWebResponse.StatusCode


                    Dim len As Integer = myHttpWebRequest.RequestUri.ToString.Length - 4
                    If myHttpWebRequest.RequestUri.ToString.Substring(len) = ".htm" Or
                        myHttpWebRequest.RequestUri.ToString.Substring(len - 1) = ".html" Or
                        myHttpWebRequest.RequestUri.ToString.Substring(len) = ".asp" Then
                        WebBrowser1.Navigate(myHttpWebRequest.RequestUri)
                        WaitForPageLoad()

                        img = CaptureWebBrowser(WebBrowser1)
                        img.Save(path)
                    End If

                End If
    ' Release the resources of the response.
    myHttpWebResponse.Close()

            Catch ex As WebException
        result = (ex.Message)
    Catch ex As Exception
        result = (ex.Message)
    End Try


            RichTextBox1.AppendText(result & "    " & Obj.value & vbNewLine)

            If radpre.Checked = True Then
                range.Cells(rCnt, 3).value = result

                If img Is Nothing Then
                Else
                    If Dir(path) <> "" Then
                        range.Cells(rCnt, 4).Select()
                        Dim opicture As Object
                        opicture = xlApp.ActiveSheet.Pictures.Insert(path)
                        opicture.ShapeRange.LockAspectRatio = True
                        opicture.ShapeRange.width = 170
                        opicture.ShapeRange.height = 170
                        My.Computer.FileSystem.DeleteFile(path)

                    End If
                End If
            ElseIf radcob.Checked = True Then
                range.Cells(rCnt, 5).value = result
                If img Is Nothing Then
                Else
                    If Dir(path) <> "" Then
                        range.Cells(rCnt, 6).Select()
                        Dim opicture As Object
                        opicture = xlApp.ActiveSheet.Pictures.Insert(path)
                        opicture.ShapeRange.LockAspectRatio = True
                        opicture.ShapeRange.width = 170
                        opicture.ShapeRange.height = 170
                        My.Computer.FileSystem.DeleteFile(path)
                    End If
                End If
            ElseIf radpost.Checked = True Then
                range.Cells(rCnt, 7).value = result
                If img Is Nothing Then
                Else
                    If Dir(path) <> "" Then
                        range.Cells(rCnt, 8).Select()
                        Dim opicture As Object
                        opicture = xlApp.ActiveSheet.Pictures.Insert(path)
                        opicture.ShapeRange.LockAspectRatio = True
                        opicture.ShapeRange.width = 170
                        opicture.ShapeRange.height = 170
                        My.Computer.FileSystem.DeleteFile(path)
                    End If
                End If
            End If


            ProgressBar1.Value = rCnt / range.Rows.Count * 100
        Next

        With xlApp
            .DisplayAlerts = False
            xlWorkBook.SaveAs(lblpath.Text.ToString)
            .DisplayAlerts = True
        End With

        xlWorkBook.Close()
        xlApp.Quit()

        'reclaim memory
        Marshal.ReleaseComObject(xlApp)
        xlApp = Nothing
    End If
End Sub
Private Function CaptureWebBrowser(ByVal wb As WebBrowser) As Image

    Try
        wb.ScrollBarsEnabled = False
        Dim hBitmap As Bitmap = New Bitmap(wb.Width, wb.Height)
        wb.DrawToBitmap(hBitmap, wb.Bounds)
        Dim img As Image = hBitmap
        Return img
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
    Return Nothing
End Function


Private Sub WaitForPageLoad()
    AddHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
    While Not pageready
        Application.DoEvents()
        System.Threading.Thread.Sleep(200)
    End While
    pageready = False
End Sub

Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
    If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
        pageready = True
        RemoveHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
    End If
End Sub


End Class
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-11 06:57:19

不幸的是,您将无法使用webBrowser.DrawToBitmap获取PDF视图的快照。在编写本文时, Acrobat控件不支持在自定义设备上下文上呈现,因此此方法无法工作,也不能发送WM_PRINT或调用IViewObject::Draw,无论是在via WebBrowser上的Reader对象上(我尝试过,还有我不孤独)。正确的解决方案是使用第三方PDF渲染组件。

另外,您应该避免使用这样的代码模式:

代码语言:javascript
运行
复制
While Not pageready
    Application.DoEvents()
End While

这是一个忙着等待紧循环,消耗CPU周期徒劳无功。至少,在循环中放一些Thread.Sleep(200),但总的来说,您也应该避免使用使用Application.DoEvents

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18728962

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档