在WPF应用程序中显示PDF不工作 - WebBrowser或Adobe Control

JTo*_*and 4 c# pdf wpf xaml

我需要在WPF应用程序中显示PDF.从我在网上做的所有阅读中,似乎在WPF应用程序中显示PDF的[仅?]方式是通过Adobe的控件或WebBrowser控件.我试过使用Adobe的控件,但是,我一直无法添加Reader控件,因为由于某些原因我无法找到它作为我可以添加到我的工具箱中的东西(即使添加了所需的引用).我正在运行Windows 7(64位),VS2010,.NET 4.0,并安装了Adobe Acrobat 7.0 Professional和Adobe Acrobat 9 Pro Extended,如果这与它有关.所以无论如何,我决定在WindowsFormsHost中托管的WebBrowser控件中尝试它.我有的XAML是这样的:

<WindowsFormsHost x:Name="FormsHost" Visibility="Visible" Grid.Column="1" Margin="7,0,0,0">
<WF:WebBrowser x:Name="WebBrowser" Dock="Fill" IsWebBrowserContextMenuEnabled="False" ScriptErrorsSuppressed="True" WebBrowserShortcutsEnabled="False" Margin="7,0,0,0" />
</WindowsFormsHost>
Run Code Online (Sandbox Code Playgroud)

然后在C#代码后面:

WebBrowser.Navigate(new System.Uri(FileName));
Run Code Online (Sandbox Code Playgroud)

其中FileName ==我需要显示的.pdf文件的确切位置.但是,当我运行这个时,我看到的是WebBrowser所在的完全空白的白色区域.我也试过像这样设置.pdf文件:

WebBrowser.Url = new System.Uri(FileName);
Run Code Online (Sandbox Code Playgroud)

我得到了完全相同的东西.我知道PDF正在正确的位置创建,因为我可以手动浏览到它并打开它.

有没有人有任何想法,为什么这不起作用?或者可能为什么我似乎没有Reader控件作为选项?在这一点上,任何解决方案都没问题,他们似乎都没有工作!

谢谢!

小智 7

这就是我做的......

主窗口XAML

 <Window x:Class="PDFView.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="PDFView">
        <Grid>
            <WebBrowser x:Name="Browser" />
        </Grid>
    </Window>
Run Code Online (Sandbox Code Playgroud)

主要窗口代码背后

using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Navigation;

namespace PDFView {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private const string hostUri = "http://localhost:8088/PsuedoWebHost/";

        private HttpListener _httpListener;

        public MainWindow() {
            InitializeComponent();

            this.Loaded += OnLoaded;
        }

        /// <summary>
        /// Loads the specified PDF in the WebBrowser control
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="routedEventArgs"></param>
        private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
        {
            // Get the PDF from the 'database'
            byte[] pdfBytes = GetPdfData();

            // Create a PDF server that serves the PDF to a browser
            CreatePdfServer(pdfBytes);

            // Cleanup after the browser finishes navigating
            Browser.Navigated += BrowserOnNavigated;
            Browser.Navigate(hostUri);
        }

        /// <summary>
        /// Retrieve a byte array from a 'database record'
        /// </summary>
        /// <returns></returns>
        private byte[] GetPdfData() {
            // TODO: Replace this code with data access code
            // TODO: Pick a file from the file system for this demo.
            string path = @"c:\Users\Me\Documents\MyPDFDocument.pdf"; 
            byte[] pdfBytes = File.ReadAllBytes(path);

            // Return the raw data
            return pdfBytes;
        }

        /// <summary>
        /// Creates an HTTP server that will return the PDF  
        /// </summary>
        /// <param name="pdfBytes"></param>
        private void CreatePdfServer(byte[] pdfBytes) {
            _httpListener = new HttpListener();
            _httpListener.Prefixes.Add(hostUri);
            _httpListener.Start();
            _httpListener.BeginGetContext((ar) => {
                                                  HttpListenerContext context = _httpListener.EndGetContext(ar);

                                                  // Obtain a response object.
                                                  HttpListenerResponse response = context.Response;
                                                  response.StatusCode = (int)HttpStatusCode.OK;
                                                  response.ContentType = "application/pdf";

                                                  // Construct a response.
                                                  if (pdfBytes != null) {
                                                      response.ContentLength64 = pdfBytes.Length;

                                                      // Get a response stream and write the PDF to it.
                                                      Stream oStream = response.OutputStream;
                                                      oStream.Write(pdfBytes, 0, pdfBytes.Length);
                                                      oStream.Flush();
                                                  }

                                                  response.Close();
                                              }, null);

        }

        /// <summary>
        /// Stops the http listener after the browser has finished loading the document
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="navigationEventArgs"></param>
        private void BrowserOnNavigated(object sender, NavigationEventArgs navigationEventArgs)
        {
            _httpListener.Stop();
            Browser.Navigated -= BrowserOnNavigated;
        }

    }
}
Run Code Online (Sandbox Code Playgroud)