/// <summary> /// 修改表格样式 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void TableFormat_Click(object sender, RibbonControlEventArgs e) { //弹出窗体提示 DialogResult IsWait = MessageBox.Show("如果表格较多运行较为缓慢,进度可在Word左下角查看" + Environment.NewLine + "是否继续?", "请选择", MessageBoxButtons.YesNo); if (IsWait != DialogResult.Yes) { return; } WordApp = Globals.ThisAddIn.Application; WordDoc = WordApp.ActiveDocument; WordApp.ScreenUpdating = false; //关闭屏幕刷新 #region //读取配置文件 //读取配置文件 ClsThisAddinConfig clsConfig = new ClsThisAddinConfig(Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments)); //表格间距 bool SpaceGroupCheck = clsConfig.ReadConfig <bool>("SettingFormatForm", "SpaceGroupCheck", true); //首行缩进 bool TableIndentCheck = clsConfig.ReadConfig <bool>("SettingFormatForm", "TableIndentCheck", true); //段前 decimal TableBeforeMainBody = clsConfig.ReadConfig <decimal>("SettingFormatForm", "TableBeforeMainBody", 0m); //段后 decimal TableAfterMainBody = clsConfig.ReadConfig <decimal>("SettingFormatForm", "TableAfterMainBody", 0m); //行间距 decimal TableRowSpace = FormatFunC.RowSpace(clsConfig.ReadConfig <string>("SettingFormatForm", "TableRowSpace", "单倍行距")); //表格边框 bool BorderGroupCheck = clsConfig.ReadConfig <bool>("SettingFormatForm", "BorderGroupCheck", true); //上下边框宽度1磅 bool TableTBBorderCheck = clsConfig.ReadConfig <bool>("SettingFormatForm", "TableTBBorderCheck", true); //左右无边框 bool TableLRBorderCheck = clsConfig.ReadConfig <bool>("SettingFormatForm", "TableLRBorderCheck", true); //表格其他选项 bool OtherGroupCheck = clsConfig.ReadConfig <bool>("SettingFormatForm", "OtherGroupCheck", true); //垂直居中 bool TableVerticalCenterCheck = clsConfig.ReadConfig <bool>("SettingFormatForm", "TableVerticalCenterCheck", true); //标题及合计行加粗 bool TableTitleWiderCheck = clsConfig.ReadConfig <bool>("SettingFormatForm", "TableTitleWiderCheck", true); //表格字号 decimal TableFontSize = FormatFunC.FontSize(clsConfig.ReadConfig <string>("SettingFormatForm", "TableFontSize", "小四")); #endregion if (SpaceGroupCheck || BorderGroupCheck || OtherGroupCheck) { //在状态栏显示进度 WordApp.StatusBar = "当前进度:0%"; int i3 = WordDoc.Tables.Count; int i4 = 1; foreach (Word.Table Tb in WordDoc.Tables) { Tb.Select(); //间距 if (SpaceGroupCheck) { if (TableIndentCheck) { WordApp.Selection.ParagraphFormat.CharacterUnitLeftIndent = 0; WordApp.Selection.ParagraphFormat.CharacterUnitRightIndent = 0; WordApp.Selection.ParagraphFormat.CharacterUnitFirstLineIndent = 0; WordApp.Selection.ParagraphFormat.FirstLineIndent = WordApp.CentimetersToPoints(0); } WordApp.Selection.ParagraphFormat.SpaceBefore = 0; WordApp.Selection.ParagraphFormat.LineUnitBefore = float.Parse(TableBeforeMainBody.ToString()); WordApp.Selection.ParagraphFormat.SpaceAfter = 0; WordApp.Selection.ParagraphFormat.LineUnitAfter = float.Parse(TableAfterMainBody.ToString()); WordApp.Selection.ParagraphFormat.LineSpacing = WordApp.LinesToPoints(float.Parse(TableRowSpace.ToString())); WordApp.Selection.ParagraphFormat.DisableLineHeightGrid = -1; //行距设置中,不勾选“如果定义了文档网格,则于网格对齐” WordApp.Selection.ParagraphFormat.AutoAdjustRightIndent = -1; //行距设置中,不勾选“如果定义了文档网格,则自动调整右缩进” } //边框 if (BorderGroupCheck) { //上下边框1磅 if (TableTBBorderCheck) { Tb.Borders[Word.WdBorderType.wdBorderTop].LineStyle = Word.WdLineStyle.wdLineStyleSingle; Tb.Borders[Word.WdBorderType.wdBorderTop].LineWidth = Word.WdLineWidth.wdLineWidth100pt; Tb.Borders[Word.WdBorderType.wdBorderTop].Color = Word.WdColor.wdColorAutomatic; Tb.Borders[Word.WdBorderType.wdBorderBottom].LineStyle = Word.WdLineStyle.wdLineStyleSingle; Tb.Borders[Word.WdBorderType.wdBorderBottom].LineWidth = Word.WdLineWidth.wdLineWidth100pt; Tb.Borders[Word.WdBorderType.wdBorderBottom].Color = Word.WdColor.wdColorAutomatic; } //左右无边框 if (TableLRBorderCheck) { Tb.Borders[Word.WdBorderType.wdBorderLeft].LineStyle = Word.WdLineStyle.wdLineStyleNone; Tb.Borders[Word.WdBorderType.wdBorderRight].LineStyle = Word.WdLineStyle.wdLineStyleNone; } } //其他选项 if (OtherGroupCheck) { //垂直居中 if (TableVerticalCenterCheck) { WordApp.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter; } // if (TableTitleWiderCheck) { } //表格字体 Tb.Range.Font.Size = float.Parse(TableFontSize.ToString()); } //显示进度 WordApp.StatusBar = "当前进度:" + Math.Round((i4 * 100d / i3), 2) + "%"; i4++; } } WordApp.ScreenUpdating = true;//关闭屏幕刷新 }
/// <summary> /// 修改文本样式 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void TextFormat_Click(object sender, RibbonControlEventArgs e) { //弹出窗体提示 DialogResult IsWait = MessageBox.Show("如果文本行较多运行较为缓慢,进度可在Word左下角查看" + Environment.NewLine + "是否继续?", "请选择", MessageBoxButtons.YesNo); if (IsWait != DialogResult.Yes) { return; } WordApp = Globals.ThisAddIn.Application; WordDoc = WordApp.ActiveDocument; WordApp.ScreenUpdating = false;//关闭屏幕刷新 //读取配置文件 ClsThisAddinConfig clsConfig = new ClsThisAddinConfig(Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments)); //正文字体字号 bool FontGroupCheck = clsConfig.ReadConfig <bool>("SettingFormatForm", "FontGroupCheck", true); string CnFont = clsConfig.ReadConfig <string>("SettingFormatForm", "CnFont", "仿宋_GB2312"); string NumFont = clsConfig.ReadConfig <string>("SettingFormatForm", "NumFont", "Arial Narrow"); decimal FontSize = FormatFunC.FontSize(clsConfig.ReadConfig <string>("SettingFormatForm", "FontSize", "小四")); //标题行间距 bool TitleGroupCheck = clsConfig.ReadConfig <bool>("SettingFormatForm", "TitleGroupCheck", true); bool TitleIndentCkeck = clsConfig.ReadConfig <bool>("SettingFormatForm", "TitleIndentCkeck", true); bool FirstTitleCheck = clsConfig.ReadConfig <bool>("SettingFormatForm", "FirstTitleCheck", true); decimal BeforeMainBody = clsConfig.ReadConfig <decimal>("SettingFormatForm", "BeforeMainBody", 0m); decimal AfterMainBody = clsConfig.ReadConfig <decimal>("SettingFormatForm", "AfterMainBody", 0.9m); decimal RowSpace = FormatFunC.RowSpace(clsConfig.ReadConfig <string>("SettingFormatForm", "RowSpace", "单倍行距")); //跳过的段落数 int SkipPgNum = FunC.TI(clsConfig.ReadConfig <decimal>("SettingFormatForm", "SkipPgs", 0m)); //段落大纲级别 Word.WdOutlineLevel PgLevel; if (FontGroupCheck || TitleGroupCheck) { //在状态栏显示进度 WordApp.StatusBar = "当前进度:0%"; int i3 = WordDoc.Paragraphs.Count; if (FontGroupCheck) { WordApp.Selection.WholeStory(); WordApp.Selection.Font.Name = CnFont; WordApp.Selection.Font.Name = NumFont; } int i4 = 0; foreach (Word.Paragraph Pg in WordDoc.Paragraphs) { i4++; if (i4 <= SkipPgNum) { continue; } if (Pg.Range.Information[Word.WdInformation.wdWithInTable]) { continue; } PgLevel = Pg.OutlineLevel; //如果勾选了标题段落,调整段前段后行间距 if (TitleGroupCheck) { //段前间距 Pg.SpaceBefore = 0; Pg.LineUnitBefore = float.Parse(BeforeMainBody.ToString()); //段后间距 Pg.SpaceAfter = 0; Pg.LineUnitAfter = float.Parse(AfterMainBody.ToString()); //段落行间距 Pg.LineSpacing = WordApp.LinesToPoints(float.Parse(RowSpace.ToString())); } if (PgLevel == Word.WdOutlineLevel.wdOutlineLevelBodyText) { if (FontGroupCheck) { Pg.Range.Font.Size = float.Parse(FontSize.ToString()); } } else { if (TitleGroupCheck) { if (TitleIndentCkeck) { Pg.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft; Pg.CharacterUnitLeftIndent = -2; } if (FirstTitleCheck) { if (PgLevel == Word.WdOutlineLevel.wdOutlineLevel1) { Pg.SpaceBefore = 0; Pg.LineUnitBefore = 0.5f; } } } } //调整表格下一行间距 if (TitleGroupCheck && FirstTitleCheck && i4 > 1) { if (WordDoc.Paragraphs[i4 - 1].Range.Information[Word.WdInformation.wdWithInTable]) { Pg.SpaceBefore = 0; Pg.LineUnitBefore = 0.5f; } } //显示进度 WordApp.StatusBar = "当前进度:" + Math.Round((i4 * 100d / i3), 2) + "%"; } } WordApp.ScreenUpdating = true;//关闭屏幕刷新 }
//启动时加载 private void SettingFormatForm_Load(object sender, EventArgs e) { //遍历系统字体并添加到字体选择框 foreach (FontFamily i in objFont.Families) { CnFont.Items.Add(i.Name.ToString()); NumFont.Items.Add(i.Name.ToString()); } //设置字号窗体选项 string[] FontSizeList = new[] { "初号", "小初", "一号", "小一", "二号", "小二", "三号", "小三", "四号", "小四", "五号", "小五", "六号", "小六", "七号", "八号", "5", "5.5", "6.5", "7.5", "8", "9", "10", "10.5", "11", "12", "14", "16", "18", "20", "22", "24", "26", "28", "36", "48", "72" }; FontSize.DataSource = FontSizeList; string[] TableFontSizeList = new[] { "初号", "小初", "一号", "小一", "二号", "小二", "三号", "小三", "四号", "小四", "五号", "小五", "六号", "小六", "七号", "八号", "5", "5.5", "6.5", "7.5", "8", "9", "10", "10.5", "11", "12", "14", "16", "18", "20", "22", "24", "26", "28", "36", "48", "72" }; TableFontSize.DataSource = TableFontSizeList; //设置行距选项 string[] RowSpaceList = new[] { "单倍行距", "1.15倍行距", "1.5倍行距", "2倍行距", "2.5倍行距", "3倍行距" }; RowSpace.DataSource = RowSpaceList; string[] TableRowSpaceList = new[] { "单倍行距", "1.15倍行距", "1.5倍行距", "2倍行距", "2.5倍行距", "3倍行距" }; TableRowSpace.DataSource = TableRowSpaceList; //从父节点SettingFormatForm中读取配置名为FontGroupCheck的值,字体字号复选框,默认为true FontGroupCheck.Checked = clsConfig.ReadConfig <bool>("SettingFormatForm", "FontGroupCheck", true); //中文字体 CnFont.SelectedItem = clsConfig.ReadConfig <string>("SettingFormatForm", "CnFont", "仿宋_GB2312"); //数字和英文字体 NumFont.SelectedItem = clsConfig.ReadConfig <string>("SettingFormatForm", "NumFont", "Arial Narrow"); //字号 FontSize.SelectedItem = clsConfig.ReadConfig <string>("SettingFormatForm", "FontSize", "小四"); //标题段落 TitleGroupCheck.Checked = clsConfig.ReadConfig <bool>("SettingFormatForm", "TitleGroupCheck", true); //标题行左缩进 TitleIndentCkeck.Checked = clsConfig.ReadConfig <bool>("SettingFormatForm", "TitleIndentCkeck", true); //一级标题、 表格下一行段前0.5倍行距 FirstTitleCheck.Checked = clsConfig.ReadConfig <bool>("SettingFormatForm", "FirstTitleCheck", true); //段前 BeforeMainBody.Value = clsConfig.ReadConfig <decimal>("SettingFormatForm", "BeforeMainBody", 0m); //段后 AfterMainBody.Value = clsConfig.ReadConfig <decimal>("SettingFormatForm", "AfterMainBody", 0.9m); //行间距 RowSpace.SelectedItem = clsConfig.ReadConfig <string>("SettingFormatForm", "RowSpace", "单倍行距"); //表格间距 SpaceGroupCheck.Checked = clsConfig.ReadConfig <bool>("SettingFormatForm", "SpaceGroupCheck", true); //首行缩进 TableIndentCheck.Checked = clsConfig.ReadConfig <bool>("SettingFormatForm", "TableIndentCheck", true); //段前 TableBeforeMainBody.Value = clsConfig.ReadConfig <decimal>("SettingFormatForm", "TableBeforeMainBody", 0m); //段后 TableAfterMainBody.Value = clsConfig.ReadConfig <decimal>("SettingFormatForm", "TableAfterMainBody", 0m); //行间距 TableRowSpace.SelectedItem = clsConfig.ReadConfig <string>("SettingFormatForm", "TableRowSpace", "单倍行距"); //跳过的段数 SkipPgs.Value = clsConfig.ReadConfig <decimal>("SettingFormatForm", "SkipPgs", 0m); //表格边框 BorderGroupCheck.Checked = clsConfig.ReadConfig <bool>("SettingFormatForm", "BorderGroupCheck", true); //上下边框宽度1磅 TableTBBorderCheck.Checked = clsConfig.ReadConfig <bool>("SettingFormatForm", "TableTBBorderCheck", true); //左右无边框 TableLRBorderCheck.Checked = clsConfig.ReadConfig <bool>("SettingFormatForm", "TableLRBorderCheck", true); //表格其他选项 OtherGroupCheck.Checked = clsConfig.ReadConfig <bool>("SettingFormatForm", "OtherGroupCheck", true); //垂直居中 TableVerticalCenterCheck.Checked = clsConfig.ReadConfig <bool>("SettingFormatForm", "TableVerticalCenterCheck", true); //标题及合计行加粗 TableTitleWiderCheck.Checked = clsConfig.ReadConfig <bool>("SettingFormatForm", "TableTitleWiderCheck", true); //表格字号 TableFontSize.SelectedItem = clsConfig.ReadConfig <string>("SettingFormatForm", "TableFontSize", "小四"); }