原創(chuàng)|其它|編輯:郝浩|2012-10-15 10:15:41.000|閱讀 6566 次
概述:Devexpress RichEditControl控件——學習筆記
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
項目中需要用富文本控件來編輯文檔(包括floating格式的圖片),然后需要在網頁中顯示,并且在word中打印。想和大家一起分享一下Devexpress RichEditControl控件的學習筆記。
下面是我的解決方案。 (請注意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格式的圖片并不能正常顯示,發(fā)現(xiàn)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,...."/>這種格式來保存,免去提交圖片到服務器的步驟,但是發(fā)現(xiàn)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) //死循環(huán) { 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(0, 0), 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(1, 1, 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方法黏貼到文檔中來間接顯示。
//復制到黏貼板 System.Windows.Forms.Clipboard.SetDataObject(value, false); //黏貼到文檔 this._wordApplication.Application.Selection.Paste();
asp.net中使用以下代碼(因為Clipboard是winform中的方法,直接使用上面代碼會報錯,具體錯誤自己試過便知)
winfrom中直接使用以下代碼
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); //成功復制到剪貼板中,注意,是服務器的剪貼板。對剪貼板的其它操作類似 }
以上就是我所在項目的需求解決方法,可能其他人的項目需求不一樣,但是功能的實現(xiàn)是差不多的,我在使用richeditcontrol這個控件的時候也遇到了很多麻煩,把自己的解決方法寫在這里,也希望其他朋友能有所幫助。
本站文章除注明轉載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:博客園