/// <summary> /// Redact a shapes by replacing it with a 1px image. /// </summary> /// <param name="Document">The document containing the shape.</param> /// <param name="ShapeToRedact">The shape to redact.</param> private void RedactShape(Word.Document Document, Word.Shape ShapeToRedact) { string ImagePath = RedactCommon.CreateRedactedImage(); object AnchorRange = ShapeToRedact.Anchor; object Height = ShapeToRedact.Height; object Width = ShapeToRedact.Width; object Top = ShapeToRedact.Top; object Left = ShapeToRedact.Left; object WrapType = ShapeToRedact.WrapFormat.Type; ((Word.Range)AnchorRange).Select(); ShapeToRedact.Delete(); Word.Shape RedactedImage = Document.Shapes.AddPicture(ImagePath, ref Missing, ref Missing, ref Left, ref Top, ref Width, ref Height, ref AnchorRange); RedactedImage.AlternativeText = string.Empty; RedactedImage.WrapFormat.Type = (Word.WdWrapType)WrapType; }
/// <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; } }