28

Any ideas how to display a PDF file in a WPF Windows Application?


I am using the following code to run the browser but the Browser.Navigate method does not do anything!

WebBrowser browser = new WebBrowser();
browser.Navigate("http://www.google.com");
this.AddChild(browser); // this is the System.Windows.Window
Bobrovsky
  • 13,789
  • 19
  • 80
  • 130
azamsharp
  • 19,710
  • 36
  • 144
  • 222
  • Is it for launching externally in Acrobat or you need a design time control for displaying inside the application? – Gulzar Nazim Sep 10 '08 at 19:30
  • Do you want to display the PDF in WPF? – Aaron Fischer Sep 10 '08 at 19:25
  • 1
    Instead of `WebBrowser` control, I have [exported PDF to XpsDocument](https://www.gemboxsoftware.com/pdf/examples/pdf-xpsdocument-wpf/1001) and used WPF's `DocumentViewer` control for a display. – hertzogth Apr 06 '20 at 03:30

9 Answers9

18

You can get the Acrobat Reader control working in a WPF app by using the WindowsFormHost control. I have a blog post about it here:

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

I also have a 5 minute screencast of how I made it here:

http://www.screencast.com/t/JXRhGvzvB

  • 1
    i getting a error when i does the same `Could not load file or assembly 'Interop.AcroPDFLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.` – joshua Jul 21 '12 at 06:21
  • very nice, thanks i used it and it works! – Hamoudy Apr 25 '13 at 18:43
  • Thank you so much for sharing this with the community. Wish all answers came in video format! :) – B.K. Sep 06 '13 at 23:20
  • 3
    Link to blog post is dead. – Jonas Sourlier Sep 06 '16 at 10:30
  • @cheeesus look google [cache](https://webcache.googleusercontent.com/search?q=cache:7DLZWclYCZYJ:https://hugeonion.wordpress.com/2009/04/06/displaying-a-pdf-file-within-a-wpf-application/+&cd=1&hl=tr&ct=clnk&gl=tr&client=opera) – EgoistDeveloper Sep 11 '17 at 23:16
  • And this using Acrobat PDF Lib,but problem doesn't everyone this plugin install. – EgoistDeveloper Sep 11 '17 at 23:21
13

You could simply host a Web Browser control on the form and use it to open the PDF.

There's a new native WPF "WebBrowser" control in .NET 3.51, or you could host the Windows.Forms browser in your WPF app.

Guy Starbuck
  • 21,603
  • 7
  • 53
  • 64
  • Does that need Acrobat Reader instaled on end users computer ? – Rasto Jan 08 '11 at 18:39
  • In this case the end user will need some sort of PDF reader software installed and registered with IE. I recommend Foxit Reader rather than Adobe, it is quicker and doesn't install spyware. – Guy Starbuck Jan 10 '11 at 14:12
  • 11
    +1; by far the simplest solution: `` – jeroenh May 23 '11 at 09:52
  • +1 that one is simplest,awesome – joshua Jul 21 '12 at 06:28
  • 2
    Update in 2016, I don't recommend Foxit anymore (it is now bloated), I use sumatra pdf. But Windows 10 Edge browser now has built-in PDF support, so it is most likely not necessary for any 3rd part PDF software installation on Windows – Guy Starbuck Sep 15 '16 at 17:07
9

Try MoonPdfPanel - A WPF-based PDF viewer control http://www.codeproject.com/Articles/579878/MoonPdfPanel-A-WPF-based-PDF-viewer-control

GitHub: https://github.com/reliak/moonpdf

B.K.
  • 9,982
  • 10
  • 73
  • 105
VahidN
  • 18,457
  • 8
  • 73
  • 117
9

Oops. this is for a winforms app. Not for WPF. I will post this anyway.

try this

private AxAcroPDFLib.AxAcroPDF axAcroPDF1;
this.axAcroPDF1 = new AxAcroPDFLib.AxAcroPDF();
this.axAcroPDF1.Dock = System.Windows.Forms.DockStyle.Fill;
this.axAcroPDF1.Enabled = true;
this.axAcroPDF1.Name = "axAcroPDF1";
this.axAcroPDF1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axAcroPDF1.OcxState")));
axAcroPDF1.LoadFile(DownloadedFullFileName);
axAcroPDF1.Visible = true;
Mo Patel
  • 2,321
  • 4
  • 22
  • 37
Gulzar Nazim
  • 51,744
  • 26
  • 128
  • 170
  • Does this work even if there is no Acrobat reader instaled on users system ? – Rasto Jan 08 '11 at 18:34
  • 1
    Note: This will not work if your application has been built as a 64bit application. You will have to force your app to be built as a 32bit app. (Same issue exists if you try and do this using the WindowsFormHost control in WPF) – Raj Rao Jun 24 '13 at 19:48
8

Just use a frame and a webbrowser like so

Frame frame = new Frame();
WebBrowserbrowser = new WebBrowser();
browser.Navigate(new Uri(filename));
frame.Content = browser;

Then when you don't need it anymore do this to clean it up:

WebBrowser browser = frame.Content as WebBrowser;
browser.Dispose();
frame.Content = null;

If you don't clean it up then you might have memory leak problems depending on the version of .NET your using. I saw bad memory leaks in .NET 3.5 if I didn't clean up.

odyth
  • 4,324
  • 3
  • 37
  • 45
8

The following code expects Adobe Reader to be installed and the Pdf extension to be connected to this. It simply runs it:

String fileName = "FileName.pdf";
System.Diagnostics.Process process = new System.Diagnostics.Process(); 
process.StartInfo.FileName = fileName;
process.Start();
process.WaitForExit();
davidsleeps
  • 9,393
  • 11
  • 59
  • 73
1

Disclosure: Here is a commercial one and I work for this company.

I realize that an answer has already been accepted but the following does not require Adobe Reader/Acrobat and it is a WPF solution - as opposed to Winforms. I also realize this is an old question but it has just been updated so I guess it is still actual.

PDFRasterizer.NET 3.0 allows you to render to a WPF FixedDocument. It preserves all vector graphics (PDF graphics are converted to more or less equivalent WPF elements. This is probably closest to what you need.

using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read))
{
  pdfDoc = new Document(file);

  ConvertToWpfOptions convertOptions = new ConvertToWpfOptions();
  RenderSettings renderSettings = new RenderSettings();
  ...

  FixedDocument wpfDoc = pdfDoc.ConvertToWpf(renderSettings, convertOptions, 0, 9, summary);
}

You can pass the wpfDoc to e.g. the WPF DocumentViewer to quickly implement a viewer.

Frank Rem
  • 3,632
  • 2
  • 25
  • 37
0

You can also use FoxitReader. It's free and comes with an ActiveX control that registers in the web browsers (IE and others) after you install the FoxitReader application. So after you install FoxitReader on the system put a WebBrowser Control and set its Source property to point to the file path of your PDF file.

Mohammad
  • 1,930
  • 1
  • 21
  • 31
-1

Check this out: http://itextsharp.sourceforge.net/ You may have to use a WindowsFormsHost, but since it is open source, you might be able to make it a little more elegant in WPF.

GEOCHET
  • 21,119
  • 15
  • 74
  • 98