RichTextBox for WPF
ロードと保存
RichTextBox for WPF の使い方 > ロードと保存

単純な TextBox コントロールのコンテンツは、Text プロパティを使用して保存できます。Text プロパティを使用して C1RichTextBox コントロールのコンテンツを保存することもできますが、豊富な書式設定は失われます。代わりに、Html プロパティを使用すると、書式設定を維持したまま C1RichTextBox のコンテンツを保存できます。

Html プロパティは、C1RichTextBox の書式設定されたコンテンツを HTML 文字列として取得または設定します。C1RichTextBox に組み込まれた HTML フィルタはかなり機能が豊富で、CSS スタイル、イメージ、ハイパーリンク、リストなどをサポートします。ただし、このフィルタはすべての HTML をサポートするわけではなく、C1RichTextBox コントロール自体がサポートする機能に限定されます。たとえば、C1RichTextBox の現在のバージョンは、テーブルをサポートしていません。それでも、Html プロパティを使用してシンプルな HTML ドキュメントを表示できます。

C1RichTextBox に「Hello World」と入力すると、Html プロパティは次のマークアップを返します。

HTML
コードのコピー
<html>
<head>
  <style type="text/css">
    .c0 { font-family:Portable User Interface;font-size:9pt; }
    .c1 { margin-bottom:7.5pt; }
  </style>
</head>
<body class="c0">
<p class="c1">Hello world.</p>
</body>
</html>

Html プロパティは、HTML と内部 C1Document クラスの間のフィルタになることに注意してください。C1RichTextBox によってサポートされていないコメント、メタ情報などの情報は HTML ストリームから破棄され、後でこの HTML ドキュメントを保存しても保持されません。

Load

Once you set the content to the C1RichTextBox through Text or Html property, you can render the content in C1RichTextBox in html, RTF and Text formats. You can refer the below code for loading the content in the C1RichTextBox in various formats.
                                        
//C1RichTextBoxのテキストを設定します
   richTextBox.Text = "C1RichTextBoxsupports importing and exporting to HTMLand RTF.";

//C1RichTextBoxテキストをHTML形式に変換します
   richTextBox.Text = new HtmlFilter().ConvertFromDocument(richTextBox.Document);
                                                               
                                 
  //C1RichTextBoxのHTMLコンテンツを設定します
  richTextBox.Html = @"<html>
  <head>
  <style>
  td
  {
        border: solid 1px Red;
    padding: 2px;
  }
  </style>
  </head>
  <body style=""font-family: Verdana; font-size: medium;"">
  <p style='text-align:center; background-color:#FFFFCC; font-size:12pt'>
  <b>C1RichTextBox</b> supports importing and exporting to <span style='text-decoration: underline'>HTML</span>
  and <span style='text-decoration: underline'>RTF</span>.</p>
  </body>
  </html>";
                                                
  //C1RichTextBoxテキストをRTF形式に変換します
   richTextBox.Text = new RtfFilter().ConvertFromDocument(richTextBox.Document);
                 
//C1RichTextBoxのテキストを設定します  
richTextBox.Text = "C1RichTextBoxsupports importing and exporting to HTMLand RTF.";   

                                

Export To Word

C1RichTextBox enables you to render the richtextbox document to Microsoft Word using the C1Word library. The rendered document contains all the set of properties that define the formatting of the content stored in the textbox.
To implement this feature, you need to add reference of C1.WPF.Word API to the application. To save the C1RichTextbox document as a word document, first convert it into a byte array by using the MemoryStream class. Then you can use the LoadFromRtf and Save methods of the C1WordDocument class for loading and saving the content as a word document.  The below code snippet shows how to save the C1RichTextBox content as a word document.

 

C#
コードのコピー
                      
//C1RichTextBoxのHTMLコンテンツを設定します
richTextBox.Html = @"<html>
<head>
<style>
td
{
 border: solid 1px Red;
 padding: 2px;
}
</style>
</head>
<body style=""font-family: Verdana; font-size: medium;"">
<p style='text-align:center; background-color:#FFFFCC; font-size:12pt'>
<b>C1RichTextBox</b> supports importing and exporting to <span style='text-decoration: underline'>HTML</span>
   and <span style='text-decoration: underline'>RTF</span>.</p>
</body>
</html>";
//RTFフィルタを使用してHTMLコンテンツをRTF形式に変換します
   var rtfText = new C1.WPF.RichTextBox.Documents.RtfFilter().ConvertFromDocument(richTextBox.Document);

//C1WordDocumentのオブジェクトを作成します
   C1WordDocument doc = new C1.WPF.Word.C1WordDocument();

//MemoryStreamクラスを使用してRTFテキストをバイト単位で取得します
   var stream = new MemoryStream(Encoding.UTF8.GetBytes(rtfText));

//C1WordDocumentにRTFテキストをロードします
   doc.LoadFromRtf(stream);

//コンテンツをWord文書形式で保存します
   doc.Save("Document.docx");