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.
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)
{
InitializeComponent();
this.axAcroPDF1.LoadFile(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()
{
InitializeComponent();
// 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.

May 19, 2009 at 7:53 pm
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.
May 28, 2009 at 12:43 am
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.
October 7, 2009 at 10:59 am
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.
Thanks,
shaun
October 9, 2009 at 9:40 am
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!
October 12, 2009 at 4:04 pm
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?
October 23, 2009 at 2:56 pm
Christopher,
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!
October 25, 2009 at 3:02 pm
Great find Shaun and thanks for the update! I will make sure all users are on version 9.2+ of Acrobat.
October 25, 2009 at 3:04 pm
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!
October 25, 2009 at 11:24 pm
HOW the same thing it would be programmed but with the language of VISUAL BASIC?
Please!!!
October 27, 2009 at 12:18 pm
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.
November 5, 2009 at 3:00 pm
Still question…but would you mind exporting your current font and color sheme and sending it my way?
November 9, 2009 at 2:09 pm
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:
this.Controls.Remove(this.pdfView);
Thread.Sleep(1000);
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.
Thanks,
Shaun
December 29, 2009 at 6:11 pm
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
Thank’s
Ram
February 2, 2010 at 11:31 am
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.
February 2, 2010 at 11:33 am
My last post remove my tags. Before component it is suppose to say canvas component.
February 2, 2010 at 4:35 pm
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.
February 3, 2010 at 2:46 pm
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?
February 3, 2010 at 2:49 pm
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.
February 3, 2010 at 2:59 pm
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)
February 20, 2010 at 2:42 am
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:
http://social.msdn.microsoft.com/Forums/en-US/csharpide/thread/cfb20a30-855b-4d29-8bbe-d48f928643d8
February 20, 2010 at 5:36 am
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?
February 25, 2010 at 1:18 pm
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.
May 13, 2010 at 3:39 am
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.
May 13, 2010 at 11:30 am
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.
June 4, 2010 at 1:36 am
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.
April 21, 2011 at 12:11 pm
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.
April 28, 2011 at 7:52 am
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.
May 13, 2011 at 10:13 am
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!
November 23, 2011 at 10:49 am
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.
Jorge.
November 30, 2011 at 1:26 am
How Can i highlight a specific text in the pdfviewer
June 30, 2012 at 8:40 am
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.