在 WPF 应用程序中显示 PDF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/55083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display a PDF in WPF Application
提问by azamsharp
Any ideas how to display a PDF file in a WPF Windows Application?
任何想法如何在 WPF Windows 应用程序中显示 PDF 文件?
I am using the following code to run the browser but the Browser.Navigate
method does not do anything!
我正在使用以下代码运行浏览器,但该Browser.Navigate
方法没有执行任何操作!
WebBrowser browser = new WebBrowser();
browser.Navigate("http://www.google.com");
this.AddChild(browser); // this is the System.Windows.Window
采纳答案by Gulzar Nazim
Oops. this is for a winforms app. Not for WPF. I will post this anyway.
哎呀。这是一个 winforms 应用程序。不适用于 WPF。无论如何我会发布这个。
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;
回答by azamsharp
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:
您可以使用 WindowsFormHost 控件使 Acrobat Reader 控件在 WPF 应用程序中工作。我在这里有一篇关于它的博客文章:
http://hugeonion.com/2009/04/06/displaying-a-pdf-file-within-a-wpf-application/
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:
我还有一个 5 分钟的截屏视频,介绍了我是如何在这里制作的:
回答by Guy Starbuck
You could simply host a Web Browser control on the form and use it to open the PDF.
您可以简单地在表单上托管一个 Web 浏览器控件并使用它打开 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.
.NET 3.51 中有一个新的本机 WPF“WebBrowser”控件,或者您可以在 WPF 应用程序中托管 Windows.Forms 浏览器。
回答by VahidN
Try MoonPdfPanel - A WPF-based PDF viewer control
http://www.codeproject.com/Articles/579878/MoonPdfPanel-A-WPF-based-PDF-viewer-control
试试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
GitHub:https: //github.com/reliak/moonpdf
回答by VahidN
The following code expects Adobe Reader to be installed and the Pdf extension to be connected to this. It simply runs it:
以下代码需要安装 Adobe Reader 并将 Pdf 扩展连接到此。它只是运行它:
String fileName = "FileName.pdf";
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = fileName;
process.Start();
process.WaitForExit();
回答by odyth
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.
如果您不清理它,那么您可能会遇到内存泄漏问题,具体取决于您使用的 .NET 版本。如果我不清理,我会在 .NET 3.5 中看到严重的内存泄漏。
回答by Frank Rem
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.
我意识到已经接受了一个答案,但以下不需要 Adobe Reader/Acrobat,它是一个 WPF 解决方案 - 与 Winforms 相反。我也意识到这是一个老问题,但它刚刚更新,所以我想它仍然是实际的。
PDFRasterizer.NET 3.0allows 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.
PDFRasterizer.NET 3.0允许您渲染到 WPF FixedDocument。它保留所有矢量图形(PDF 图形被转换为或多或少等效的 WPF 元素。这可能最接近您的需要。
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.
您可以将 wpfDoc 传递给例如 WPF DocumentViewer 以快速实现查看器。
回答by Mohammad
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.
您也可以使用 FoxitReader。它是免费的,并带有一个 ActiveX 控件,在您安装 FoxitReader 应用程序后,该控件会在 Web 浏览器(IE 和其他)中注册。因此,在系统上安装 FoxitReader 后,放置一个 WebBrowser Control 并将其 Source 属性设置为指向 PDF 文件的文件路径。
回答by GEOCHET
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.
看看这个:http: //itextsharp.sourceforge.net/您可能必须使用 WindowsFormsHost,但由于它是开源的,您可以在 WPF 中使它更优雅一些。