I want to use Spire .NET to replace an image in a template and save as the output docx file. I use the code below to replace it. My problem is I don't know how to set the image title for the image in MS Word so that Spire knows where my image is to replace.
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();
Right click on your picture -> Format Picture -> Layout & Properties -> ALT TEXT, then you will find the title part, see below picture (I'm using Office 2013).