Search code examples
wpfpdfwindowsformshostaxacropdf

WPF Control Freeze after WindowsFormsHost trigger to view PDF


I'm having problem with my User Control freeze. It's happen when I'm triggering the WindowsFormsHost to preview the PDF File. The pdf file still running in the WindowsFormsHost where i can still scroll to view it. However, my other controls(togglebutton,popupbox,etc) doesn't seems to work.

Here is the XAML code for WindowsFormsHost in my UserControl

<Grid Margin="0,0,203,0">
  <WindowsFormsHost x:Name="ViewPDFWinForm" HorizontalAlignment="Left" Height="444" VerticalAlignment="Top" Width="708"/>
</Grid>      

Here is the code to trigger the WindowsFormsHost to call PDF File from UserControl

PreviewReportPDF uc = new PreviewReportPDF(ReportGenerator.ReportPath);
this.ViewPDFWinForm.Child = uc;

Here is how I pass the pdf file path

public PreviewReportPDF(string filepath)
    {
        InitializeComponent();
        this.axAcroPDF1.LoadFile(filepath);
        this.axAcroPDF1.setZoom(63);
    }

Solution

  • I have followed your example, as good as I could. I cannot see why you controls are freezing. It seems that the issue is located anywhere else.

    I added the Acrobat Reader Control to a UserControl "UserControl1"

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WpfApplication9
    {
    public partial class UserControl1 : UserControl
    {
    
        public UserControl1(string filepath)
        {
            InitializeComponent();
            this.axAcroPDF1.LoadFile(filepath);
            this.axAcroPDF1.setZoom(63);
        }
    }
    }
    

    I created a WPF Window, like this:

    Window x:Class="WpfApplication9.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:WpfApplication9"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <WindowsFormsHost x:Name="host"/>
        <ToggleButton Content="Toggle" Grid.Column="1" Click="ToggleButton_Click" />
    
    </Grid>
    </Window>
    

    and with this code behind

    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 System.IO;
    using Microsoft.VisualBasic;
    
    namespace WpfApplication9
    {
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            UserControl1 uc = new UserControl1(System.IO.Path.Combine(Microsoft.VisualBasic.FileIO.SpecialDirectories.Desktop, "Test.pdf"));
            this.host.Child = uc;
        }
    
        private void ToggleButton_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Hallo welt!");
        }
    }
    }
    

    After the pdf file was loaded I could fire the Toggle Button Event, so Messagebox was shown. This is the reason why I gues your issue Comes from anywhere else.