How to Add WPF PdfViewer Features to WinForms PdfViewer
10 Jul 20264 minutes to read
To leverage the advanced features offered by the WPF PdfViewer, such as adding annotations, form filling, signatures, stamps, sticky notes, and more, you can integrate the WPF PdfViewer into the Windows Forms and take advantage of its extensive feature sets.
You can host a WPF PdfViewer control within a Windows Forms application using an ElementHost. By embedding the WPF PdfViewer, you can access advanced features available in the WPF control while maintaining a Windows Forms interface.
Steps to host the WPF PdfViewer in the WinForms application
-
Add a WPF UserControl and install the Syncfusion.PdfViewer.WPF NuGet package to your WinForms application.
-
Add the following Syncfusion® namespace in XAML to make use of the WPF PdfViewerControl.
<UserControl xmlns:PdfViewerUserControl="clr-namespace:Syncfusion.Windows.PdfViewer;assembly=Syncfusion.PdfViewer.WPF"> </UserControl> -
Add the below code in the XAML page to initialize the WPF PdfViewer.
<Grid> <PdfView:PdfViewerControl x:Name="pdfViewer"/> </Grid> -
Load the PDF file into the PdfViewer when the UserControl is loaded in the application
using System.Windows;
using System.Windows.Controls;
/// <summary>
/// Interaction logic for PdfViewerUserControl.xaml
/// </summary>
public partial class PdfViewerUserControl : UserControl
{
public PdfViewerUserControl()
{
InitializeComponent();
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
pdfViewer.Load(@"../../Data/Windows Store Apps Succinctly.pdf");
}
}- Add a panel to the form. Create an ElementHost object in the Form and add the created UserControl’s instance as a child of the ElementHost object. Add the elementHost as a child of the panel as mentioned below.
using System.Windows.Forms;
using System.Windows.Forms.Integration;
namespace SampleWF
{
public partial class Form1 : Form
{
ElementHost elementHost = null;
public Form1()
{
InitializeComponent();
// Create the ElementHost control to host the WPF PdfViewer
elementHost = new ElementHost();
elementHost.AutoSize = true;
elementHost.Size = panel1.Size;
elementHost.Dock = DockStyle.Fill;
// Adds the ElementHost to the panel1 control
panel1.Controls.Add(elementHost);
// Create the WPF PdfViewer control and assign it as a child of ElementHost
PdfViewerUserControl pdfViewerUserControl = new PdfViewerUserControl();
// Embeds the PdfViewer into the ElementHost
elementHost.Child = pdfViewerUserControl;
// Set additional visual properties for a seamless look and feel
elementHost.Margin = new Padding(0, 0, 0, 0);
elementHost.Region = new Region();
elementHost.BackColor = System.Drawing.Color.White;
}
// Set the form to maximized when it loads
private void Form1_Load(object sender, System.EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
}
}
}You can find the complete sample in the GitHub for your reference.