This is a migrated thread and some comments may be shown as answers.

how to open docx file..?

1 Answer 201 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Rahul
Top achievements
Rank 1
Rahul asked on 25 Jul 2012, 07:30 AM
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

1 Answer, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 26 Jul 2012, 12:55 PM
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.

Tags
RichTextBox
Asked by
Rahul
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Share this question
or