C# Aspose.Words 删除word中的图片操作
时间:2021-05-16 09:38:32|栏目:.NET代码|点击: 次
今天介绍下 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是一个枚举器,里面有镶嵌类型,即

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

上一篇:c#如何实现接口事件
栏 目:.NET代码
下一篇:使用C#实现基于TCP和UDP协议的网络通信程序的基本示例
本文标题:C# Aspose.Words 删除word中的图片操作
本文地址:http://www.codeinn.net/misctech/122592.html






