1

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?

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
Noble-Surfer
  • 3,052
  • 11
  • 73
  • 118
  • Possible duplicate of [Render PDF using DocumentViewer control?](http://stackoverflow.com/questions/3430177/render-pdf-using-documentviewer-control) – Manfred Radlwimmer May 24 '16 at 10:03
  • 1
    Sorry, not possible - `DocumentViewer` is for `FixedDocuments` like XPS, not PDF. XPS can be loaded like this: `XpsDocument doc = new XpsDocument(fileName, FileAccess.Read); documentViewer.Document = doc.GetFixedDocumentSequence();` – Manfred Radlwimmer May 24 '16 at 10:04
  • pls also check http://stackoverflow.com/questions/3430177/render-pdf-using-documentviewer-control – Felix D. May 24 '16 at 13:24
  • "point this DocumentViewer to a PDF (or .doc, or whatever) file" this is too generic to provide a proper answer. Please make the question more specific. – Emond Apr 06 '19 at 17:33

3 Answers3

0

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.

Community
  • 1
  • 1
oziomajnr
  • 1,671
  • 1
  • 15
  • 39
0

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.

hertzogth
  • 236
  • 1
  • 2
  • 19
-1

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.

Felix D.
  • 4,811
  • 8
  • 38
  • 72