'Opening a PDF file from within a WPF application

I have a WPF application in which the GUI displays a few different aspects of the application to the user, using a different tab for each part of the application. I am now looking to add the functionality to load and view document from within the application on one of the tabs.

I have added a DocumentViewer to the tab, and can see that it is displayed in the GUI when I run my application, but I'm not sure how to get that DocumentViewer to load/ display a document, and can't seem to find any method calls/ markup that enable you to do this.

The XAML markup I am using to add the DocumentViewer to my application is:

<TabItem Header="Document Viewer">
    <StackPanel>
        <DocumentViewer x:Name="docViewer" Height="643" Margin="0,0,-0.4,0"/>
            <DocumentViewer x:Name="documentViewer" Height="1" Margin="0,0,-0.4,0" RenderTransformOrigin="0.5,0.5">
                <DocumentViewer.RenderTransform>
                    <TransformGroup>
                        <ScaleTransform ScaleY="-1"/>
                        <SkewTransform/>
                        <RotateTransform/>
                        <TranslateTransform/>
                    </TransformGroup>
                </DocumentViewer.RenderTransform>
            </DocumentViewer>
    </StackPanel>
</TabItem>

How do I point this DocumentViewer to a PDF (or .doc, or whatever) file that's located on my computer, so that it will load and display that document inside my application window?



Solution 1:[1]

Similar question here . Wpf does no provide a base class for that and if you want to work around it you couod open the pdf in its own application using System. Diagnostics.Process.Start (@"filename.pdf");

You could also visit the link for other options.

Solution 2:[2]

GemBox libraries can convert files to XpsDocument object which you could assign to DocumentViewer control.

For example, here is how to do that for PDF with GemBox.Pdf:

XpsDocument xpsDocument;

public MainWindow()
{
    InitializeComponent();

    using (var document = PdfDocument.Load("input.pdf"))
    {
        this.xpsDocument = document.ConvertToXpsDocument(SaveOptions.Xps);
        this.docViewer.Document = this.xpsDocument.GetFixedDocumentSequence();
    }
}

And for DOC with GemBox.Document:

XpsDocument xpsDocument;

public MainWindow()
{
    InitializeComponent();

    var document = DocumentModel.Load(path);
    this.xpsDocument = document.ConvertToXpsDocument(SaveOptions.XpsDefault);
    this.docViewer.Document = this.xpsDocument.GetFixedDocumentSequence();
}

Note, in both cases the XpsDocument object must stay referenced so that DocumentViewer can access its resources. Otherwise, GC will collect/dispose XpsDocument and DocumentViewer will no longer work.

Solution 3:[3]

I recommend using a free PDF Lib for c#.

http://www.e-iceblue.com/Introduce/free-pdf-component.html#.V0RVLfmLRpg is a good example!

To view PDFs in WPF converting single pages to image and show that is a good solution.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 hertzogth
Solution 3 Felix D.