using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; using DevExpress.XtraEditors; using System.IO; using DevExpress.XtraRichEdit; using DevExpress.XtraRichEdit.API.Native; using ProjectBase.Data.Logs; namespace ProjectBase.Controls.DocViewer { /// /// WORD控件的测试例子 /// public partial class FrmWordView : DevExpress.XtraEditors.XtraForm { /// /// 加载流数据 /// public Stream Stream { get; set; } /// /// 文件后缀名,如.doc /// public string Extension { get; set; } /// /// 文档文件路径。如果指定了该属性,可以不用设置Stream和Extension属性。 /// public string FilePath { get; set; } /// /// 是否显示打开文件按钮 /// public bool ShowBtnOpenFile { get { return m_ShowBtnOpenFile; } set { m_ShowBtnOpenFile = value; this.btnOpenFile.Visible = this.ShowBtnOpenFile; } } //是否显示保存文件按钮 public bool ShowBtnSaveFile { get { return m_ShowBtnSaveFile; } set { m_ShowBtnSaveFile = value; this.btnSaveFile.Visible = this.ShowBtnSaveFile; } } //记录窗体的名称 readonly string mainFormText; private bool m_ShowBtnOpenFile = false;//是否显示打开文件按钮 private bool m_ShowBtnSaveFile = false;//是否显示保存文件按钮 public FrmWordView() { InitializeComponent(); //记录窗体的名称,并实现文档变化事件的处理,方便显示新的文件名称 mainFormText = this.Text; this.richEditControl1.DocumentLoaded += new EventHandler(richEditControl1_DocumentLoaded); } /// /// WORD文档变化后,实现对新文件名称的显示 /// void richEditControl1_DocumentLoaded(object sender, EventArgs e) { string fileName = Path.GetFileName(this.richEditControl1.Options.DocumentSaveOptions.CurrentFileName); if (String.IsNullOrEmpty(fileName)) { Text = mainFormText; } else { Text = fileName + " - " + mainFormText; } //修改默认字体 DocumentRange range = richEditControl1.Document.Range; CharacterProperties cp = this.richEditControl1.Document.BeginUpdateCharacters(range); cp.FontName = "新宋体"; //cp.FontSize = 12; this.richEditControl1.Document.EndUpdateCharacters(cp); } private void FrmWordView_Load(object sender, EventArgs e) { //如果文件流不为空,首先根据Stream对象加载文档,否则根据文件路径进行加载 if (!this.DesignMode) { if (this.Stream != null) { #region MyRegion try { if (!string.IsNullOrEmpty(Extension)) { if (Extension.Equals(".doc", StringComparison.OrdinalIgnoreCase)) { this.richEditControl1.LoadDocument(this.Stream, DocumentFormat.Doc); } else if (Extension.Equals(".docx", StringComparison.OrdinalIgnoreCase)) { this.richEditControl1.LoadDocument(this.Stream, DocumentFormat.OpenXml); } else if (Extension.Equals(".rtf", StringComparison.OrdinalIgnoreCase)) { this.richEditControl1.LoadDocument(this.Stream, DocumentFormat.Rtf); } else { this.richEditControl1.LoadDocument(this.Stream, DocumentFormat.Doc); } } else { this.richEditControl1.LoadDocument(this.Stream, DocumentFormat.Doc); } } catch (Exception ex) { DevExpress.XtraEditors.XtraMessageBox.Show("加载Word文件失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); LogHelper.log.Error(string.Format("加载Word文件失败,错误为{0}", ex.ToString())); } #endregion } else if (!string.IsNullOrEmpty(FilePath)) { this.richEditControl1.LoadDocument(this.FilePath); } } } /// /// 打开WORD文件 /// private void btnOpenFile_Click(object sender, EventArgs e) { string filePath; OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "Word(*.doc)|*.doc|All File(*.*)|*.*"; dialog.Title = "Word选择"; dialog.RestoreDirectory = true; dialog.FileName = null; if (dialog.ShowDialog() == DialogResult.OK) { filePath = dialog.FileName; } else { filePath = string.Empty; } if (!string.IsNullOrEmpty(filePath)) { richEditControl1.LoadDocument(filePath);//, DocumentFormat.Doc); } } /// /// 保存WORD文件 /// private void btnSaveFile_Click(object sender, EventArgs e) { this.richEditControl1.SaveDocument(); this.Close(); } /// /// 另存为WORD文件 /// private void btnSaveAs_Click(object sender, EventArgs e) { try { richEditControl1.SaveDocumentAs(); DevExpress.XtraEditors.XtraMessageBox.Show("保存Word文件成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); } catch (Exception ex) { DevExpress.XtraEditors.XtraMessageBox.Show("保存Word文件失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); LogHelper.log.Error(string.Format("保存Word文件失败,错误为{0}", ex.ToString())); } } /// /// WORD文件打印 /// private void btnPreview_Click(object sender, EventArgs e) { this.richEditControl1.ShowPrintPreview(); } private void btnClose_Click(object sender, EventArgs e) { this.richEditControl1.SaveDocument(); this.Close(); } } }