Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View a PDF in WPF without using WindowsFormsHost

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).

like image 806
Eric K Avatar asked Feb 09 '12 21:02

Eric K


2 Answers

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...

like image 154
WPF-it Avatar answered Nov 20 '22 05:11

WPF-it


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.

like image 21
Flot2011 Avatar answered Nov 20 '22 06:11

Flot2011