• silverlight中的richtextbox和rtf格式的转换


    silvergliht 4.0中有了richtextbox控件,但是它只接受xaml格式的输入,下面这个函数实现了rtf和xaml的转换,用到了flowdocument。

    需要注意的是直接从rtf转换过来的话有些attribute silverlight不认。我也不知道找全了没有,目前发现的是 section 里面的一堆attribute,然后就是margin和textindent。另外中文字体也会出错。

     

        1         /// <summary>

        2         /// convert the string "from" from sourceFormat to targetFormat

        3         /// </summary>

        4         /// <param name="from"></param>

        5         /// <param name="sourceFormat">System.Windows.DataFormats.Rtf</param>

        6         /// <param name="targetFormat">System.Windows.DataFormats.Xaml </param>

        7         /// <returns></returns>

        8         private string convertFormat(string from, string sourceFormat, string targetFormat)

        9         {

       10             string xaml = String.Empty;

       11             System.Windows.Documents.FlowDocument doc = new System.Windows.Documents.FlowDocument();

       12             System.Windows.Documents.TextRange range = new System.Windows.Documents.TextRange(doc.ContentStart, doc.ContentEnd);

       13 

       14             using (MemoryStream ms = new MemoryStream())

       15             {

       16                 using (StreamWriter sw = new StreamWriter(ms))

       17                 {

       18                     sw.Write(from);

       19                     sw.Flush();

       20                     ms.Seek(0, SeekOrigin.Begin);

       21                     range.Load(ms, sourceFormat);

       22                 }

       23             }

       24 

       25 

       26             using (MemoryStream ms = new MemoryStream())

       27             {

       28                 range = new System.Windows.Documents.TextRange(doc.ContentStart, doc.ContentEnd);

       29 

       30                 range.Save(ms, targetFormat);

       31                 ms.Seek(0, SeekOrigin.Begin);

       32                 using (StreamReader sr = new StreamReader(ms))

       33                 {

       34                     xaml = sr.ReadToEnd();

       35                 }

       36             }

       37 

       38             // remove all attribuites in section and remove attribute margin

       39             if (targetFormat.Equals(System.Windows.DataFormats.Xaml))

       40             {

       41                 int start = xaml.IndexOf("<Section");

       42                 int stop = xaml.IndexOf(">") + 1;

       43 

       44                 string section = xaml.Substring(start, stop);

       45 

       46                 xaml = xaml.Replace(section, "<Section xml:space=\"preserve\" HasTrailingParagraphBreakOnPaste=\"False\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">");

       47 

       48                 // remove Margin attribute

       49                 Regex reg = new Regex("Margin=\"[^\"]*\"");

       50                 xaml = reg.Replace(xaml, string.Empty);

       51 

       52                 // remove TextIndent attribute

       53                 reg = new Regex("TextIndent=\"[^\"]*\"");

       54                 xaml = reg.Replace(xaml, string.Empty);

       55

       56                 // change the non english font family

       57                 reg = new Regex("FontFamily=\"[^a-zA-Z]*\"");

       58                 xaml = reg.Replace(xaml, "FontFamily=\"Arial\"");

       59 

       60             }

       61             return xaml;

       62         }



  • 相关阅读:
    Java实现HttpClient发送GET、POST请求(https、http)
    解决.net core 3.1 json日期带T的问题
    Java验证身份证号码的格式
    c++20新特性concept
    位图
    Linux内核 hlist_head/hlist_node结构解析
    linux将c++程序制作成.deb
    应用程序或动态库中与加载的其他动态库的类或者函数重名问题
    vue props 属性值接受多个类型
    异步循环
  • 原文地址:https://www.cnblogs.com/fresky/p/1846896.html
Copyright © 2020-2023  润新知