NavigateToString PDF 引发 System.ArgumentException: 'Value does not fall within the expected range.'



我曾经使用以下代码显示了一个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和其他任何地方都能很好地工作。

WebView2当前在NavigateToString中有一个限制。

大约在1.5到200毫巴之间。

解决方法是在本地加载文件。

相关内容

最新更新