Devexpress RichEditControl控件——笔记

环境: win7, winform ,vs2012 ,deve 12.1.5

项目中需要用富文本控件来编辑文档(包括floating格式的图片),然后需要在网页中显示,并且在word中打印。

下面是我的解决方案。 (请注意devexpress版本为12.1.5,我在搜资料的时候看到11的版本好像也可以,但是我自己没有试过,这个请自行考虑)

1:关于floating格式的图片,网上中文资料不多,英文的很多,可以自己去搜。 下面是我搜到的代码. 

//自定义用户插入floating图片的辅助类 

public class CustomInsertFloatingObjectPictureCoreCommand : InsertFloatingObjectPictureCoreCommand
    {
        Image _IMG = null;
        public CustomInsertFloatingObjectPictureCoreCommand(IRichEditControl control,Image img)
            : base(control)
        {
            this._IMG = img;
        }
        protected override DevExpress.XtraRichEdit.Model.FloatingObjectContent CreateDefaultFloatingObjectContent(DevExpress.XtraRichEdit.Model.FloatingObjectAnchorRun run)
        {
            return new PictureFloatingObjectContent(run, RichEditImage.CreateImage(_IMG));
        }
        protected override void ModifyModel()
        {
            this.ImportSource = new DevExpress.Office.Internal.ImportSource<RichEditImageFormat, RichEditImage>(""new BitmapPictureImporter());
            base.ModifyModel();
        }
    }

//代码中直接使用如下代码 

CustomInsertFloatingObjectPictureCoreCommand cmd = new CustomInsertFloatingObjectPictureCoreCommand(richEditControl1, img);
cmd.Execute();
richEditControl1.Document.Selection = richEditControl1.Document.CreateRange(0, 1);
SetFloatingObjectBehindTextWrapTypeCommand cmd2 = new SetFloatingObjectBehindTextWrapTypeCommand(richEditControl1);

cmd2.Execute(); 

ps:命名空间请自行添加引用  

2:上面只是解决在控件中插入floating格式的代码, 在richeditcontrol中也能正常使用和显示,但是在网页中flaoting格式的图片并不能正常显示,发现richeditcontrol控件的属性HtmlText中,根本就没有float这类的样式代码,原来richeditcontrol默认是以rtf格式在转换和显示内容,我尝试着用各种方法将richeditcontrol内容转换为html,但是都不如愿,后来实在不行了,只好用了一个不是办法的办法,在richeditcontrol中使用DrawToBitmap方法,将控件内容打印成图片,然后在网页中将图片拼接显示,图片再由程序上传到web服务器,以达到和控件一样的排版显示。经测试,此方法可行。代码如下:(转成图片后按html格式拼接成字符串:“<img src='imageurl' width='' height=''/><img src='imageurl' width='' height=''/>......”,lz试过用<img src="data:image/png;base64,...."/>这种格式来保存,免去提交图片到服务器的步骤,但是发现ie低版本不支持,所以放弃

int height = 0, index = 0, scroolWidth = 19;

 DateTime t = DateTime.Now;

                string timeStr = string.Format("{0}{1}{2}{3}{4}{5}"
                    , t.Year, t.Month, t.Day, t.Hour, t.Minute, t.Millisecond);
                string ServerPath = string.Format("http://127.0.0.1/fileshare/images/{0}", timeStr);

while (true) //死循环

                {
                    t = DateTime.Now;
                    height = rich.Height * index++;
                    rich.VerticalScrollPosition = height;
                    //如果控件内容有分页的话,需要打印几张图片拼接来显示完整内容

    if (height <= rich.VerticalScrollPosition || height == 0)
                    {
                        Bitmap m_Bitmap = new Bitmap(rich.Width - scroolWidth, rich.Height);
                        rich.DrawToBitmap(m_Bitmap, new Rectangle(new Point(00), m_Bitmap.Size));
                        m_Bitmap = (Bitmap)m_Bitmap.GetThumbnailImage(m_Bitmap.Width, m_Bitmap.Height, null, IntPtr.Zero);
                        //需要截取四周1个像素的边框,lz的电脑上有个问题就是截取出来的图片周边会有边框,在拼接的时候会很明显,但是网上资料都没有提到这个问题,不知道是不是lzrp问题
                        Bitmap biN = m_Bitmap.Clone(new Rectangle(11, m_Bitmap.Width - 1, m_Bitmap.Height - 2), System.Drawing.Imaging.PixelFormat.Format16bppRgb555);

string file = ServerPath + t.Millisecond + ".png";//服务器图片路径
                        string localpath = System.Environment.CurrentDirectory + "/users/RicheditControl/" + timeStr + t.Millisecond + ".png";//本地图片路径
                        biN.Save(localpath, ImageFormat.Png);
                        upload.UpLoadFile(localpath, file);//lz自己的上传图片方法
                        html.AppendFormat("<img src=\"{0}\" width=\"{1}\" height=\"{2}\"/>"
                            , file, m_Bitmap.Width - 1, m_Bitmap.Height - 2);//保存格式
                        m_Bitmap.Dispose();
                        biN.Dispose();
                    }
                    else
                    {
                        break;
                    }
                }


 3:在word文档中插入richeditcontrol内容,也是使用曲线救国的方法,使用richeditcontrol控件的rtftext属性,将其内容用代码复制到剪切板上,然后使用word文档的paste方法黏贴到文档中来间接显示

     winfrom中直接使用以下代码       
//复制到黏贴板            

 System.Windows.Forms.Clipboard.SetDataObject(value, false);

            //黏贴到文档

this._wordApplication.Application.Selection.Paste();

  asp.net中使用以下代码(因为Clipboard是winform中的方法,直接使用上面代码会报错,具体错误自己试过便知) 

        public void InsertRTF(string value)
        {
            Thread cbThread = new Thread(new ParameterizedThreadStart(CopyToClipboard));
            cbThread.TrySetApartmentState(ApartmentState.STA);
            cbThread.Start(value);
            cbThread.Join(); 
            //黏贴到文档
            this._wordApplication.Application.Selection.Paste();
        }

 [STAThread]

        protected void CopyToClipboard(object value)
        {
            System.Windows.Forms.Clipboard.SetData(System.Windows.Forms.DataFormats.Rtf, value); //成功复制到剪贴板中,注意,是服务器的剪贴板。对剪贴板的其它操作类似  
        }  

 以上就是我所在项目的需求解决方法,可能其他人的项目需求不一样,但是功能的实现是差不多的,我在使用richeditcontrol这个控件的时候也遇到了很多麻烦,把自己的解决方法写在这里,也希望其他朋友能有所帮助.

原文地址:https://www.cnblogs.com/liumang/p/2621927.html