【问题标题】:Draw on a PDF into a WPF applicartion using Microsoft Webview2 control使用 Microsoft Webview2 控件将 PDF 绘制到 WPF 应用程序中
【发布时间】:2021-08-08 09:42:28
【问题描述】:

我正在开发一个 WPF 应用程序,我想在其中打开并绘制成 pdf。
我能够在 webview2 控件中打开 pdf,但我无法“绘制到 pdf 中” (如果我通过 Microsoft Edge 打开这个 pdf 是可能的)

有没有人知道/提示/建议为什么在我编译代码时绘图功能被禁用/不活动?

Desired Output

Output

使用过的资源: Get started with WebView2 in WPF

Using the WebView2 control in a .Net application

**Installed versions:**
[Introduction to Microsoft Edge WebView2][1]
[Using the WebView2 control in a .Net application][2]
Installed version of Microsoft.Web.WebView2: 1.0.902.49
Installed version of Microsoft Edge: 92.0.902.67
Installed version of Microsoft Edge WebView2-Runtime: 92.0.902.67 
Target Framework: .NET Framework 4.7.2
Visual Studio 2019





    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using Microsoft.Web.WebView2.Core;
    
    namespace SwissSonic.Pages
    {
        /// <summary>
        /// Interaction logic for Change_drawing.xaml
        /// </summary>
        public partial class Change_drawing : Page
        {
            private MainWindow mainWindow;
    
            public Change_drawing(MainWindow main)
            {
                InitializeComponent();
                InitializeAsync();
                mainWindow = main;
                mainWindow.Button_Click_Zeichnung.Click += Button_Click_Zeichnung;
            }
    
            async void InitializeAsync()
            {
                await webView1.EnsureCoreWebView2Async(null);
            }
    
            private void Button_Click_Zeichnung(object sender, RoutedEventArgs e)
            {
                webView1.CoreWebView2.Settings.IsStatusBarEnabled = true;
                webView1.CoreWebView2.Settings.AreDefaultContextMenusEnabled = true;
                webView1.CoreWebView2.Settings.IsScriptEnabled = true;
                webView1.CoreWebView2.Settings.IsStatusBarEnabled = true; 
    
               
                string test = @"file:\\\C:\\TestPDF\\mcdonalds_in_india.pdf";
                //string test = @"https://www.stadlerrail.com/media/pdf/web_stadler_rail_gb20_de.pdf";
                //  webView1.Source = new Uri(test);
                  webView1.CoreWebView2.Navigate(test);   
    
            }
            
        }          
    
    }


<Page x:Class="SwissSonic.Pages.Change_drawing"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:SwissSonic.Pages"
      xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      Title="Change_drawing"> 
          
    
    
    <Grid Background="Red">

        <Grid.RowDefinitions>
            <RowDefinition Height="35"/>
            <RowDefinition Height="*"/>
            <RowDefinition/>
        </Grid.RowDefinitions>


        <wv2:WebView2  Name="webView1" Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="0" Grid.RowSpan="3"/>        
   
    </Grid>
</Page>

【问题讨论】:

    标签: c# wpf pdf microsoft-edge webview2


    【解决方案1】:

    我可以使用最新版本的 WebView2 重现该问题。我认为the resource link you provide 中的屏幕截图来自以前版本的 WebView2。在当前版本的 WebView2 中,Draw、Highlight 和 Erase 似乎被禁用了。

    我能找到的唯一一个相关 API 是 HiddenPdfToolbarItems,它被添加到 latest WebView2 prerelease 中。但是这个API只能在pdf工具栏中隐藏/显示these buttons

    我认为我们无法使用代码制作 Draw、Highlight 和 Erase 显示,因为没有相关的 API。您可以提供有关此问题的反馈here。我认为Edge WebView团队会检查并做出答复。

    【讨论】:

    • 非常感谢您的回复。你能建议我如何达到同样的结果吗? :)
    • 对不起,我不知道如何实现这一点。编写自己的绘图函数似乎极其复杂。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-27
    • 1970-01-01
    相关资源
    最近更新 更多