環境: 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();
}
}
//代碼中直接使用如下代碼
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低版本不支持,所以放棄)
DateTime t = DateTime.Now;
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(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中的方法,直接使用上面代碼會報錯,具體錯誤自己試過便知)
[STAThread]
以上就是我所在項目的需求解決方法,可能其他人的項目需求不一樣,但是功能的實現是差不多的,我在使用richeditcontrol這個控件的時候也遇到了很多麻煩,把自己的解決方法寫在這里,也希望其他朋友能有所幫助.