繁体   English   中英

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

[英]Opening a PDF file from within a WPF application

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

我在选项卡中添加了一个DocumentViewer ,当我运行我的应用程序时可以看到它显示在 GUI 中,但我不确定如何让DocumentViewer加载/显示文档,并且似乎无法找到使您能够执行此操作的任何方法调用/标记。

我用来将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>

我如何将此DocumentViewer指向位于我计算机上的 PDF(或 .doc,或其他)文件,以便它加载并在我的应用程序窗口中显示该文档?

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

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

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

例如,以下是使用 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();
    }
}

对于带有 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();
}

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

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

http://www.e-iceblue.com/Introduce/free-pdf-component.html#.V0RVLfmlRpg就是一个很好的例子!

在 WPF 中查看 PDF 将单页转换为图像并显示这是一个很好的解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM