Document support (rtf, doc, docx) for UWP/Windows

2020-07-17 06:35发布

问题:

How can I display documents (doc, docx, rtf) in an UWP app? The WebView isn't able to do this.

Other options would be calling an external application with Windows.System.Launcher.LaunchUriAsync (e.g. Word) or using a 3rd party library. The requirement is to have the data in the app, because you don't have control over it, if it's handled to another one. Another option would be to convert it to another format (e.g. PDF) which UWP can handle (not really).

Any ideas?

回答1:

If you would like to display word or pdf files in the UWP app you can use WebView control with Google Docs Viewer - I was using it for the online documents.

This is the code:

XAML:

<WebView x:Name="MyWebView"/>

Code behind:

public WebViewPage()
    {
        this.InitializeComponent();
        loadWebView();
    }

    void loadWebView()
    {
        var googleDocsViewer = "http://drive.google.com/viewerng/viewer?embedded=true&url=";
        var pdf = "http://www.uma.es/precarios/images/pdfs/wd-spectools-word-sample-04.doc";
        MyWebView.Navigate(new Uri(googleDocsViewer + pdf));
    }

I did not test it with local files, but I think that should also work. Please try and let me know.



回答2:

UWP can actually handle RTF files with RichEditBox - for more info about that see here in the official documentation (I don't know about RichTextBlock). For Docx support I can recommend a third-party library by Syncfusion, which you can get for free if you meet certain requirements.