Richeditor使用

敖永丰
2023-12-01

一、简单的读入xmal,html,cs,docx 数据:

 private static readonly string CsResource = "下载说明.htm";

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

            Uri resourceUri = new Uri(resourcePath, UriKind.Relative);

            return resourceUri;
        }

(2) using ( Stream stream = Application.GetResourceStream(
                     GetResourceUri(Demo.MainPage.CsResource)).Stream)
            {
                this.Editor.Document.LayoutMode = DocumentLayoutMode.Flow;
                IDocumentFormatProvider xamlProvider = DocumentFormatProvidersManager.GetProviderByExtension(".html");//可以根据内容不同,修改对扩展名的支持

             //-----------------------------------中文处理-----------------------------------------
                StreamReader reader = new StreamReader(stream, new USTC.Gb2312Encoding());
                UTF8Encoding utf8 = new UTF8Encoding();
                byte[] encodedBytes = utf8.GetBytes(reader.ReadToEnd());

            //--------------------------------------------------------------------------------
                this.Editor.Document = xamlProvider.Import(encodedBytes);
                        }

 

二、对文档结构的解析:

       private void LoadSampleCsDocument()
        {
            if (this.CsDocument == null)
            {
                Stream csStream = Application.GetResourceStream(
                    GetResourceUri(Demo.MainPage.CsResource)).Stream;

                using (StreamReader reader = new StreamReader(csStream, new USTC.Gb2312Encoding()))
                {
                   
                    this.CsDocument = CreateFormattedDocument(reader.ReadToEnd(), ".cs");
                }
            }

            this.Editor.Document = this.CsDocument;
            this.Editor.Document.ShowFormattingSymbols = false;// ToggleFormattingSymbols.IsChecked ?? true;
            this.Editor.UpdateEditorLayout();
        }

        private RadDocument CreateFormattedDocument(string text, string fileFormat)
        {
            RadDocument document = new RadDocument();
            document.LayoutMode = DocumentLayoutMode.Flow;

/首先生成section

            Telerik.Windows.Documents.Model.Section section = new Telerik.Windows.Documents.Model.Section();
            document.Sections.Add(section);

            Tokenizer tokenizer = new Tokenizer();
            string[] tokens = tokenizer.TokenizeCode(text, fileFormat);

其次生成 Paragraph   ,在 Paragraph 下可以进一步添加更多的详细元素。。

            Paragraph currentParagraph = new Paragraph();
            currentParagraph.SpacingAfter = 0;
            section.Paragraphs.Add(currentParagraph);
            foreach (string token in tokens)
            {
                string[] lines = Regex.Split(token, Environment.NewLine);
                bool createParagraph = false;
                foreach (string line in lines)
                {
                    if (createParagraph)
                    {
                        currentParagraph = new Paragraph();
                        currentParagraph.SpacingAfter = 0;
                        section.Paragraphs.Add(currentParagraph);
                    }
                    createParagraph = true;
                    if (!string.IsNullOrEmpty(line))
                    {
                        Span span = new Span();// token.GetSpanStyle();
                        span.FontWeight = FontWeights.Bold;
                        span.Text = line;
                        currentParagraph.Inlines.Add(span);
                    }
                }
            }
            document.CaretPosition.Reset();
            return document;
        }

 

三、打开本地文档:

   private void button1_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Word Document (*.docx)|*.doc|Web Page (*.html)|*.html;*.htm|XAML File (*.xaml)|*.xaml|Text File (*.txt)|*.txt";
            if (ofd.ShowDialog() == true)
            {
                using (Stream stream = ofd.File.OpenRead())
                {
                    this.LoadDocument(stream, ofd.File.Extension);
                }
            }

        }

 

 private void LoadDocument(Stream stream, string extension)
        {
            RadDocument doc;

            IDocumentFormatProvider provider = DocumentFormatProvidersManager.GetProviderByExtension(extension);
            if (provider != null)
            {
                StreamReader reader = new StreamReader(stream, new USTC.Gb2312Encoding());
                UTF8Encoding utf8 = new UTF8Encoding();
                byte[] encodedBytes = utf8.GetBytes(reader.ReadToEnd());

                doc = provider.Import(encodedBytes);
            }
            else
            {
                MessageBox.Show("Unknown format.");
                return;
            }

            doc.Measure(RadDocument.MAX_DOCUMENT_SIZE);
            doc.Arrange(new RectangleF(PointF.Empty, doc.DesiredSize));

            doc.LayoutMode = DocumentLayoutMode.Paged;

            doc.DefaultPageLayoutSettings.Width = 320 / 0.7F;
            doc.DefaultPageLayoutSettings.Height = 390 / 0.7F;
            doc.SectionDefaultPageMargin = new Padding(55);
            doc.Sections.First.PageMargin = new Padding(55);

            doc.UpdateLayout();
            Editor.Document = doc;
        }


     

 类似资料: