FrmWordView.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using DevExpress.XtraEditors;
  11. using System.IO;
  12. using DevExpress.XtraRichEdit;
  13. using DevExpress.XtraRichEdit.API.Native;
  14. using ProjectBase.Data.Logs;
  15. namespace ProjectBase.Controls.DocViewer
  16. {
  17. /// <summary>
  18. /// WORD控件的测试例子
  19. /// </summary>
  20. public partial class FrmWordView : DevExpress.XtraEditors.XtraForm
  21. {
  22. /// <summary>
  23. /// 加载流数据
  24. /// </summary>
  25. public Stream Stream { get; set; }
  26. /// <summary>
  27. /// 文件后缀名,如.doc
  28. /// </summary>
  29. public string Extension { get; set; }
  30. /// <summary>
  31. /// 文档文件路径。如果指定了该属性,可以不用设置Stream和Extension属性。
  32. /// </summary>
  33. public string FilePath { get; set; }
  34. /// <summary>
  35. /// 是否显示打开文件按钮
  36. /// </summary>
  37. public bool ShowBtnOpenFile
  38. {
  39. get { return m_ShowBtnOpenFile; }
  40. set
  41. {
  42. m_ShowBtnOpenFile = value;
  43. this.btnOpenFile.Visible = this.ShowBtnOpenFile;
  44. }
  45. }
  46. //是否显示保存文件按钮
  47. public bool ShowBtnSaveFile
  48. {
  49. get { return m_ShowBtnSaveFile; }
  50. set
  51. {
  52. m_ShowBtnSaveFile = value;
  53. this.btnSaveFile.Visible = this.ShowBtnSaveFile;
  54. }
  55. }
  56. //记录窗体的名称
  57. readonly string mainFormText;
  58. private bool m_ShowBtnOpenFile = false;//是否显示打开文件按钮
  59. private bool m_ShowBtnSaveFile = false;//是否显示保存文件按钮
  60. public FrmWordView()
  61. {
  62. InitializeComponent();
  63. //记录窗体的名称,并实现文档变化事件的处理,方便显示新的文件名称
  64. mainFormText = this.Text;
  65. this.richEditControl1.DocumentLoaded += new EventHandler(richEditControl1_DocumentLoaded);
  66. }
  67. /// <summary>
  68. /// WORD文档变化后,实现对新文件名称的显示
  69. /// </summary>
  70. void richEditControl1_DocumentLoaded(object sender, EventArgs e)
  71. {
  72. string fileName = Path.GetFileName(this.richEditControl1.Options.DocumentSaveOptions.CurrentFileName);
  73. if (String.IsNullOrEmpty(fileName))
  74. {
  75. Text = mainFormText;
  76. }
  77. else
  78. {
  79. Text = fileName + " - " + mainFormText;
  80. }
  81. //修改默认字体
  82. DocumentRange range = richEditControl1.Document.Range;
  83. CharacterProperties cp = this.richEditControl1.Document.BeginUpdateCharacters(range);
  84. cp.FontName = "新宋体";
  85. //cp.FontSize = 12;
  86. this.richEditControl1.Document.EndUpdateCharacters(cp);
  87. }
  88. private void FrmWordView_Load(object sender, EventArgs e)
  89. {
  90. //如果文件流不为空,首先根据Stream对象加载文档,否则根据文件路径进行加载
  91. if (!this.DesignMode)
  92. {
  93. if (this.Stream != null)
  94. {
  95. #region MyRegion
  96. try
  97. {
  98. if (!string.IsNullOrEmpty(Extension))
  99. {
  100. if (Extension.Equals(".doc", StringComparison.OrdinalIgnoreCase))
  101. {
  102. this.richEditControl1.LoadDocument(this.Stream, DocumentFormat.Doc);
  103. }
  104. else if (Extension.Equals(".docx", StringComparison.OrdinalIgnoreCase))
  105. {
  106. this.richEditControl1.LoadDocument(this.Stream, DocumentFormat.OpenXml);
  107. }
  108. else if (Extension.Equals(".rtf", StringComparison.OrdinalIgnoreCase))
  109. {
  110. this.richEditControl1.LoadDocument(this.Stream, DocumentFormat.Rtf);
  111. }
  112. else
  113. {
  114. this.richEditControl1.LoadDocument(this.Stream, DocumentFormat.Doc);
  115. }
  116. }
  117. else
  118. {
  119. this.richEditControl1.LoadDocument(this.Stream, DocumentFormat.Doc);
  120. }
  121. }
  122. catch (Exception ex)
  123. {
  124. DevExpress.XtraEditors.XtraMessageBox.Show("加载Word文件失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  125. LogHelper.log.Error(string.Format("加载Word文件失败,错误为{0}", ex.ToString()));
  126. }
  127. #endregion
  128. }
  129. else if (!string.IsNullOrEmpty(FilePath))
  130. {
  131. this.richEditControl1.LoadDocument(this.FilePath);
  132. }
  133. }
  134. }
  135. /// <summary>
  136. /// 打开WORD文件
  137. /// </summary>
  138. private void btnOpenFile_Click(object sender, EventArgs e)
  139. {
  140. string filePath;
  141. OpenFileDialog dialog = new OpenFileDialog();
  142. dialog.Filter = "Word(*.doc)|*.doc|All File(*.*)|*.*";
  143. dialog.Title = "Word选择";
  144. dialog.RestoreDirectory = true;
  145. dialog.FileName = null;
  146. if (dialog.ShowDialog() == DialogResult.OK)
  147. {
  148. filePath = dialog.FileName;
  149. }
  150. else
  151. {
  152. filePath = string.Empty;
  153. }
  154. if (!string.IsNullOrEmpty(filePath))
  155. {
  156. richEditControl1.LoadDocument(filePath);//, DocumentFormat.Doc);
  157. }
  158. }
  159. /// <summary>
  160. /// 保存WORD文件
  161. /// </summary>
  162. private void btnSaveFile_Click(object sender, EventArgs e)
  163. {
  164. this.richEditControl1.SaveDocument();
  165. this.Close();
  166. }
  167. /// <summary>
  168. /// 另存为WORD文件
  169. /// </summary>
  170. private void btnSaveAs_Click(object sender, EventArgs e)
  171. {
  172. try
  173. {
  174. richEditControl1.SaveDocumentAs();
  175. DevExpress.XtraEditors.XtraMessageBox.Show("保存Word文件成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  176. this.Close();
  177. }
  178. catch (Exception ex)
  179. {
  180. DevExpress.XtraEditors.XtraMessageBox.Show("保存Word文件失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  181. LogHelper.log.Error(string.Format("保存Word文件失败,错误为{0}", ex.ToString()));
  182. }
  183. }
  184. /// <summary>
  185. /// WORD文件打印
  186. /// </summary>
  187. private void btnPreview_Click(object sender, EventArgs e)
  188. {
  189. this.richEditControl1.ShowPrintPreview();
  190. }
  191. private void btnClose_Click(object sender, EventArgs e)
  192. {
  193. this.richEditControl1.SaveDocument();
  194. this.Close();
  195. }
  196. }
  197. }