Displaying a PDF file within a WPF application

I had a need to view PDF files directly within a WPF application and Stackoverflow and Google did not provide much assistance, so I set out on my own to see what I could create.  It turns out, it is not that difficult.  The trick is to use the WinForms support in WPF ala the WindowsFormHost control.  The application is so simple, I built it start to finish in under 5 minutes and captured it in a screencast for you to check out.

WPF PDF viewer app

The code is very simple, here’s the whopping 2 lines that I changed from the default usercontrol template:

public partial class UserControl1 : UserControl
     public UserControl1(string filename)


The only other code change I made is in creating an instance of the user control in the code behind of my WPF window:

public Window1()

     // TODO: Make sure you point to a PDF on your system:
     var uc = new UserControl1(@"C:\FoundationsOfProgramming.pdf");
     this.windowsFormsHost1.Child = uc;

The code is free for you to use: WpfPDFViewer.zip. Note it is a Visual Studio 2008 solution, and you must have Adobe Acrobat installed on your system of course.

Posted in .NET, Code, WPF. 40 Comments »

40 Responses to “Displaying a PDF file within a WPF application”

  1. DeanMc Says:

    Hi Chris,

    Great example, could I ask what you use to display your code? I’ve been looking for a good code formatter but cannot find one.

  2. Christopher Hujanen Says:

    Hi Dean — For source code formatting, I am using the Syntax Highlighter from Alex Gorbatchev, found at http://code.google.com/p/syntaxhighlighter/. Try it out… it works well for me. :)

  3. sespiritu Says:

    Hi, Chris.

    Very helpful example along with the video. Gave me a great starting point.
    I’ve got it implemented, but now what I’m finding is that everytime I load my application (and that control) I get a message: “The maximum number of PDF documents that can be simultaneous openned in the browser has been exceeded.”

    This doesn’t go away until I restart my machine.

    Have you run into this and if so how did you solve that? I’ve been searching all day and have seen the same quesion asked, but no solution.


  4. Christopher Hujanen Says:

    Hey Shaun — I have seen that exact issue. In particular on one machine, but not as often on others. I did not track it down as it’s not being used on that particular machine anymore, but it definitely seems like a resource is not getting released.

    I will post back here if I ever track it down, and if you find the issue, be sure to let me know.

    Thanks for bringing it up!

  5. christiandanis Says:

    Hi Christopher,

    Thanks a lot, this really helped, and your video is great. However, maybe you can help me take this to the next level, I need to display a pdf file in that viewer that is stored as binary data in a sql database. Any ideas?

  6. sespiritu Says:

    I think I found a solution to the “The maximum number of PDF documents that can be simultaneous openned in the browser has been exceeded.” issue.

    I found somewhere that the problem occurs ONLY if Adobe Reader 8 is installed. I uninstalled 8.x and installed 9.2 which I got from the Adobe site. I’ve been running all day without issue. That may just mean I haven’t hit the threshold, but if I do I’ll post back here.

    Good luck!

  7. Christopher Hujanen Says:

    Great find Shaun and thanks for the update! I will make sure all users are on version 9.2+ of Acrobat.

  8. Christopher Hujanen Says:

    Hi christiandanis — If I were to tackle such a thing, the first idea that comes to mind is to write out the binary data from SQL Server to a temporary file and then just use the control as is. Not sure if Adobe’s software allows feeding it a binary stream of data.

    Good luck!

  9. chemicalb0y Says:

    HOW the same thing it would be programmed but with the language of VISUAL BASIC?

    Please!!! :(

  10. sespiritu Says:

    More on the “The maximum number of PDF documents that can be simultaneous openned in the browser has been exceeded.” issue.

    I switched to 9.2 and while I never got the above error I found that if I opened up more than one PDF (or the same one twice) within the same application session on application exit I would get a popup that memory could not be accessed. I did not get this error when I only opened one. I’m thinking the last doc you opened will cleanup fine, but the previous are leaving something behind.

    I then uninstalled 9.2 and installed 7.0. I haven’t hit any seen either of these issues yet.

  11. chumad Says:

    Still question…but would you mind exporting your current font and color sheme and sending it my way? :)

  12. sespiritu Says:

    Another update to the memory leak issue in Adobe Reader 7, 8 and 9.

    On the Adobe forum I found to add this to the dispose logic of the control:


    The ThreadSleep is required, even though it does pause shutdown for a bit. The original post said 500 ms would be good, but I foudn 1000 ms was needed.


  13. ramsgotrus Says:

    Hi Christopher,
    Its really helpful App ..i appreciate if you could help me how to
    open word ,excel or power point doc in same way


  14. dcinadr Says:

    This is great! However I noticed something when trying to embed the usercontrol inside a component. The pdf will not display if it is embedded in a component. Also, it won’t display if the wpf window allows transparency. I had to set the AllowsTransparency to false and remove the usercontrol from component.

  15. dcinadr Says:

    My last post remove my tags. Before component it is suppose to say canvas component.

  16. alberstt Says:

    Hi Christopher,
    It’s really a good post about displaying PDF file within WPF.
    I have tried to follow your sample over Windows XP and it works perfectly. But when it runs over Windows 7, I got COMException – class not registered. I tried to register AcroPDF.DLL – PDF Browser Control with regsvr32, but failed. It looks like your sample is running under Windows 7, Could you let me know how do you get the PDF browser control registered. Thanks in advance.

  17. Christopher Hujanen Says:

    Hi dcinadr — I suspect these issues might be related to the fact that it is using the WindowsFormsHost control, but I am not sure. Do you happen to have some example code I could play with?

  18. Christopher Hujanen Says:

    Hi alberstt — I actually didn’t have to do anything special with Acrobat. I believe it registers itself by default when you install it. I must say the whole Acrobat browser control does not seem to be the most reliable code from Adobe, so it wouldn’t surprise me if it failed silently or has some other conflict on your system, but doesn’t give you any indication. :( Sorry I don’t have a good answer for you.

  19. dcinadr Says:

    I actually removed my code because I started using a third party component for displaying pdf files. The Acrobat Reader would not allow me to hide the navigation panel programmatically.
    But all you have to do is create a WPF window and set the AllowsTransparency to true. Then place your WindowsFormsHost control with the usercontrol embedded and run the app. It won’t display the loaded pdf. I’m pretty sure its really the WindowsFormsHost control that isn’t being displayed thus the reason the pdf isn’t showing.
    Another way to get this result is to simply put your WindowsFormsHost control iniside a canvas compenent (I don’t think AllowsTransparency needs to be true)

  20. brentgroves Says:

    If you need this app to work on x64 Vista and maybe Windows 7 make sure the build platform is set to x86. Refer to the following link for instructions:

  21. brentgroves Says:

    In the video one of the first steps that you do is add a reference to the acrobat COM library this library is not showing up in my COM tab. I downloaded the adobe 9.1 sdk and it comes with a Visual Studio 2005 app wizard setup program but I only have VC# Express and it won’t install. How did you get the Acrobat COM library installed?

  22. Christopher Hujanen Says:

    Hi Brent — I didn’t have to do anything (besides install Acrobat) to have the COM library show up. There might be a trick to re-register the library or something, but I checked on my new Windows 7 machine and it also shows up in the Visual Studio reference dialog. Sorry I don’t have a good answer for you.

  23. miketwalker Says:

    Thanks for this great article and the x64 advice, it worked like a charm. I see dcinadr ran into a similar issue as I did, there are options to mess with the toolbars and scrollbars (setShowToolbar and setShowScrollbars, respectively) but there doesn’t appear to be a way to hide the left side navigation panel. Have you, or anyone, found a solution to continue using the AxAcroPDFLib but get control of that and allow the program to hide it? Thanks again for your help.

  24. dcinadr Says:

    miketwalker – i don’t think it is possible to hide the navigation panel at this time. This is something they need to add to there api (they may have done that by now, not sure). I used a third party api (FoxitReader) which allows hiding the navigation panel. However this wasn’t perfect because it didn’t have a way of hiding the scroll bars. As a workaround i used panels to cover up the scroll bars.

  25. asusu Says:

    Nice example, so I use this for my project but now I have a problem is, when I press F5 to view in full screen (my function) but there’s no change because this pdf file catch that event, so how can I prevent pdf catch my F5 press key event.

  26. sfairbanks23 Says:

    This looked like just what I need, however when I downloaded, built and ran the application, I get a System.Windows.Markup.XamlParseException. It says there’s an error in markup file ‘Window1.xaml’ Line 1 Position 9. Any ideas? I have a pdf file ready to be viewed.

  27. Christopher Hujanen Says:

    Ah — I was able to reproduce the XamlParseException. It is due to the Adobe library not working with the build platform set to Any CPU or x64 on a 64 bit system. Just change your build settings platform to x86 and it should work.

  28. jtoland02 Says:

    This doesn’t seem to work for me. I’m running VS2010 with Windows 7 (64-bit). When I go to add the Adobe Reader Control to my Toolbox, it’s not listed there, even after added the correct reference. All I see is “Acrobat 3D Office Insert” and “Adobe Acrobat 7.0 Browser Control”. I tried the Browser Control listed, but when I try to create the control and load it (like you do above in “Window1”, I get the ever frustrating cross-thread exception. Any ideas about why the Reader control doesn’t appear for me? Or about how to fix the cross-thread exception? Thanks for this!

  29. Jorge Melgar Says:

    Hi Chris, this is post is really useful!
    Have you considered licensing issues regarding the use of Acrobat components? Are you distributing something similar as part of a proprietary solution? I’ve been doing some research and i can’t understand if i need to sign an agreement with adobe or not.
    Thanks in advance.


  30. Abhijit Pattnaik Says:

    How Can i highlight a specific text in the pdfviewer

  31. Frank Kusluski Says:

    There is a nice inexpensive control for viewing PDF files in WPF applications at http://www.getfilez.com/wpfpdf.pdf
    It requires Adobe Reader (any version) but allows you to control access to specific Adobe Reader features such as printing, saving, toolbar, document properties screen, text copying, etc.

  32. anil ghildiyal Says:

    Hello all,

    I have used Webbrowser, Adobe Reader winform and awesomium webcontrol for a pdf. For first two it was fine but i got stuck in rotation of WindowsFormsHost. It shows some error.
    Inspite of this when i go for awesomium webcontrol the issue was different, even here i am able to rotate my browser in any angle. but when i am trying to open any pdf on awesomium webcontrol, it displays a black screen instead of pdf..
    The above solution is good but not completing my requirement…
    Please assist me if you people have any resolution for this problem.

  33. Ahmed M. Gamil Says:

    Hey Christopher,

    Many thanks for your efforts.. Great work really but I need help for the following two points:

    1. Is there any possibility to disable/enable options for the displayed document (i.e enable/diable print, change the view type ..etc)?
    2. Can you open a password protected pdf file using this control by providing the password in the code?

    Any help would be appreciated. Waiting for your reply.

  34. fireman1923 Says:

    The download link doesn’t seem to be working, can you check it please?

  35. Christopher Hujanen Says:

    I just checked the download link and it worked for me. What kind of error are you getting?

  36. fireman1923 Says:

    It just won’t download,I don’t get any error message. Can you possibly mail me the archive to firejohndare@gmail.com?

  37. sdsharukh Says:

    im not getting acrobat in my com folder please help

  38. Embed a PDF into a WPF application | Zaharek Answers Says:

    […] a PDF in a WindowsFormsHost hosting an AxAcroPdf control, further to what's shown here. The problem is that my concentration sets AllowsTransparency = True to emanate a impression […]

  39. Santosh Ramamurthy Says:

    Hi Christopher…

    How to enable the markup toolbar after hosting the control. I am not able to find any method related to that.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

%d bloggers like this: