|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Enhance WinForms PdfViewer with WPF Features | WinForms | Syncfusion |
| 4 | +description: Learn how to integrate the WPF PdfViewer in Windows Forms to access advanced features like annotations, form filling, and signatures. |
| 5 | +platform: windowsforms |
| 6 | +control: PDF Viewer |
| 7 | +documentation: ug |
| 8 | +--- |
| 9 | +# How to Add WPF PdfViewer Features to WinForms PdfViewer |
| 10 | + |
| 11 | +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]( https://support.syncfusion.com/kb/article/7882/how-to-host-pdf-viewer-in-windows-forms-application ) and take advantage of its extensive feature sets. |
| 12 | + |
| 13 | +we 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. |
| 14 | +To host the WPF PdfViewer in the winforms application follow the below steps, |
| 15 | + |
| 16 | +1. Create an UserControl and add the below XAML code to it. |
| 17 | + |
| 18 | +* Add the following Syncfusion namespace in XAML to make use of the WPF [PdfViewerControl](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.PdfViewer.PdfViewerControl.html) |
| 19 | + |
| 20 | + ~~~xaml |
| 21 | + <UserControl |
| 22 | + xmlns:PdfViewerUserControl="clr-namespace:Syncfusion.Windows.PdfViewer;assembly=Syncfusion.PdfViewer.WPF"> |
| 23 | + </UserControl> |
| 24 | + ~~~ |
| 25 | +* Declare the WPF [PdfViewerControl](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.PdfViewer.PdfViewerControl.html) in the XAML page. |
| 26 | + |
| 27 | + ~~~xaml |
| 28 | + <UserControl x:Class="SampleWF.PdfViewer" |
| 29 | + xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" |
| 30 | + xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
| 31 | + xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" |
| 32 | + xmlns:d="http://schemas.microsoft.com/expression/blend/2008" |
| 33 | + xmlns:local="clr-namespace:SampleWF" |
| 34 | + xmlns:Syncfusion="http://schemas.syncfusion.com/wpf" |
| 35 | + mc:Ignorable="d" |
| 36 | + xmlns:PdfView="clr-namespace:Syncfusion.Windows.PdfViewer;assembly=Syncfusion.PdfViewer.WPF" |
| 37 | + Loaded="UserControl_Loaded" |
| 38 | + > |
| 39 | + |
| 40 | + <Grid> |
| 41 | + <PdfView:PdfViewerControl x:Name="pdfViewer"/> |
| 42 | + </Grid> |
| 43 | +</UserControl> |
| 44 | + ~~~ |
| 45 | + * Add the below line of codes in the code behind file. |
| 46 | + |
| 47 | +{% tabs %} |
| 48 | +{% highlight c# %} |
| 49 | + |
| 50 | +using System.Windows; |
| 51 | +using System.Windows.Controls; |
| 52 | + |
| 53 | +namespace SampleWF |
| 54 | +{ |
| 55 | + /// <summary> |
| 56 | + /// Interaction logic for PdfViewer.xaml |
| 57 | + /// </summary> |
| 58 | + public partial class PdfViewerUserControl : UserControl |
| 59 | + { |
| 60 | + public PdfViewer() |
| 61 | + { |
| 62 | + InitializeComponent(); |
| 63 | + } |
| 64 | + |
| 65 | + private void UserControl_Loaded(object sender, RoutedEventArgs e) |
| 66 | + { |
| 67 | + pdfViewer.Load(@"../../Data/Windows Store Apps Succinctly.pdf"); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +{%endhighlight%} |
| 73 | +{% endtabs %} |
| 74 | + |
| 75 | +2. Create an ElementHost object in the Form and add the created UserControl's object as child to the ElementHost object. |
| 76 | + |
| 77 | +To do the above step follow the below procedures |
| 78 | + |
| 79 | +{% tabs %} |
| 80 | +{% highlight c# %} |
| 81 | + |
| 82 | +using System.Windows.Forms; |
| 83 | +using System.Windows.Forms.Integration; |
| 84 | + |
| 85 | +namespace SampleWF |
| 86 | +{ |
| 87 | + |
| 88 | + public partial class Form1 : Form |
| 89 | + { |
| 90 | + ElementHost elementHost = null; |
| 91 | + public Form1() |
| 92 | + { |
| 93 | + InitializeComponent(); |
| 94 | + |
| 95 | + // Create the ElementHost control to host the WPF PdfViewer |
| 96 | + elementHost = new ElementHost(); |
| 97 | + elementHost.AutoSize = true; |
| 98 | + elementHost.Size = panel1.Size; |
| 99 | + elementHost.Dock = DockStyle.Fill; |
| 100 | + // Adds the ElementHost to the panel1 control |
| 101 | + panel1.Controls.Add(elementHost); |
| 102 | + |
| 103 | + // Create the WPF PdfViewer control and assign it as a child of ElementHost |
| 104 | + PdfViewerUserControl pdfViewerUserControl = new PdfViewerUserControl(); |
| 105 | + // Embeds the PdfViewer into the ElementHost |
| 106 | + elementHost.Child = pdfViewerUserControl; |
| 107 | + |
| 108 | + // Set additional visual properties for a seamless look and feel |
| 109 | + elementHost.Margin = new Padding(0, 0, 0, 0); |
| 110 | + elementHost.Region = new Region(); |
| 111 | + elementHost.BackColor = System.Drawing.Color.White; |
| 112 | + } |
| 113 | + |
| 114 | + // Set the form to maximized when it loads |
| 115 | + private void Form1_Load(object sender, System.EventArgs e) |
| 116 | + { |
| 117 | + this.WindowState = FormWindowState.Maximized; |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +{%endhighlight%} |
| 123 | +{% endtabs %} |
| 124 | + |
| 125 | +You can find the sample for you reference [here](https://www.syncfusion.com/downloads/support/directtrac/general/ze/SampleWF920548313) |
0 commit comments