Nina.Tang wrote:Hi,
Thanks for your inquiry.
Please refer to the following code. If it doesn’t fulfil your requirement, please provide your input Word file as well as your expected output for our reference.
- Code: Select all
static void Main(string[] args)
{
Document doc = new Document();
doc.LoadFromFile(InputFilePath);
//Repalce identifier with an image
Image image = Image.FromFile(imagepath);
ReplaceImage(doc, "identifier", image);
//Save the document
doc.SaveToFile(OutputFilePath, FileFormat.Docx);
}
private static void ReplaceImage(Document doc, string identifier,Image image)
{
//Find the identifier
TextSelection[] selections = doc.FindAllString(identifier, false, true);
foreach (TextSelection selection in selections)
{
TextRange tr = selection.GetAsOneRange();
//Get the paragraph in which the identifier is located
Paragraph par = tr.OwnerParagraph as Paragraph;
//Get the location of the identifier
int index = par.GetIndex(tr);
//Add a temporary paragraph
Paragraph parTem = doc.Sections[0].AddParagraph();
//Add the image in the paragraph
DocPicture picture = parTem.AppendPicture(image);
par.ChildObjects.Insert(index, picture);
//Set the image's wrap style
picture.TextWrappingStyle = TextWrappingStyle.Inline;
//Remove the paragraph
doc.Sections[0].Body.ChildObjects.Remove(parTem);
//Remove the identifier
par.ChildObjects.Remove(tr);
}
}
Sincerely,
Nina
E-iceblue support team
There are several questions:
1.The inserted picture is a thumbnail that has been processed and fits the document. But when inserted, it automatically zooms in, causing the document to deform.
2.I use the FreeSpire.Doc package on NuGet without the" Paragraph par = tr.OwnerParagraph as Paragraph;int index = par.GetIndex (tr);",The par.GetIndex () method was not found
Login to view the files attached to this post.