[英]Opening a PDF file from within a WPF application
I have a WPF application in which the GUI displays a few different aspects of the application to the user, using a different tab
for each part of the application.我有一个 WPF 应用程序,其中 GUI 向用户显示应用程序的几个不同方面,为应用程序的每个部分使用不同的
tab
。 I am now looking to add the functionality to load and view document from within the application on one of the tabs.我现在希望在其中一个选项卡上添加从应用程序中加载和查看文档的功能。
I have added a DocumentViewer
to the tab, and can see that it is displayed in the GUI when I run my application, but I'm not sure how to get that DocumentViewer
to load/ display a document, and can't seem to find any method calls/ markup that enable you to do this.我在选项卡中添加了一个
DocumentViewer
,当我运行我的应用程序时可以看到它显示在 GUI 中,但我不确定如何让DocumentViewer
加载/显示文档,并且似乎无法找到使您能够执行此操作的任何方法调用/标记。
The XAML markup I am using to add the DocumentViewer
to my application is:我用来将
DocumentViewer
添加到我的应用程序的 XAML 标记是:
<TabItem Header="Document Viewer">
<StackPanel>
<DocumentViewer x:Name="docViewer" Height="643" Margin="0,0,-0.4,0"/>
<DocumentViewer x:Name="documentViewer" Height="1" Margin="0,0,-0.4,0" RenderTransformOrigin="0.5,0.5">
<DocumentViewer.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleY="-1"/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</DocumentViewer.RenderTransform>
</DocumentViewer>
</StackPanel>
</TabItem>
How do I point this DocumentViewer
to a PDF (or .doc, or whatever) file that's located on my computer, so that it will load and display that document inside my application window?我如何将此
DocumentViewer
指向位于我计算机上的 PDF(或 .doc,或其他)文件,以便它加载并在我的应用程序窗口中显示该文档?
Similar question here .类似的问题在这里。 Wpf does no provide a base class for that and if you want to work around it you couod open the pdf in its own application using System.
Wpf 没有为此提供基类,如果您想解决它,您可以使用 System.open 在它自己的应用程序中打开 pdf。 Diagnostics.Process.Start (@"filename.pdf");
Diagnostics.Process.Start (@"filename.pdf");
You could also visit the link for other options.您还可以访问其他选项的链接。
GemBox libraries can convert files to XpsDocument
object which you could assign to DocumentViewer
control. GemBox 库可以将文件转换为
XpsDocument
对象,您可以将其分配给DocumentViewer
控件。
For example, here is how to do that for PDF with GemBox.Pdf:例如,以下是使用 GemBox.Pdf 对 PDF执行此操作的方法:
XpsDocument xpsDocument;
public MainWindow()
{
InitializeComponent();
using (var document = PdfDocument.Load("input.pdf"))
{
this.xpsDocument = document.ConvertToXpsDocument(SaveOptions.Xps);
this.docViewer.Document = this.xpsDocument.GetFixedDocumentSequence();
}
}
And for DOC with GemBox.Document: 对于带有 GemBox.Document 的DOC :
XpsDocument xpsDocument;
public MainWindow()
{
InitializeComponent();
var document = DocumentModel.Load(path);
this.xpsDocument = document.ConvertToXpsDocument(SaveOptions.XpsDefault);
this.docViewer.Document = this.xpsDocument.GetFixedDocumentSequence();
}
Note, in both cases the XpsDocument
object must stay referenced so that DocumentViewer
can access its resources.请注意,在这两种情况下,
XpsDocument
对象都必须保持被引用,以便DocumentViewer
可以访问其资源。 Otherwise, GC will collect/dispose XpsDocument
and DocumentViewer
will no longer work.否则,GC 将收集/处理
XpsDocument
并且DocumentViewer
将不再工作。
I recommend using a free PDF Lib for c#.我建议为 c# 使用免费的 PDF 库。
http://www.e-iceblue.com/Introduce/free-pdf-component.html#.V0RVLfmLRpg is a good example! http://www.e-iceblue.com/Introduce/free-pdf-component.html#.V0RVLfmlRpg就是一个很好的例子!
To view PDFs in WPF converting single pages to image and show that is a good solution.在 WPF 中查看 PDF 将单页转换为图像并显示这是一个很好的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.