WPF display PDF file (转)

http://hugeonion.com/2009/04/06/displaying-a-pdf-file-within-a-wpf-application/

 

 

Displaying a PDF file within a WPF application

April 6, 2009 — Christopher Hujanen

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:

1public partial class UserControl1 : UserControl
2{
3     public UserControl1(string filename)
4     {
5          InitializeComponent();
6 
7          this.axAcroPDF1.LoadFile(filename);
8     }
9}

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

1public Window1()
2{
3     InitializeComponent();
4 
5     // TODO: Make sure you point to a PDF on your system:
6     var uc = new UserControl1(@"C:\FoundationsOfProgramming.pdf");
7     this.windowsFormsHost1.Child = uc;
8}

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 on 2009-12-17 16:22  逝去的时光  阅读(802)  评论(2)    收藏  举报

导航