View a PDF in WPF without using WindowsFormsHost
Asked Answered
C

3

7

Are there any native WPF controls for displaying PDFs? I am writing a program that will load a PDF file and then display extra notations on top of it.

Using a WindowsFormsHost (http://hugeonion.com/2009/04/06/displaying-a-pdf-file-within-a-wpf-application/) won't work because a WindowsFormsHost control always displays on top of all other WPF controls in a window. This won't allow my notations to be seen over the PDF.

Converting the PDF into a raster image with the level of zoom detail that I need would create a file that is too large.

The WebBrowser control doesn't allow the pages to be changed or zoomed programmatically. I also can't remove the Adobe toolbars.

Any third-party libraries I used would need to be free (as in beer).

Crisper answered 9/2, 2012 at 21:3 Comment(0)
U
7

Provided that you have some PDF viewer plugin (such as Acrobat Reader) for IE on your machine...

    <Grid>
            <WebBrowser x:Name="WebBrowser1"
                        Source="C:\Temp\Test.pdf"/>
    </Grid>

works just fine...

Ufo answered 10/2, 2012 at 11:15 Comment(3)
I tried this, but I can't seem to control the WebBrowser enough for my purposes. See the update to my original question for more details.Crisper
@Eric K, What is it that you want to control? WebBrowser can be controlled... it provides its HTMLHost interface that can do wonders!Isopropyl
I need to remove all visible toolbars from the PDF ActiveX plugin. I also need to be able to zoom the PDF. And finally, I need to be able to navigate to specific pages. I know that the WebBrowser has some page navigation, but it is specific to URLs and your viewing history. It doesn't allow you to specify, for example, page 3 of a specific PDF.Crisper
N
1

Unfortunately I don't have enough reputations yet to make a comment, so I will put it as an answer. I have had a very similar problem with Flash recently and I ended up using WindowsFormsHost and Overlays/Adorners. Just my 2cents.

Here is XAML creating an overlay as a popup window:

    <Grid>
       <Canvas >
        <WebBrowser  x:Name="wbMain" Width="800" Height="500"></WebBrowser>
        <Popup x:Name="puOverlay" AllowsTransparency="True" Placement="Bottom" PlacementTarget="{Binding ElementName=wbMain}">
                <Ellipse Canvas.Left="0" Canvas.Top="0" Height="50" Name="headEllipse" Stroke="Black" Fill="Orange" Width="50" Canvas.ZIndex="5"/>
        </Popup>
        <Ellipse Canvas.Left="0" Canvas.Top="0" Height="50" Name="headEllipse1" Stroke="Black" Fill="Orange" Width="50" Canvas.ZIndex="5"/>
         </Canvas>
    </Grid>

For the simplicity sake I reduced my overlay to one ellipse. Web Browser is hosted in WindowsFormsHost. Here is the code placing and showing it:

    public MainWindow()
    {
        InitializeComponent();
        puOverlay.VerticalOffset = -60;
        puOverlay.HorizontalOffset = (wbMain.ActualWidth / 2) - 20;
        puOverlay.IsOpen = true;
        ...
    }

Pretty simple, however don't hesitate to ask if something is still unclear.

Newcomer answered 10/2, 2012 at 8:14 Comment(4)
Would you mind sharing your example? I'm having a bit of trouble getting the sample in your link to work correctly.Crisper
I tried using an Adorner by following the steps in this guide: marlongrech.wordpress.com/2008/02/28/… but the WindowsFormsHost was still drawn on top. Any suggestions?Crisper
Thanks for the sample, but using a Popup is different from the Adorner technique described in your link.Crisper
You are right, sorry, I forgot that I ended up using Popups as the fastest and the easiest way.Newcomer
N
0

I haven't found any good royalty free 3rd party WPF controls that work well. Telerik has a pdf viewer, but it doesn't do all types and won't do large documents well for me. I tried Syncfusion's as well and that was buggy too. I ended up using WindowsFormsHost with a another 3rd party GD picture's windows forms viewer. They say they're making a WPF version though.

Now there are others that you have to pay for every server install that might be good, I just can't work with that though.

Nematic answered 19/6, 2013 at 17:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.