首页 > 编程语言 > C# Aspose.Words 删除word中的图片操作
2021
01-13

C# Aspose.Words 删除word中的图片操作

今天介绍下 Aspose.Words 对 word 中的图片进行删除

string tempFile = Application.StartupPath + "\\resource\\templete\\项目建议书模板.doc";
Document doc = new Document(tempFile);
NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);
foreach (Shape item in shapes)
{
 if (item.HasImage)
 {
  item.Remove();
 }
}
doc.Save(docPath);

补充:C#word插入图片在指定标签位置(附加图片上下左右移动)

这一篇我就直接讲讲图片的添加和移动了

如上图是直接插入,插入位置是镶嵌类型,我想让它浮动在文字下面,且大小也想调动一下

object Nothing = System.Reflection.Missing.Value;
   try
   {
    //定义该插入图片是否为外部链接
    object linkToFile = false;
    //定义插入图片是否随word文档一起保存
    object saveWithDocument = true;
    
    //图片
    string replacePic = picture;
    if (doc.Bookmarks.Exists(bookMark_text) == true)
    {
     object bookMark = bookMark_text;
     //查找书签
     doc.Bookmarks.get_Item(ref bookMark).Select();
     //设置图片位置
     worldApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
     
     //在书签的位置添加图片
     InlineShape inlineShape = worldApp.Selection.InlineShapes.AddPicture(replacePic, ref linkToFile, ref saveWithDocument, ref Nothing);
     //设置图片大小
     inlineShape.Width = 100;
     inlineShape.Height = 100;
     inlineShape.Select();
     inlineShape.ConvertToShape().IncrementLeft(-60.0f); 
     //将图片设置浮动在文字上方
     inlineShape.ConvertToShape().WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapBehind;
 
    }
   }
   catch
   {
    doc.Saved = false;
    //word文档中不存在该书签,关闭文档
    doc.Close(ref Nothing, ref Nothing, ref Nothing);
   }

其中inlineShape.ConvertToShape()可以理解为选中这个图片

IncrementLeft();方法是要素水平移动,正值 代表向右移动,负值代表向左移动

IncrementTop(); 方法是要素垂直移动,正值代表向下移动,负值代表向上移动

WdWrapType是一个枚举器,里面有镶嵌类型,即

通过插入和移动就可以达到插入图片到自己想要的位置了

结果:

以上为个人经验,希望能给大家一个参考,也希望大家多多支持自学编程网。如有错误或未考虑完全的地方,望不吝赐教。

编程技巧