vb.net 从 webbrowser 控件保存 pdf 文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16560602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:35:52  来源:igfitidea点击:

Saving pdf document from webbrowser control

vb.netwebbrowser-control

提问by Mtok

I'm navigating from webbrowser control to an url like this; http://www.who.int/cancer/modules/Team%20building.pdf

我正在从 webbrowser 控件导航到这样的 url; http://www.who.int/cancer/modules/Team%20building.pdf

It's shown in webbrowser control. What I want to do is to download this pdf file to computer. But I tried many ways;

它显示在网络浏览器控件中。我想要做的是将这个pdf文件下载到电脑上。但是我尝试了很多方法;

Dim filepath As String
filepath = "D:\temp1.pdf"
Dim client As WebClient = New WebClient()
client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
client.DownloadFileAsync(WebBrowserEx1.Url, filepath)

This one downloads a pdf but there is nothing in the file.

这个下载了一个pdf,但文件中没有任何内容。

Also tried with

也试过

objWebClient.DownloadFile()

nothing changed.

没有改变。

I tried to show a save or print dialog;

我试图显示一个保存或打印对话框;

WebBrowserEx1.ShowSaveAsDialog()
WebBrowserEx1.ShowPrintDialog()

but they didnt show any dialog. Maybe the last one is because it doesnt wait to load the the pdf into webbrowser completely.

但他们没有显示任何对话框。也许最后一个是因为它没有等待将 pdf 完全加载到 webbrowser 中。

When I try html files there is no problem to dowload, but in this .pdffile, I think I didn't manage to wait the file to be loaded as pdf into browser. This function(s);

当我尝试 html 文件时,下载没有问题,但在这个.pdf文件中,我想我没有设法等待文件作为 pdf 加载到浏览器中。此功能;

 Private Sub WaitForPageLoad(ByVal adimno As String)
    If adimno = "1" Then
        AddHandler WebBrowserEx1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
        While Not pageReady
            Application.DoEvents()
        End While
        pageReady = False
    End If

End Sub

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

are not working for this situation. I mean it gets into infinite loop.

不适用于这种情况。我的意思是它进入无限循环。

So anyone knows how to wait this to load pdf then save into computer.

所以任何人都知道如何等待它加载pdf然后保存到计算机中。

回答by Jason Bayldon

you could test the URL when document completed fires and if its .pdf, then do the following then navigate back, for example.

例如,您可以在文档完成时测试 URL,如果它是 .pdf,则执行以下操作然后返回。

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    WebBrowserEx1.Navigate("http://www.who.int/cancer/modules/Team%20building.pdf")
End Sub

Private Sub WebBrowserEx1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowserEx1.DocumentCompleted

    If WebBrowserEx1.Url.ToString.Contains(".pdf") Then

        Using webClient = New WebClient()
            Dim bytes = webClient.DownloadData(WebBrowserEx1.Url.ToString) 'again variable here

            File.WriteAllBytes(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "TEST.pdf"), bytes) 'save to desktop or specialfolder. to list all the readily available user folders
        End Using

 'WebBrowserEx1.goback() 'could send browser back a page as well

    End If



End Sub

You will need to make the filename "TEST" as a variable instead of a static string or else you will overwrite the same file each time. Perhaps:

您需要将文件名“TEST”作为变量而不是静态字符串,否则每次都会覆盖同一个文件。也许:

 WebBrowserEx1.DocumentTitle.ToString & ".pdf"

instead, which would save the file as pdf named by the webpage title. Only problem there is if the page contains illegal characters (that windows doesnt let you save with) it will throw an exception so that should be handled.

相反,这会将文件保存为由网页标题命名的 pdf。唯一的问题是,如果页面包含非法字符(该窗口不允许您保存),它将引发异常,因此应该进行处理。