Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue Nov 13, 2018 8:37 am

How to replace a specified identifier with a picture, the location of the identifier cannot be determined in the first paragraph of the document, and there will be multiple identifiers in a document that need to be replaced with a picture. I did not find any relevant instructions on the forum. Thank you!

1136636919
 
Posts: 2
Joined: Tue Nov 13, 2018 8:34 am

Tue Nov 13, 2018 9:29 am

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
User avatar

Nina.Tang
 
Posts: 1187
Joined: Tue Sep 27, 2016 1:06 am

Wed Nov 14, 2018 2:36 am

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

1136636919
 
Posts: 2
Joined: Tue Nov 13, 2018 8:34 am

Wed Nov 14, 2018 7:01 am

Hi,

Thanks for your feedback.
Below are my answers to your questions.
1. You could define the image size by using below code. If there is any question, please provide your image source as well as your input Word for our reference.
Code: Select all

//Add the image in the paragraph
DocPicture picture = parTem.AppendPicture(image);
//Define image size
picture.Height = 100.0f;
picture.Width = 100.0f;
par.ChildObjects.Insert(index, picture);

2. The GetIndex() method doesn’t exist in our free version. And sorry we have no plan to maintain free version at present. Please evaluate our commercial version instead.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1187
Joined: Tue Sep 27, 2016 1:06 am

Return to Spire.Doc