Skip to content

Commit 2a2cf1d

Browse files
Adding WPF WebViewer implementation sample
Created sample that implements the WebViewer in WPF using WebView2. The sample creates a virtual host for the WebView2 to make use of the local files.
1 parent 1bb36cc commit 2a2cf1d

16 files changed

+677
-0
lines changed

webviewer-wpf/.gitignore

Lines changed: 403 additions & 0 deletions
Large diffs are not rendered by default.

webviewer-wpf/LICENSE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Copyright (c) 2025 Apryse Software Inc. All rights reserved.
2+
WebViewer UI project/codebase or any derived works is only permitted in solutions with an active commercial Apryse WebViewer license. For exact licensing terms refer to the commercial WebViewer license. For licensing, pricing, or product questions, Contact [Sales](https://apryse.com/form/contact-sales).

webviewer-wpf/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# WebViewer - WPF sample
2+
3+
[WebViewer](https://docs.apryse.com/web/guides/get-started) is a powerful JavaScript-based PDF Library that is part of the [Apryse SDK](https://apryse.com/). It provides a slick out-of-the-box responsive UI that enables you to view, annotate and manipulate PDFs and other document types inside any web project.
4+
5+
- [WebViewer Documentation](https://docs.apryse.com/web/guides/get-started)
6+
- [WebViewer Demo](https://showcase.apryse.com/)
7+
8+
This sample is designed to show you how to integrate WebViewer into a WPF project leveraging [WebView2](https://learn.microsoft.com/en-us/microsoft-edge/webview2/webview2-api-reference?tabs=dotnetcsharp).
9+
10+
## Get your trial key
11+
12+
A license key is required to run WebViewer. You can obtain a trial key in our [get started guides](https://docs.apryse.com/web/guides/get-started), or by signing-up on our [developer portal](https://dev.apryse.com/).
13+
14+
## Initial setup
15+
Modify the index.html file in the **WebViewer** folder to include the license key you obtained from our developer portal.
16+
17+
index.html
18+
```js
19+
WebViewer.Iframe({
20+
path: 'lib',
21+
initialDoc: 'WebviewerDemoDoc.pdf',
22+
//licenseKey: 'LICENSE_KEY'
23+
})
24+
```
25+
26+
## Install
27+
```
28+
git clone --depth=1 https://github.com/ApryseSDK/webviewer-samples.git
29+
cd webviewer-samples/webviewer-wpf
30+
npm install
31+
```
32+
33+
## Run
34+
- Open the webviewer-wpf/webview2/WebView2.sln file in Visual Studio.
35+
- Build the project and then run it.
36+
37+
## Sample Output
38+
![alt text](image.png)

webviewer-wpf/WebView2/App.xaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Application x:Class="WebView2.App"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:local="clr-namespace:WebView2"
5+
StartupUri="MainWindow.xaml">
6+
<Application.Resources>
7+
8+
</Application.Resources>
9+
</Application>

webviewer-wpf/WebView2/App.xaml.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Configuration;
2+
using System.Data;
3+
using System.Windows;
4+
5+
namespace WebView2;
6+
7+
/// <summary>
8+
/// Interaction logic for App.xaml
9+
/// </summary>
10+
public partial class App : Application
11+
{
12+
}
13+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Windows;
2+
3+
[assembly:ThemeInfo(
4+
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
5+
//(used if a resource is not found in the page,
6+
// or application resource dictionaries)
7+
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
8+
//(used if a resource is not found in the page,
9+
// app, or any theme specific resource dictionaries)
10+
)]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Window x:Class="WebView2.MainWindow"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:local="clr-namespace:WebView2"
7+
xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
8+
mc:Ignorable="d"
9+
Title="WPF WebViewer - MainWindow" Height="450" Width="800">
10+
<Grid>
11+
<Grid>
12+
<wv2:WebView2 Name="webViewUI"/>
13+
</Grid>
14+
</Grid>
15+
</Window>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using IO = System.IO;
2+
using System.Text;
3+
using System.Windows;
4+
using System.Windows.Controls;
5+
using System.Windows.Data;
6+
using System.Windows.Documents;
7+
using System.Windows.Input;
8+
using System.Windows.Media;
9+
using System.Windows.Media.Imaging;
10+
using System.Windows.Navigation;
11+
using System.Windows.Shapes;
12+
using Microsoft.Web.WebView2.Core;
13+
using Microsoft.Web.WebView2.Wpf;
14+
15+
namespace WebView2;
16+
17+
/// <summary>
18+
/// Interaction logic for MainWindow.xaml
19+
/// </summary>
20+
public partial class MainWindow : Window
21+
{
22+
public MainWindow()
23+
{
24+
InitializeComponent();
25+
InitializeWebView();
26+
}
27+
28+
private async void InitializeWebView()
29+
{
30+
await webViewUI.EnsureCoreWebView2Async(null);
31+
32+
//Set up a virtual host and map to local folder containing Apryse's WebViewer
33+
webViewUI.CoreWebView2.SetVirtualHostNameToFolderMapping(
34+
"app.local",
35+
GetWebViewerPath(),
36+
CoreWebView2HostResourceAccessKind.Allow);
37+
38+
//Navigate to the page on virtual host
39+
webViewUI.CoreWebView2.Navigate("https://app.local/index.html");
40+
}
41+
42+
private string GetWebViewerPath()
43+
{
44+
//Get the path to the current directory
45+
string rootPath = AppDomain.CurrentDomain.BaseDirectory;
46+
47+
//Get the path to the webviewer folder using relative path, moving up 4 levels
48+
rootPath = IO.Path.GetFullPath(IO.Path.Combine(rootPath, @"..\..\..\..\"));
49+
string projectPath = IO.Path.Combine(rootPath, @"webview2\webviewer");
50+
return projectPath;
51+
}
52+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>WinExe</OutputType>
5+
<TargetFramework>net9.0-windows</TargetFramework>
6+
<Nullable>enable</Nullable>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<UseWPF>true</UseWPF>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.3124.44" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.13.35825.156 d17.13
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebView2", "WebView2.csproj", "{B4D8D2D2-8C85-40BA-B915-468E4D4869EC}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{B4D8D2D2-8C85-40BA-B915-468E4D4869EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{B4D8D2D2-8C85-40BA-B915-468E4D4869EC}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{B4D8D2D2-8C85-40BA-B915-468E4D4869EC}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{B4D8D2D2-8C85-40BA-B915-468E4D4869EC}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {58170C69-E511-4E4A-9325-E98AD66972E4}
24+
EndGlobalSection
25+
EndGlobal

0 commit comments

Comments
 (0)