我想使用 Spire .NET 替换模板中的图像并保存为输出 docx 文件。我使用下面的代码来替换它。我的问题是我不知道如何在 MS Word 中为图像设置图像标题,以便 Spire 知道要替换我的图像的位置。
Document document = new Document("template.docx");
//Loop through the paragraphs of the section
foreach (Paragraph paragraph in document.Sections[0].Paragraphs)
{
//Loop through the child elements of paragraph
foreach (DocumentObject docObj in paragraph.ChildObjects)
{
if (docObj.DocumentObjectType == DocumentObjectType.Picture)
{
DocPicture picture = docObj as DocPicture;
if (picture.Title == "logo") // <--- I don't know how to set title in Word
{
//Replace the image
picture.LoadImage(Image.FromFile("logo.png"));
}
}
}
}
document.SaveToFile("generated.docx");
document.Close();