# .NET and More > WPF, WCF, WF >  [RESOLVED] display a PDF

## bflosabre91

Anyone know if there is a way to display a PDF file in a WPF application? and not by using a window form control? im trying to avoid that but it looks like that might be the only way...

----------


## DeanMc

I believe the only functional method at this time is to use a control host ala: http://hugeonion.com/2009/04/06/disp...f-application/

Is this your pdf or someone else's? The reason I ask is there are some pretty slick examples on the web of rich documents native to WPF.

----------


## bflosabre91

well maybe you might have a better idea. I want to include help files in a WPF application which would be PDF or a word doc i guess. I was hoping to be able to prevent the user from saving the file and printing the file as best as i could. So this would be my pdf, I just wanted to make a custom help interface so it doesnt look like crap compared to the rest of the WPF application. thanks

----------


## DeanMc

In WPF there is a huge amount of power over document creation. While it can be comple at first its power is amazing for top notch documents or in your case help files, it also means that these files can be fully intergrated with the look and feel of your application.

The first port of call is to read up about document creation here. Pay paticular attention to the XPS section which is a document format that can be used on word 07 documents. This means you could create all your documents in word and write a tool to convert them to XPS and display them in your wpf application natively.

There is an example of this here in VB.

To be honest though it is probably best to write up a small application to author these files natively in the first place since all the formatting is essentially xaml but have word does give you the flexablity to have very complex help files.

One other thing is although i agree about the saving i would disagree about the ability to print. Most people, like me often print out manuals in order to have them as a reference. If you use any of the document containers you should give the ability to print and if you use a fixed container your helpfiles will look the same on screen and paper. Trust me people will still print the manual even if they have to to so by screen shotting so give them that ability.

----------


## bflosabre91

awesome. this looks like it will be something i will be able to use. Ill look into it. I appreciate your help. Yeah, i agree with you about the printing, but my boss wants to lock down the printing of the help as much as we can to prevent the competitors from getting their hands on what we are doing  :Smilie:  of course, if theres a will theres a way, but i would like to make it as much of a pain in the ass as possible. Thanks for your help

----------


## chris128

> Pay paticular attention to the XPS section which is a document format that can be used on word 07 documents. This means you could create all your documents in word and write a tool to convert them to XPS and display them in your wpf application natively.


You dont need Word 2007 to be able to create XPS documents. I think the XPS printer is automatically installed when you install the Office 2007 compatibility pack (which is a free download). I know I've got it on this PC and I've only got Word 2003 on this machine  :Wink:

----------


## bflosabre91

haha, i was just about to post the question if i needed office 2007. thanks chris, ill look at the compatibility pack.

----------


## DeanMc

But will the documents formating persist? I know that the conversion is from .docx to xps for raw workable data in wpf. I suppose a little testing is in order....  care to try chris? It would be interesting to find out.

----------


## bflosabre91

can you guys run that sample that DeanMc posted? I cant get it to run to save my life. I keep getting a COMException: Value out of range. I believe i installed all the XPS prerequisites. but i just cant figure this out... thanks for you help

----------


## chris128

When you say 'raw workable data in wpf' what do you mean? I thought your suggestion was to just save some word documents to XPS file format and then just display them in the WPF app, not modify them within the WPF application

----------


## DeanMc

I would imagine you get the xps file as a workable stream that you could save as a flow document. Im in work at the moment though so i wont be able to take a good look at it until later.

----------


## chris128

oh right, well if thats the case I cant see why it would only be with Word 2007 - at the end of the day surely one XPS format file can be imported/manipulated in the exact same way as any other XPS format file

----------


## DeanMc

True but word does have advanced editing features tha can be helpful in the creation of the actual document. I'm going to run some tests now.

----------


## DeanMc

Ok I think I've got it!



So here is what you need to do:

1: Get the XPS printer installed.
2: Make help file in program of choice and "print" to XPS printer.
3: Open in your program.

Now here are the cravats....

1: Links don't seem to work although you could have one big helpfile like a PDF to solve this.

2: You cant hide the original documents per say. I would imagine trying to parse out of a database would be a nightmare but lets face it if your competitors really wanted them they would just use a disassembler on your app if they could be embedded.

3:Your kind of stuck with the built in viewing controls from what I can see, there are a view but they look similar to my screeny.

Having said that they are pretty neat. If you wanted complete control over how your help files and truly wanted them internal you could look at hand crafted flow documents. These are essentially Xaml marked text files that could be stored in a DB and parsed on the fly but it would be similar to creating HTML help files IE by hand.

As for the code. Its 1 imports statement and 2 lines of code:


VB Code:
Imports System.Windows.Xps.Packaging
 Class Window1
       Private Sub Window1_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
         Dim Document As New XpsDocument("C:\\C.xps", IO.FileAccess.Read)
        DocumentViewer1.Document = Document.GetFixedDocumentSequence()
     End Sub
End Class

you also need to add a reference to ReachFramework.DLL in order to use XPS its in the add reference list though so thats not too much trouble. The document view is simply a control as you can see:



```
<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="389" Width="654">
    <Grid>
        <DocumentViewer Name="DocumentViewer1" />
    </Grid>
</Window>
```

^ I love my Xaml parsing engine...

Any questions?

----------


## chris128

> ^ I love my Xaml parsing engine...


haha shameless plug :P but yeah it is good!  :Smilie: 

As for not being able to hide the XPS documents, couldnt you just store them in the database and download them from there when the user hits the help button? I guess it wouldnt work very well if they are large file sizes though...

----------


## DeanMc

Thats what I was thinking, although you could just rename it to .help and im sure it would still read.

----------


## bflosabre91

i just got to that point like probably 10 minutes ago. lol. So looks like we both came to the same conclusion :P I never was aware of this XPS stuff until now, but i really appreciate your help. I'm also going to override the controltemplate to get rid of the copy and print buttons on the document viewer and i think i will be able to not allow selection of the text as well. so that should be locked down enough. if they want to screen capture 100 pages, then they can be my guest  :Smilie:  thanks alot for both your help.

----------


## bflosabre91

im am going to test the help files over the DB but im not sure how well it will work over the network. some pages have images and stuff on them so it might be slow.

----------


## DeanMc

If your overriding the template you could add in a navigation pane, that way you can simply break the files into pages and store them that way in a db. I imagine a page of parsed information would not be that hard on the connection?

----------


## DeanMc

I actually had not looked into it either but it essentially means you can have any information that you want in your wpf application providing you can print it which is pretty amazing!

----------


## chris128

and you get the XPS printer installed with the office 07 compat pack like I said dont you? Or did you get it from somewhere else?

----------


## bflosabre91

yeah, thats pretty much what im going to do but im going to build it outside of the documentviewer and split up the huge help file into relevant sections.  it will be used in a few states across the country so i will have to see. last time when trying to do something like this with images, it did not turn out so well..

----------


## DeanMc

Actually I got it after installing Office 07 but yeah you do get it from that way. It's a pity Microsoft doesn't push things like this enough!

----------


## DeanMc

> yeah, thats pretty much what im going to do but im going to build it outside of the documentviewer and split up the huge help file into relevant sections.  it will be used in a few states across the country so i will have to see. last time when trying to do something like this with images, it did not turn out so well..


That makes sense and also the power of word would be silly to overlook when it comes to making nice documents!

----------


## bflosabre91

chris- im not sure when the xps printer came up for me but i assume that when it did. but i didnt know it would show up as an actual printer so it took me forever to get to that point.  :Smilie:

----------


## DeanMc

Ha ha yeah it is a software printer which makes the most confusing sense yet!

----------

