在WPF中显示PDF文件

在WPF中显示PDF文件,wpf,Wpf,我试图在WPF中显示PDF文件。我已经做了,但过了一段时间它就停止工作了。请帮助我以下是我的代码: else if (result.FileExtension == ".pdf") { // TODO: Make sure you point to a PDF on your system: var str = System.Text.Encoding.Default.GetString(result.ImageToByte); var uc = new UserCont

我试图在WPF中显示PDF文件。我已经做了,但过了一段时间它就停止工作了。请帮助我以下是我的代码:

else if (result.FileExtension == ".pdf")
{
    // TODO: Make sure you point to a PDF on your system:
    var str = System.Text.Encoding.Default.GetString(result.ImageToByte);
    var uc = new UserControl1(str);
    this.windowsFormsHost1.Child = uc;
}
下面是我的用户控制代码:

public UserControl1(string filename)
{
    InitializeComponent();
    this.axAcroPDF1.LoadFile(filename);
}
以及XAML:

<WindowsFormsHost Margin="1" Name="windowsFormsHost1" />

我在我的一个项目中做了类似的事情。最后,我使用WebBrowser托管PDF文档。显然,这意味着用户需要在机器上安装PDF查看器,但这是一个很小的代价

我遇到的一个问题是64位机器。为了让PDF显示在64位机器上,我把目标对准了x86机器

在XAML中:

<WebBrowser visual:WebBrowserExtensions.BindableSource="{Binding Path=Model.AnnouncementUrl, ElementName=Root}"/>

我不久前使用过axAcroPDF1控件,在加载大型PDF文件时确实遇到问题

因此,请尝试以下操作,而不是使用LoadFile:


this.axAcroPDF1.src=文件名

你试过调试吗?是否出现错误和/或异常?可能是与
axAcroPDF1
的兼容性问题。最好询问供应商。定义“停止工作”。我们无法在不知道症状的情况下排除故障,但如果pdf文件大小较大,应用程序将被挂起:(
public static class WebBrowserExtensions
{
    public static readonly DependencyProperty BindableSourceProperty =
         DependencyProperty.RegisterAttached("BindableSource", typeof(object), typeof(WebBrowserExtensions), new UIPropertyMetadata(null, BindableSourcePropertyChanged));

    public static object GetBindableSource(DependencyObject obj)
    {
        return (string)obj.GetValue(BindableSourceProperty);
    }

    public static void SetBindableSource(DependencyObject obj, object value)
    {
        obj.SetValue(BindableSourceProperty, value);
    }

    public static void BindableSourcePropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
    {
        WebBrowser browser = o as WebBrowser;

        if (browser == null)
            return;

        Uri uri = null;

        if (e.NewValue is string)
        {
            var uriString = e.NewValue as string;
            uri = string.IsNullOrWhiteSpace(uriString) ? null : new Uri(uriString);
        }
        else if (e.NewValue is Uri)
        {
            uri = e.NewValue as Uri;
        }
        browser.Source = uri;
    }
}