In UWP, how can I open a word document in a splitview content?

KaranKapoor29 81 Reputation points
2020-09-30T03:54:46.673+00:00

I wanted to navigate different word documents using a master-detail layout. So, how can I open a word doc in a content as a read only?

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,856 Reputation points
    2020-09-30T05:02:07.247+00:00

    Hello Welcome to Microsoft Q&A,

    In UWP, how can I open a word document in a splitview content?

    For your scenario , we suggest you use the Open XML SDK to achieve it, before using it, you need to install the DocumentFormat.OpenXml nuget package. Then using WordprocessingDocument.Open(Stream stream, bool isEditable) method to create a instance of WordprocessingDocument. If you want to display it in the splitview content, we suggest load Body's InnerText and display it with TextBlock.

    Code below

    private async void Button_Click(object sender, RoutedEventArgs e)
    {
        StorageFolder folder = KnownFolders.PicturesLibrary;
        StorageFile sampleFile = await folder.GetFileAsync("hello.docx");
    
        Stream randomAccessStream = await sampleFile.OpenStreamForWriteAsync();
        var text = OpenextToWordDocument(randomAccessStream);
    }
    public static string OpenextToWordDocument(Stream stream)
    {
        var text = string.Empty;
        // Open a WordprocessingDocument for editing using the stream.
        WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(stream, false);
        // Assign a reference to the existing document body.
        Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
        // Add new text.
        text = body.InnerText;
        wordprocessingDocument.Close();
        return text;
    }
    
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. KaranKapoor29 81 Reputation points
    2020-09-30T10:19:20.267+00:00

    Thank you for your guidance @Nico Zhu (Shanghai Wicresoft Co,.Ltd.) ,
    Is there any way I could display media along with the text from a MS Word Document?
    Would appreciate your help again.