1
votes

I try to use Acrobat PDF Reader in a WPF app but I found that WindowsFormsHost is a WinForm Control ...so it could be the source of the issue... I got the message "Cannot convert OphtalBox.PDFReader to Windows.Forms.control" at the indicated line. Thanks

I did a mix of these 2 tutorials :
http://www.screencast.com/t/JXRhGvzvB

http://www.codeproject.com/Articles/380019/Using-Adobe-Reader-in-a-WPF-app

My page to display my usercontrol

public partial class DidactielPage : Window
{
    public DidactielPage()
    {
        InitializeComponent();
        var ucPdfReader = new PdfReader("/Resource/Data/DidacticielOphtalBoX.pdf");
        this.WindowsFormHost1.Child = ucPdfReader;// the error message shows here


    }
}

My userControl Class

public partial class PdfReader : UserControl
{
    public PdfReader(string filename)
    {
        InitializeComponent();

        AcroPDF acro = new AcroPDF();
        acro.setShowToolbar(false);

        acro.setView("FitH");
        acro.LoadFile(filename);
        acro.src = filename;
        acro.setViewScroll("FitH", 0);
    }
}
2
Why use a WindowsFormsHost when your PdfReader is a WPF UserControl? That doesn't make sense. - Clemens
So what would make sense ? Because this is what is done in the WPF tutorial :screencast.com/t/JXRhGvzvB - Paul Martinez
Put PdfReader directly into one of the usual WPF panels, e.g. a Grid? - Clemens
Just don't. Adobe Reader sucks in so many ways, you never want to support this. Google "wpf pdf viewer" to go shopping. - Hans Passant

2 Answers

1
votes

What you need is Windows Forms Integration with element host.

1) Add a reference to WindowsFormsIntegration, on the Add Reference dialog, go to .NET and order alphabetically to find it.

2) Add the import/using

using System.Windows.Forms.Integration;

3) Use this sweet little convenience method.

    private static ElementHost createFormHostForWpfElement(UserControl wpfControl)
    {
        ElementHost elementHost = new ElementHost();
        elementHost.Child = wpfControl;
        return elementHost;
    }

4) Now add the HostElement to your form.

this.WindowsFormHost1.Child = createFormHostForWpfElement(ucPdfReader);

Let me know if that takes care of it for you.

0
votes

Late response: To fix this issue you should use a ElementHost from the System.Windows.Forms.Integration.ElementHost() library to contain the UserControl, and then add the ElementHost to your Child's list.

Try the following:

var elementHostPartial = new System.Windows.Forms.Integration.ElementHost();
elementHostPartial.TabIndex = 0;//increment this if more controls are needed
var ucPdfReader = new PdfReader("/Resource/Data/DidacticielOphtalBoX.pdf");
elementHostPartial.Child = ucPdfReader;
this.WindowsFormHost1.Child = elementHostPartial;

I hope this helps.