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插入圖片在指定標簽位置(附加圖片上下左右移動)
這一篇我就直接講講圖片的添加和移動了

如上圖是直接插入,插入位置是鑲嵌類型,我想讓它浮動在文字下面,且大小也想調(diào)動一下
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文檔中不存在該書簽,關(guān)閉文檔
doc.Close(ref Nothing, ref Nothing, ref Nothing);
}
其中inlineShape.ConvertToShape()可以理解為選中這個圖片
IncrementLeft();方法是要素水平移動,正值 代表向右移動,負值代表向左移動
IncrementTop(); 方法是要素垂直移動,正值代表向下移動,負值代表向上移動
WdWrapType是一個枚舉器,里面有鑲嵌類型,即

通過插入和移動就可以達到插入圖片到自己想要的位置了
結(jié)果:

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
- C#語言MVC框架Aspose.Cells控件導出Excel表數(shù)據(jù)
- C#使用Aspose.Cells導出excel
- C#使用Aspose.Cells創(chuàng)建和讀取Excel文件
- C#使用Aspose.Cells控件讀取Excel
- C# 利用Aspose.Words.dll將 Word 轉(zhuǎn)成PDF
- C# 調(diào)用exe傳參,并獲取打印值的實例
- 如何利用C#打印九九乘法表
- c# winform 解決PictureBox 無法打印全部圖片的問題
- C#操作Word打印的示例
- C# 打印網(wǎng)頁不顯示頁眉頁腳的實現(xiàn)方法
- C#實現(xiàn)掃描槍掃描二維碼并打印(實例代碼)
- C#利用PrintDocument定制打印單據(jù)的小例子
- c#使用Aspose打印文件的示例

