【开发日记】使用Aspose.Words替换Word里的图片

office版本office2021-64,

Aspose.Words版本6.5.0.0,这个只能识别到word老版本即*.doc加载*.docx文件会报错

第一步:打开word文件并且插入图片

第二步:选择图片右键->选择图片->替换文字->在可选文件里输入名称

代码部分:

using Aspose.Words;
using Aspose.Words.Drawing;
internal class Program
{
    private static void Main(string[] args)
    {
        //模板文件地址
        string strTemplatePath = "";
        //替换后保存的文件地址
        string strSavePath = "";
        //图片地址
        string strImagePath = "";
        Document doc = new Document(strTemplatePath);
        NodeCollection nodes = doc.GetChildNodes(NodeType.Shape, true);
        foreach (Shape shape in nodes)
        {
            // AlternativeText:这个属性,在操作模板时 ,与可选文字一致
            var alternativeText = shape.AlternativeText;
            if (alternativeText.Equals("image"))
            {
                shape.ImageData.SetImage(strImagePath);
            }
        }
        //保存文件
        doc.Save(strSavePath);
        Console.WriteLine("保存成功!");
    }
}

 附Aspose.Words.dll+Aspose.Cells.dll文件

https://files.cnblogs.com/files/blogs/804227/Aspose.Words%E5%92%8CAspose.Cells.zip?t=1698033522&download=true

posted @ 2023-09-28 11:07  取个昵称显示已被使用  阅读(174)  评论(0编辑  收藏  举报