winforms NavigateToString PDF引发系统参数异常:'值不在预期范围内,'

ih99xse1  于 2022-11-17  发布在  其他
关注(0)|答案(1)|浏览(119)

我曾经用下面的代码显示过一个PDF文件,但是由于某种原因,它不再工作了,而且总是引发错误
System.ArgumentException:'值不在预期范围内。'
我不知道我做错了什么。
我使用的是Nuget提供的Microsoft.Web.WebView2。
我在窗体上放置了一个WebView2,其名称为WebView2。
下面是我的代码:

Private Async Sub PDFViewer_Load(sender As Object, e As EventArgs) Handles Me.Load

    Debug.WriteLine("before InitializeAsync")
    Await (InitializeAsync())
    Debug.WriteLine("after InitializeAsync")

    Dim pdfBytes() As Byte
    pdfBytes = File.ReadAllBytes("somefile.pdf")

    Dim pdfBase64 As String = Convert.ToBase64String(pdfBytes)
    Dim html As String = "<!DOCTYPE html><html><head>
                        </head>
                        <style>
                               body   {margin: 0;}
                               iframe {display: block; background: #000; border: none; height: 100vh; width: 100vw;}
                        </style>
                        <body>" & $"<iframe src=""data:Application/pdf;base64,{pdfBase64}"">" & "</iframe></body></html>"

    WebView2.NavigateToString(html) 'here the error is thrown

End Sub

它在Acrobat Reader和其他任何地方都能正常工作。

nzrxty8p

nzrxty8p1#

WebView2当前在NavigateToString中有一个限制。
大约在1.5到2 MB之间。
解决方法是在本地加载该文件。

相关问题