Search code examples
c#wpfpdfmicrosoft-edgewebview2

Draw on a PDF into a WPF applicartion using Microsoft Webview2 control


I am working on a WPF application in which I want to open and draw into a pdf.
I am able to open the pdf in the webview2 control, but I am not able to "draw into the pdf" (If i am open this pdf via Microsoft Edge it is possible)

Has anyone an idea/hint/suggestion why the drawing function is disabled/not active when I compile the code?

Desired Output

Output

Used resources: 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>

Solution

  • I can reproduce the issue with the latest version of WebView2. I think the screenshot in the resource link you provide is from a previous version of WebView2. In present version of WebView2, the Draw, Highlight and Erase seems to be disabled.

    And the only one related API I can find is HiddenPdfToolbarItems which is added in the latest WebView2 prerelease. But this API can only hide/show these buttons in pdf toolbar.

    I think we can't make the Draw, Highlight and Erase show using code because there isn't related API. You can provide feedback about this issue here. I think Edge WebView team will check it and make a reply.