/// <summary> /// 规范表格,而且删除表格中的嵌入式图片 /// </summary> /// <param name="TableStyle">要应用的表格样式</param> /// <param name="ParagraphFormat">表格中的段落样式</param> /// <param name="blnDeleteShapes">是否要删除表格中的图片,包括嵌入式或非嵌入式图片。</param> /// <remarks></remarks> public void TableFormat(string TableStyle = "zengfy表格-上下总分型1", string ParagraphFormat = "表格内容置顶", bool blnDeleteShapes = false) { var Selection = _app.Selection; if (Selection.Tables.Count > 0) { //定位表格 Table Tb = default(Table); Range rg = default(Range); foreach (Table tempLoopVar_Tb in Selection.Range.Tables) { Tb = tempLoopVar_Tb; rg = Tb.Range; _app = Tb.Application; // _app.ScreenUpdating = false; //调整表格尺寸 dynamic with_1 = Tb; with_1.AutoFitBehavior(WdAutoFitBehavior.wdAutoFitContent); with_1.AutoFitBehavior(WdAutoFitBehavior.wdAutoFitWindow); //清除表格中的超链接 Hyperlinks hps = default(Hyperlinks); hps = rg.Hyperlinks; int hpCount = 0; hpCount = hps.Count; for (var i = 1; i <= hpCount; i++) { hps[1].Delete(); } //将手动换行符删除 { Tb.Range.Find.ClearFormatting(); Tb.Range.Find.Replacement.ClearFormatting(); Tb.Range.Find.Text = "^l"; Tb.Range.Find.Replacement.Text = ""; Tb.Range.Find.Execute(WdReplace.wdReplaceAll); } //删除表格中的乱码空格 { Tb.Range.Find.ClearFormatting(); Tb.Range.Find.Replacement.ClearFormatting(); Tb.Range.Find.Text = " "; Tb.Range.Find.Replacement.Text = " "; Tb.Range.Find.Execute(WdReplace.wdReplaceAll); } //删除表格中的嵌入式图片 if (blnDeleteShapes) { InlineShapes inlineshps = default(InlineShapes); int Count = 0; InlineShape inlineShp = default(InlineShape); inlineshps = Tb.Range.InlineShapes; Count = inlineshps.Count; for (var i = Count; i >= 1; i--) { inlineShp = inlineshps[Convert.ToInt32(i)]; inlineShp.Delete(); } //删除表格中的图片 ShapeRange shps = default(ShapeRange); Shape shp = default(Shape); shps = Tb.Range.ShapeRange; Count = shps.Count; for (var i = Count; i >= 1; i--) { shp = shps[i]; shp.Delete(); } } //清除表格中的格式设置 rg.Select(); Selection.ClearFormatting(); // ----- 设置表格样式与表格中的段落样式 try //设置表格样式 { Tb.set_Style(TableStyle); } catch (Exception) { } try //设置表格中的段落样式 { rg.ParagraphFormat.set_Style(ParagraphFormat); } catch (Exception) { } } //取消选择并刷新界面 Selection.Collapse(); _app.ScreenRefresh(); _app.ScreenUpdating = true; } else { MessageBox.Show("请至少选择一个表格。", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } }
/// <summary> /// 嵌入式图片加边框 /// </summary> /// <param name="wdApp"></param> /// <param name="pictureParagraphStyle">此图片所在段落的段落样式</param> /// <remarks></remarks> public static void AddBoadersForInlineshapes(Application wdApp, string pictureParagraphStyle) { Selection selection = wdApp.Selection; int picCount; //选中区域中嵌入式图片的张数 picCount = selection.InlineShapes.Count; if (picCount == 0) { MessageBox.Show("没有发现嵌入式图片", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { wdApp.ScreenUpdating = false; // InlineShape Pic = default(InlineShape); foreach (InlineShape tempLoopVar_Pic in selection.Range.InlineShapes) { Pic = tempLoopVar_Pic; //显示出图片的边框来,不然下面的设置边框线宽就会报错 //用下面的Enable语句将图片的四个边框同时显示出来 Pic.Borders.Enable = 1; // true; //To remove all the borders from an object, set the Enable property to False. //也可以用pic.Borders(wdBorderLeft).visible = True将图片的四条边依次显示出来。 //而对于一般的图片(即jpg等图片,而不是像AutocAD、Visio等嵌入式的对象), //只要设置了任意一条边的visible为true,则四条边都会同时显示出来。 Range rg = default(Range); rg = Pic.Range; try { // rg.ParagraphFormat.Style = ParagraphStyle; rg.ParagraphFormat.set_Style(pictureParagraphStyle); // 这张图所在段落的样式为"图片" } catch (Exception) { MessageBox.Show("请先向文档中添加样式\"图片\"", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Information); } //对于表格中的图片,如果单元格中仅仅只有这一张图片的话,下面的添加边框的代码会失效。 //此时要先在单元格的图片后面插入一个字符,然后添加边框,最后将字符删除。 rg.Collapse(Direction: WdCollapseDirection.wdCollapseEnd); rg.InsertAfter(" "); //下面设置图片边框的线宽;这一定要在图片有边框时才可用,不然会报错。 dynamic with_1 = Pic; with_1.Select(); Border BorderSide = default(Border); foreach (Border tempLoopVar_BorderSide in with_1.Borders) { BorderSide = tempLoopVar_BorderSide; dynamic with_2 = BorderSide; with_2.LineStyle = WdLineStyle.wdLineStyleSingle; //边框线型wdLineStyleNone表示无边框 with_2.LineWidth = WdLineWidth.wdLineWidth025pt; // 边框线宽 with_2.Color = WdColor.wdColorBlack; // 边框颜色 } //下一个边框 //设置图片的大小 with_1.ScaleHeight = 80; // 以 80% 的比例进行缩放 with_1.ScaleWidth = 80; rg.Collapse(WdCollapseDirection.wdCollapseStart); rg.Delete(Unit: WdUnits.wdCharacter, Count: 1); } //下一张图片 // selection.Collapse(); selection.MoveRight(); wdApp.ScreenRefresh(); } wdApp.ScreenUpdating = true; }