You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用WebView2的NavigateToString加载PDF时抛出ArgumentException异常求助

解决WebView2 NavigateToString加载PDF抛出ArgumentException的问题

可能的原因及修复方案

1. 数据URI长度超限

WebView2对NavigateToString接收的内容存在长度上限,大体积PDF转base64后容易超出阈值,触发参数异常。

  • 修复方案:将PDF字节写入临时文件,通过Navigate加载本地文件路径:
Private Async Sub PDFViewer_Load(sender As Object, e As EventArgs) Handles Me.Load
    Await WebView2.EnsureCoreWebView2Async(Nothing)
    
    Dim pdfBytes() As Byte = File.ReadAllBytes("somefile.pdf")
    ' 创建临时PDF文件
    Dim tempFilePath = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.pdf")
    File.WriteAllBytes(tempFilePath, pdfBytes)
    
    ' 加载临时文件
    WebView2.Navigate(tempFilePath)
    
    ' 窗体关闭时清理临时文件
    AddHandler Me.FormClosed, Sub(s, args)
                                 If File.Exists(tempFilePath) Then
                                     File.Delete(tempFilePath)
                                 End If
                             End Sub
End Sub

2. MIME类型大小写不规范

数据URI中的Application/pdf应为小写的application/pdf,部分WebView2版本对MIME类型大小写敏感,会导致参数校验失败。

  • 修复方案:修正iframe的src中的MIME类型:
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>"

3. 控件初始化未完全完成

虽然调用了InitializeAsync,但可能存在异步初始化未就绪的情况,导致后续操作触发异常。

  • 修复方案:通过CoreWebView2InitializationCompleted事件确保控件初始化完成后再执行加载:
Private Async Sub PDFViewer_Load(sender As Object, e As EventArgs) Handles Me.Load
    AddHandler WebView2.CoreWebView2InitializationCompleted, AddressOf OnWebView2Initialized
    Await WebView2.EnsureCoreWebView2Async(Nothing)
End Sub

Private Sub OnWebView2Initialized(sender As Object, e As CoreWebView2InitializationCompletedEventArgs)
    If Not e.IsSuccess Then Return
    
    Dim pdfBytes() As Byte = 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)
End Sub

4. WebView2运行时版本兼容问题

不同版本的WebView2运行时对数据URI的处理逻辑可能存在差异,旧版本可能存在已知bug。

  • 修复方案:通过NuGet更新Microsoft.Web.WebView2包至最新版本,同时安装最新的WebView2运行时。

额外排查方向

  • 测试小体积PDF文件,确认是否是大文件导致的长度超限问题
  • 检查PDF是否包含特殊加密或权限设置(部分加密PDF可能无法被WebView2的内置渲染器解析)

内容的提问来源于stack exchange,提问作者tmighty

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 06:30:55