how to open docx file..?

2 posts, 0 answers
  1. Rahul
    Rahul avatar
    4 posts
    Member since:
    Jul 2012

    Posted 25 Jul 2012 Link to this post

    hi,

    I want to open docx, txt, html file in my project,

    for html file and txt file i have done like this..

    My XAML COde

            <telerik:RadRichTextBox Name="editor" Grid.Row="1" IsReadOnly="True">
                <telerik:RadRichTextBox.Resources>
                    <telerikHtml:HtmlDataProvider x:Name="htmlDataProvider" RichTextBox="{Binding ElementName=editor}" Html="{Binding OrignalData}"/>
                </telerik:RadRichTextBox.Resources>
            </telerik:RadRichTextBox>

    My ViewModel Code

     private string _orignalData;
            public string OrignalData
            {
                get { return _orignalData; }
                set { _orignalData = value; }
            }

            public ViewModel()
            {
                string resource = "test.html";
                Uri uri = GetResourceUri(resource);
                using (Stream stream = Application.GetResourceStream(uri).Stream)
                {
                    StreamReader reader = new StreamReader(stream);
                    this.OrignalData = reader.ReadToEnd();
                }
            }

            private static Uri GetResourceUri(string resource)
            {
                AssemblyName assemblyName = new AssemblyName(typeof(MainPage).Assembly.FullName);
                string resourcePath = "/" + assemblyName.Name + ";component/" + resource;
                Uri resourceUri = new Uri(resourcePath, UriKind.Relative);

                return resourceUri;
            }

    now which provider i have to use in xaml to open docx file, is it possible to write only a common provider which can open all the fles for me.

    regards
    rahul
  2. Martin Ivanov
    Admin
    Martin Ivanov avatar
    104 posts

    Posted 26 Jul 2012 Link to this post

    Hello,

    In order to open Docx you need DocxDataProvider.

    However, there is no universal data provider for all formats. I am sending you a similar scenario for opening a document with suitable format provider. Small changes would work for data provider as well.
    OpenFileDialog fd = new OpenFileDialog();
    fd.ShowDialog();
     
    bool isSuccessfullyParseed = false;
     
    byte[] content = new byte[fd.File.Length];
    fd.File.OpenRead().Read(content, 0, (int)fd.File.Length);
    RadDocument document = null;
     
    try
    {
        XamlFormatProvider xaml = new XamlFormatProvider();
        document = xaml.Import(content);
        isSuccessfullyParseed = true;
    }
    catch (FormatException fe)
    {
    }
     
    if (!isSuccessfullyParseed)
    {
        try
        {
            DocxFormatProvider docx = new DocxFormatProvider();
            document = docx.Import(content);
            isSuccessfullyParseed = true;
        }
        catch (FormatException fe)
        {
        }
    }
     
    if (!isSuccessfullyParseed)
    {
        try
        {
            RtfFormatProvider rtf = new RtfFormatProvider();
            document = rtf.Import(content);
            isSuccessfullyParseed = true;
        }
        catch (FormatException fe)
        {
        }
    }
     
    if (!isSuccessfullyParseed)
    {
        try
        {
            PdfFormatProvider pdf = new PdfFormatProvider();
            document = pdf.Import(content);
            isSuccessfullyParseed = true;
        }
        catch (FormatException fe)
        {
        }
    }
     
    if (!isSuccessfullyParseed)
    {
        try
        {
            HtmlFormatProvider pdf = new HtmlFormatProvider();
            document = pdf.Import(content);
            isSuccessfullyParseed = true;
        }
        catch (FormatException fe)
        {
        }
    }
     
    if (!isSuccessfullyParseed)
    {
        TxtFormatProvider txt = new TxtFormatProvider();
        document = txt.Import(content);
     
    }
     
    this.radRichTextBox.Document = document;

    You can implement a data provider that uses this format provider in order to be able to bind all kinds of text. Note that you will have to think of some logic in order to determine what format the data provider should export the document to.

    Regards,
    Martin
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. DevCraft banner
Back to Top