在DevExpress控件组里面有一个控件,他的名字是RichEditControl,这个控件功能很强大
可以利用它来做邮件编辑器,实现图文并茂的邮件的功能,如下所示。
可以利用它做HTML富文本编辑器。
可以利用它加载RTF、WORD、HTML等文件进行展示,编辑保存以及打印(可做模板文件替换打印)。
常用属性:
设置器ActiveViewType=PrintLayout,显示如下
image
设置器ActiveViewType=PrintLayout,显示如下
image
创建工具栏
image
单击Create BarManager然后可以进一步看到更多的工具栏菜单了,如下所示。你可以悬着Create All Bar来创建所有工具栏,然后删除多余的就可以了。
image
实现自定义的按钮功能
打开文件(不支持URL格式)
private void barLoadFile_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
string filter ="Word2003(*.doc)|*.doc|Word2007(*.docx)|*.docx|RTF(*.rtf)|*.rtf|HTM(*.htm)|*.htm|HTML(*.html)|*.html|All File(*.*)|*.*";
stringfile = FileDialogHelper.Open("打开文件", filter);
if(!string.IsNullOrEmpty(file))
{
richEditControl1.Document.LoadDocument(file);
//文件显示不正确 使用下面写法
/** string path = Path.GetFullPath(file);
string extension = Path.GetExtension(file);
switch (extension.ToLower())
{
case".htm":
case".html":richEditControl1.Document.LoadDocument(file, DocumentFormat.Html, path);break;
case".doc": richEditControl1.Document.LoadDocument(file, DocumentFormat.Doc, path);break;
case".docx":richEditControl1.Document.LoadDocument(file, DocumentFormat.OpenXml, path); break;
default:richEditControl1.Document.LoadDocument(file, DocumentFormat.PlainText, path);break;
}
**/
}
}
显示URL路径文件(不同文件指定不同文件类型,查看DocumentFormat)
string docpath = AppContext.Configuration.Application.ServiceUrl + result.result.ToString();
var bytes = (new WebClient()).DownloadData(docpath);
var stream = new MemoryStream(bytes);
if (docpath.Contains(".rtf"))
richEditControl1.LoadDocument(stream, DevExpress.XtraRichEdit.DocumentFormat.Rtf);
else if(docpath.Contains(".docx"))
richEditControl1.LoadDocument(stream, DevExpress.XtraRichEdit.DocumentFormat.OpenXml);
else
richEditControl1.LoadDocument(stream, DevExpress.XtraRichEdit.DocumentFormat.Doc);
属性 | 含义 |
---|---|
DocumentFormat.OpenXml | MS Office 2007 or docx |
DocumentFormat.Rtf | Rtf |
DocumentFormat.Doc | MS Office97-2003 |
DocumentFormat.ePub | EPUB |
DocumentFormat.Html | HTML |
DocumentFormat.Mht | MHT |
DocumentFormat.OpenDocument | .odt |
DocumentFormat.PlainText | Plain |
DocumentFormat.WordML | .xml or MS Office2003 |
字体修正
void richEditControl1_DocumentLoaded(object sender, EventArgs e)
{
DocumentRange range = richEditControl1.Document.Range;
CharacterProperties cp =richEditControl1.Document.BeginUpdateCharacters(range);
cp.FontName ="新宋体";
richEditControl1.Document.EndUpdateCharacters(cp);
}
替换文档中内容
document.BeginUpdate();
try
{
DocumentRange[] namPis = document.FindAll("$_姓名", SearchOptions.None);
foreach (var item in namPis)
{
document.Replace(item, _record.namePi);
}
}
finally
{
document.EndUpdate();
}