/// <summary> /// 设置添加到图片上的文字 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void menuSetStrAddToPic_Click(object sender, EventArgs e) { DocForm doc = new DocForm(); if (doc.ShowDialog() == DialogResult.OK) { this.strAddToPic = doc.TxtSource.Text; this.font = doc.font; this.color = doc.color; } }
private void NewFile_Click(object sender, EventArgs e) { if (doc != null) { doc.Close(); } //窗体记录数++ wCount++; //创建文档窗体对象 doc = new DocForm(); //设置主窗口为文档窗体的父窗口 doc.MdiParent = this; //设置文档窗体的标题 doc.Text = "文档" + wCount; //显示文档窗体 doc.Show(); }
//保存richTextBox数据到已经存在的word文档中。 //先打开word文档,全选word文档中的数据,然后全选richTextBox中的数据并复制,粘贴到word文档中,最后保存word文档,并关闭文档 public void SaveWord(string fileName) { Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word.Document doc = null; object missing = System.Reflection.Missing.Value; object File = fileName; object readOnly = false; object isVisible = true; try { doc = app.Documents.Open(ref File, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing); doc.ActiveWindow.Selection.WholeStory(); //全选 this.doc.TxtSource.SelectAll(); Clipboard.SetData(DataFormats.Rtf, this.doc.TxtSource.SelectedRtf); //复制RTF数据到剪贴板 doc.ActiveWindow.Selection.Paste(); //doc.Paragraphs.Last.Range.Text = richTextBox1.Text;//word文档赋值数据,不带格式 doc.Save(); MessageBox.Show("文件保存成功"); } finally { if (doc != null) { doc.Close(ref missing, ref missing, ref missing); doc = null; } if (app != null) { app.Quit(ref missing, ref missing, ref missing); app = null; } } }
private void OpenFile_Click(object sender, EventArgs e) { if (doc != null) { doc.Close(); } if (openFileDialog.ShowDialog() == DialogResult.OK) { try { RichTextBoxStreamType fileType; //判断文档类型 switch (openFileDialog.FilterIndex) { case 1: fileType = RichTextBoxStreamType.PlainText; break; case 2: fileType = RichTextBoxStreamType.RichText; break; default: fileType = RichTextBoxStreamType.UnicodePlainText; break; } wCount++; doc = new DocForm(); doc.MdiParent = this; doc.Text = openFileDialog.FileName; doc.TxtSource.LoadFile(openFileDialog.FileName, fileType); doc.Show(); } catch (Exception ex) { MessageBox.Show(ex.Message + ex.StackTrace, "错误"); } } }