将PDF查看器添加到WPF应用程序
我是WPF的新手,正在尝试将PDF查看器添加到WPF应用程序中,但似乎无法解决该问题.是在网上找到的,但由于各种原因,它们似乎对我没有用...
I am new to WPF, and am trying to add a PDF viewer to my WPF application, but can't seem to work out how to do it... I have tried following a couple of tutorials/ examples that I have found online, but they don't seem to work for me for varying reasons...
例如,我尝试按照以下教程进行操作: https://documentation.devexpress.com/# WPF/CustomDocument114328 在设计时添加PDF查看器-表示
For example, I tried following the tutorial at: https://documentation.devexpress.com/#WPF/CustomDocument114328 to add a PDF Viewer at Design Time- it says to
从DX.15.2中拖动PdfViewerControl:Data& Analytics Toolbox标签并将其拖放到主窗口中
drag the PdfViewerControl from the DX.15.2: Data & Analytics Toolbox tab and drop it onto the main window
但是,我似乎在工具箱中没有Data & Analytics
选项卡...有一个Data
选项卡,但是其中只有诸如Pointer,Chart,ListView等项目.我需要做些什么吗?如何在Visual Studio中添加/启用Data & Analytics
工具栏?
However, I don't seem to have a Data & Analytics
tab in the toolbox... there's a Data
tab, but that just has items like Pointer, Chart, ListView, etc. Is there something I need to do to add/ enable the Data & Analytics
toolbar in Visual Studio?
我尝试在以下位置遵循本教程: https://documentation.devexpress.com/#WPF/CustomDocument114329 通过以下方式添加PDF查看器代码-它说
I tried following the tutorial at: https://documentation.devexpress.com/#WPF/CustomDocument114329 to add a PDF Viewer via code- it says to
打开解决方案资源管理器,右键单击引用",然后选择添加引用..."以添加PDF Viewer库.
Open the Solution Explorer, right-click References and choose Add Reference... to add the PDF Viewer Library.
然后找到DevExpress.Data.v15.2,DevExpress.Pdf.v15.2.Core,DevExpress.Xpf.DocumentViewer.v15.2.Core和DevExpress.Xpf.PdfViewer.v15.2程序集并激活他们的复选框.
Then, locate the DevExpress.Data.v15.2, DevExpress.Pdf.v15.2.Core, DevExpress.Xpf.DocumentViewer.v15.2.Core, and DevExpress.Xpf.PdfViewer.v15.2 assemblies and activate their check boxes.
但是当我转到Add Reference
时,我在任何地方都找不到它提到的程序集,如果我搜索"它们,则找不到任何项目...
But when I go to Add Reference
, I can't find the assemblies it mentions anywhere, and if I 'search' for them, no items are found...
我是否缺少include
,还是需要从某个地方或某些地方导入一些库才能使用它们?
Am I missing an include
, or do I need to import some libraries from somewhere or something in order to use these?
我尝试过的另一种方法是: http ://www.codeproject.com/Articles/380019/Using-Adobe-Reader-in-a-WPF-app 其中说:
Another one I have tried is: http://www.codeproject.com/Articles/380019/Using-Adobe-Reader-in-a-WPF-app which says:
一旦将此控件添加到项目中,Windows窗体设计器应使用空白画布打开.您将需要打开工具框(CTRL + W,X).第一步,为自定义控件添加一个新选项卡是一个好主意-这是工具箱上上下文菜单中的一个选项.扩展此新选项卡后,从上下文菜单中选择选择项目".当出现选择工具箱项"对话框时,选择"COM组件"选项卡,然后选择"Adobe PDF Reader"(这会将AcroPDF.DLL添加到工具箱中).
Once this control is added to the project, the Windows Forms Designer should be open with a blank canvas. You will need to open the tool box (CTRL + W, X). As a first step it is a good idea to add a new tab for custom controls- this is an option from the context menu on the toolbox. With this new tab expanded, select "choose items" from the context menu. When the Choose Toolbox Items dialog appears, select the COM Components tab and select Adobe PDF Reader (this will add the AcroPDF.DLL to the toolbox).
但是我似乎找不到它所谈论的Choose Toolbox Items
或COM Components
...
But I can't seem to find the Choose Toolbox Items
or COM Components
it talks about...
谁能指出我更清晰的教程,或解释如何将PDF查看器添加到WPF应用程序?我正在使用Visual Studio 2015.
Can anyone point me to a clearer tutorial, or explain how I would add a PDF viewer to my WPF application? I am using Visual Studio 2015.
修改
我尝试通过执行以下操作在我的应用程序窗口中显示PDF文件:
I have tried to display the PDF file inside my application window, by doing the following:
添加<Grid>
以在XAML中将PDF显示到GUI:
Adding a <Grid>
to display the PDF to the GUI in the XAML:
<StackPanel>
<Grid x:Name="browserHost" Height="300" Width="525" Margin="0,0,0,0"></Grid>
</StackPanel>
在C#中的<Grid>
中添加WebBrowser
,并将其指向我要显示的PDF的位置:
Adding a WebBrowser
to the <Grid>
in the C#, and pointing that to the location of the PDF I want to display:
System.Windows.Controls.WebBrowser browser = new System.Windows.Controls.WebBrowser();
public MainWindow()
{
InitializeComponent();
try
{
//browser.Navigate("C:\\...\\sample.pdf");
browserHost.Children.Add(browser);
//browser.Visible = true;
browser.Navigate("C:\\...\\sample.pdf");
browserHost.Opacity = 200;
}catch(Exception e)
{
Console.WriteLine("browser is visible/ not: " + browserHost.Visibility);
}
}
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
/*Create the interop host control */
//System.Windows.Forms.Integration.WindowsFormsHost host = new System.Windows.Forms.Integration.WindowFormsHost();
/*Create the MaskedTextBox control */
//browser.Navigate("C:\\...\\sample.pdf");
//host.Child = browser;
browserHost.Children.Add(browser);
}
但是目前,当我运行我的应用程序时,一旦加载它,我添加到它的browser
就会显示一个页面,显示:
But currently, when I run my application, as soon as it loads, the browser
that I've added to it displays a page that says:
该网页的导航已被取消
Navigation to the webpage was canceled
并弹出一个对话框,询问我是否要打开或保存文件(sample.pdf
-我要在浏览器中显示的文件)...
and a dialog box pops up asking me if I want to open or save the file (sample.pdf
- the one I'm trying to display in the browser)...
为什么它试图下载文件而不是显示文件?如何获取browser
来显示文件而不是尝试下载文件?还是我应该在这里使用System.Windows.Controls.WebBrowser
以外的其他东西?
Why is it trying to download the file, rather than display it? How can I get the browser
to display the file instead of trying to download it? Or should I be using something other than a System.Windows.Controls.WebBrowser
here?
正如@NawedNabiZada所建议的那样,一种尝试而直接的方法是使用嵌入式InternetExplorer来显示Adobe PDF Reader
ActiveX控件.因此,假设您正在Windows上运行并且已安装Adobe PDF Reader
.
As already suggested by @NawedNabiZada, one tried and straightforward way is to use embedded InternetExplorer to show Adobe PDF Reader
ActiveX control. So it assumes you are running on Windows and have Adobe PDF Reader
installed.
然后创建一个包含以下控件的用户控件,窗口等:
Then you create a user control, window etc. that contains following control:
<WebBrowser x:Name="pdfWebViewer"></WebBrowser>
在构造函数中,导航至空白页:
In the constructor navigate to blank page:
pdfWebViewer.Navigate(new Uri("about:blank"));
使用以下简单代码将PDF文档加载到该控件中:
To load a PDF document to that control use this simple code:
pdfWebViewer.Navigate(fullPathToPDF);
许多Windows软件不仅使用WPF应用程序(包括SAP客户端)使用这种方法,而且存在一个隐藏的问题,请参见
This approach is used by many Windows software not only WPF apps including SAP client, but has a hidden problem, see this question.
必须启用Internet Explorer中的Adobe PDF Reader插件,此功能才能起作用. Acrobat Reader XI存在各种问题,最好使用DC版本.要启用Adobe PDF,请转到IE设置,附加组件并找到Adobe PDF Reader并将其启用(AR XI及更高版本).
The Adobe PDF Reader Addon in Internet Explorer must be enabled for this to work. There are various problems with Acrobat Reader XI, better to use DC version. To enable Adobe PDF go to IE settings, add-ons and find Adobe PDF Reader and enable it (AR XI and above).
对我来说,与您链接的代码项目文章相比,这是首选的方式.
For me this was the preferred way compared to the code project article you linked.