Question

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
Was it helpful?

Solution

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;

OTHER TIPS

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

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.

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();

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.

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.

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top