从 WPF 应用程序中打开 PDF 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37410258/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:49:22  来源:igfitidea点击:

Opening a PDF file from within a WPF application

c#wpfdocumentviewer

提问by Noble-Surfer

I have a WPF application in which the GUI displays a few different aspects of the application to the user, using a different tabfor each part of the application. I am now looking to add the functionality to load and view document from within the application on one of the tabs.

我有一个 WPF 应用程序,其中 GUI 向用户显示应用程序的几个不同方面,对应用程序的tab每个部分使用不同的方面。我现在希望在其中一个选项卡上添加从应用程序中加载和查看文档的功能。

I have added a DocumentViewerto 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 DocumentViewerto 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 DocumentViewerto 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 DocumentViewerto 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 或其他)文件,以便它加载并在我的应用程序窗口中显示该文档?

回答by oziomajnr

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. Diagnostics.Process.Start (@"filename.pdf");

类似的问题 在这里。Wpf 没有为此提供基类,如果您想解决它,您可以使用 System.open 在它自己的应用程序中打开 pdf。Diagnostics.Process.Start (@"filename.pdf");

You could also visit the link for other options.

您还可以访问其他选项的链接。

回答by hertzogth

GemBox libraries can convert files to XpsDocumentobject which you could assign to DocumentViewercontrol.

GemBox 库可以将文件转换为XpsDocument您可以分配给DocumentViewer控件的对象。

For example, here is how to do that for PDFwith 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 DOCwith GemBox.Document:

对于DOC与GemBox.Document:

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 XpsDocumentobject must stay referenced so that DocumentViewercan access its resources. Otherwise, GC will collect/dispose XpsDocumentand DocumentViewerwill no longer work.

请注意,在这两种情况下,XpsDocument对象都必须保持被引用,以便DocumentViewer可以访问其资源。否则,GC 将收集/处理XpsDocument并且DocumentViewer不再工作。

回答by Felix D.

I recommend using a free PDF Lib for c#.

我建议为 c# 使用免费的 PDF 库。

http://www.e-iceblue.com/Introduce/free-pdf-component.html#.V0RVLfmLRpgis 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 将单页转换为图像并显示这是一个很好的解决方案。