Display a PDF in WPF Application

101,208

Solution 1

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;

Solution 2

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.

Solution 3

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

Solution 4

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.

Solution 5

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.

Share:
101,208
azamsharp
Author by

azamsharp

Mohammad Azam is an iOS Instructor at DigitalCrafts. Before joining DigitalCrafts Azam worked as a senior mobile developer at Blinds.com, A Home Depot company. Azam led the mobile team at Blinds.com to develop the Home Depot blinds/shades online experience. Previously, Azam has worked as a team lead for many large companies including Schlumberger, Baker Hughes, AIG and VALIC. Azam has also published 8-10 personal apps to the App Store, including Vegetable Tree - Gardening Guide which was featured by Apple as the best gardening app in the App Store. Azam is also active on YouTube and maintains his popular channel "AzamSharp" where he shares his iOS knowledge. Azam is also a Udemy instructor where he has published courses on "Swift 2.0" and "iOS MapKit Development Using Swift Language". Azam also frequently contributes iOS articles to the Code Magazine and talks at different iOS conferences all around the country. Before entering the iOS community Azam was an active member of the Microsoft .NET community. Azam was also awarded Microsoft MVP award due to his contributions in the .NET community. Azam is also an instructor on Udemy with more than 2000 students. Azam has earned perfect ratings for his courses on Udemy. You can check out Azam's courses using the link below: http://www.azamsharp.com/courses/ When not developing iOS applications Azam likes to spend time with his family and plan his next trip to the unknown corners of the world.

Updated on September 26, 2020

Comments

  • azamsharp
    azamsharp over 3 years

    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