Draw on a PDF into a WPF applicartion using Microsoft Webview2 control - c#

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>

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.

Related

WPF MainWindow differs at runtime from it's expected outcome

I am relatively new to this platform, so forgive me for my lack of professionalism.
I encountered a problem while creating one of my first WPF applications.
I managed to include some buttons in MainWindow and run it, but I spotted some bugs, so I tried to start from the scratch.
I decided to run an empty window, but then i saw the previous "buggy" window at runtime. Since then, no matter what i change in MainWindow.xaml file i get the same old result.
I tried to chceck whether my program compiles, and for what I know it does.
Unfortunately I erased old xaml code responsible for "buggy" window, but I can recall it was something like this.
<Window x:Class="ProjektPO.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ProjektPO"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<StackPanel>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button x:Name="A" Content="B" Click="A_Click"/>
</Grid>
</StackPanel>
</Grid>
</Window>
And the .cs file (almost default)
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;
namespace ProjektPO
{
/// <summary>
/// Logika interakcji dla klasy MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void A_Click(object sender, RoutedEventArgs e)
{
}
}
}
Program compiles and dispalys old "buggy" Window.

Could not load file or assembly WPFToolkit

I am trying to use WPFToolkit in my user control dll. I inserted the WPFToolkit in its reference, and my user control dll builds with no error.
Then I insert my user control dll into my application, but when my application new an object of my user control dll
MultiROIStats mroi = new MultiROIStats();
the exception occured, saying:
Additional information: Could not load file or assembly 'WPFToolkit, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
Here is my user control dll code, the constructor where the error occurs.
View xaml code:
<Window x:Class="MultiROIStats.MultiROIStats"
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:dg="http://schemas.microsoft.com/wpf/2008/toolkit"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<dg:DataGrid ItemsSource="{Binding Path=FileData}" Margin="0,30,0,0" />
<Button Height="22" HorizontalAlignment="Left" Margin="8,4,0,0"
Name="button1" VerticalAlignment="Top" Width="48"
Command="{Binding Path=GetDataCommand}">Button
</Button>
</Grid>
</Window>
View C# code
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;
namespace MultiROIStats
{
using System.Windows;
using ViewModel;
//xmlns:dg="clr-namespace:Microsoft.Windows.Controls;assembly=WpfToolkit"
/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>
public partial class MultiROIStats : Window
{
public MultiROIStats()
{
InitializeComponent(); // exception occurs here!
DataContext = new MultiROIStatsViewModel();
}
}
}
I also checked the binary folder of my user control dll, the WPFToolkei.dll is there. So I am confused, and I am wondering how can I correct this error? Thanks.
Have you maybe tried the NuGet package for the toolkit to see if that works instead?

How to synchronize designer template and C# sharp code in Visual Studio Express 2012?

Let's say, I create a page MyPage in a WPF application MyApp with Visual Studio 2012 Express.
Very often, when I try to access newly created elements of MyPage.xaml in MyPage.xaml.cs, I get an error message like
The name “group_combobox” does not exist in the current context
This happens even though other elements of the same page / the same context do exist and are available in my C# sharp code editor window.
To give you an example of the problem, see the following MyPage.xaml and MyPage.xaml.cs which are returning the error message, when editing the MyPage.xaml.cs file:
MyPage.xaml
<Page x:Class="MyApp.MyPage"
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"
mc:Ignorable="d"
d:DesignHeight="768" d:DesignWidth="1024"
Title="MyApp - MyPage" Width="Auto" Height="Auto">
<Button x:Name="MyPage_back_button" Content="Back" Margin="10,100,10,0" VerticalAlignment="Top" FontSize="24" Height="50"/>
<StackPanel Margin="0" x:Name="settings_stackpanel">
<GroupBox Header="Group" VerticalAlignment="Top" FontSize="24" Margin="10" Height="100">
<ComboBox x:Name="group_combobox" Height="62" VerticalAlignment="Top" Margin="0" BorderBrush="{x:Null}" Background="{x:Null}" VerticalContentAlignment="Center"/>
</GroupBox>
</StackPanel>
</Page>
MyPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
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;
namespace MyApp
{
public partial class MyPage : Page
{
public MyPage()
{
Debug.WriteLine("MyPage");
InitializeComponent();
group_combobox.Items.Clear(); // <-- error: The name “group_combobox” does not exist in the current context
string test = MyPage_back_button.ToString(); // <-- no error
}
}
}
As you can see, the error is only thrown for one of two elements from the same page. I think, there is somehow somewhere described how this "context" looks and what it contains. And at a certain point there is some kind of "synchronization" or something like that.
I noticed this behaviour before, and it solved "magically" that I think I somehow accidentally fired that trigger or the mechanism that is responsible for this synchronization.
Do you know how to trigger that kind of synchronization and how to access the information about "the current context"?
p.s. Rebuilding the project did not solve the problem

wpf custom title bar does not work but have no exceptions

I downloaded the sample code from
http://www.cnblogs.com/Files/sheva/RibbonStyle2.zip
I added three key files into a new project without changing the files. They are NativeMethods.cs, OfficeWindow.cs, and Generic.xaml.
I then use my new WFP form (MainWindow.xaml) to inherit from OfficeWindow.
<cc:OfficeWindow
x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cc="clr-namespace:RibbonStyle"
ResizeMode="CanResizeWithGrip"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/WpfApplication1;component/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
</Grid>
</cc:OfficeWindow>
and code behind MainWindow.xaml.cs
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 RibbonStyle;
namespace WpfApplication1
{
public partial class MainWindow : OfficeWindow
{
public MainWindow()
{
InitializeComponent();
}
}
}
The design view looks good. It shows ribbon style title bar as expected. However when I run it in the debugger it shows a classic title bar without and exceptions.
Note that if you download the original sample code from
http://www.cnblogs.com/Files/sheva/RibbonStyle2.zip
and run it directly it works.
Can anyone try my way and tell me what I have missed?
Thanks,
i have checked this example ..i think it is working fine here is the screeschot..
if thr is problem in its styling comment below..

Binding to runtime object instance

I'm going round in circles here. I've kind of got the hang of XmlDataProvider bindings but the file I'm using seems too large to bind dynamically (50Mb doesn't work; 2Mb works). So instead I have the data loaded into classes using code generated from the XSD.
However, I can't get binding to CLR objects to work, due to my lack of knowledge. I'm using Visual Studio 2008 Pro, C# and .Net 3.5.
Here's the XAML file:
<Window x:Class="WpfObjectText.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfObjectText"
Title="Window1" Height="300" Width="300">
<Grid>
<Grid.Resources>
<ObjectDataProvider x:Key="simpleBinding" ObjectType="{x:Type local:ExampleClass}"/>
</Grid.Resources>
<StackPanel>
<TextBox Name="textBox1" Text="{Binding Path=simpleBinding}" />
</StackPanel>
</Grid>
</Window>
And the code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
namespace WpfObjectText
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public ExampleClass TestInstance = new ExampleClass("Hello, world!");
public Window1()
{
InitializeComponent();
}
}
public class ExampleClass
{
public string TestString { get; set; }
public ExampleClass(string initialText)
{
TestString = initialText;
}
}
}
I've deliberately kept it simple so I can take baby steps. All I want to do here is populate the textbox from an instance of ExampleClass, and have the TestString field updated if the textbox changes (ie bidirectional). I know I can set MethodName in the binding which works to a certain extent in ListBoxes but that doesn't seem to imply bidirectional to me. Coming from a Delphi7 Win32 programmer, this is alien territory for me!
Assistance appreciated.
These are the necessary changes:
<TextBox Name="textBox1" Text="{Binding Path=TestString}" />
and then in the constructor:
DataContext = TestInstance;
If my understanding of the question is correct, you don't need Grid.Resources section at all.

Categories