Insert Image at specific location

Hi,
I have a word document template in which form-fields (its legacy) are used to indicate specific locations in the document that text will be inserted. One such location is that of where we need to insert a stored image file. I can iterate over the collection of form-fields and upon identification of the appropriate form-field I’ve tried using builder.InsertImage, which does kind of work but the image is located at the top-left of the document, not at the location of the current form-field… How to I set the position so that InsertImage will place the image in the current location?

@ABew

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document and image.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a standalone console application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

AsposeSupport-InsertImage.zip (224.1 KB)

Hi,
Attached is a standalone application that demonstrates the issue.
Please refer to the AsposeSupport-InsertImage\Resources\ folder for the following;
Sample.xml - Word document to process
Test.png - image to insert
Output.doc - generated by application. demonstrates the image at the top-left rather than at appropriate form-field position

Regards,

@ABew

In your case, you need to move the cursor to the form field and then insert the image. You can use DocumentBuilder.MoveToField method to move the cursor to a field in the document.

We suggest you please read the following article.
Moving the Cursor

Following code example shows how to move the cursor to the form field and insert the image. Hope this helps you.

Document document = new Document(MyDir + "Sample.xml");
DocumentBuilder builder = new DocumentBuilder(document);
NodeCollection formfields = document.GetChildNodes(NodeType.FormField, true);

foreach (FieldStart fieldStart in document.GetChildNodes(NodeType.FieldStart, true))
{

    if (fieldStart.GetField().Type == FieldType.FieldFormTextInput)
    {
        builder.MoveToField(fieldStart.GetField(), true);
        builder.InsertImage(MyDir + "Test.png");
        fieldStart.GetField().Remove();
    }
}

document.Save(MyDir + @"19.10.docx");

@ABew

Further to my previous post, I have modified your code. Please check the following method. Hope this helps you.

private static bool InsertImageForFormField(FormField formField, Stream imageStream, DocumentBuilder builder, Document docTemplate)
{
    double width = 200;
    double height = 100;

    Node node = formField;

    while (node != null && node.NodeType != NodeType.FieldStart)
    {
	node = node.PreviousSibling;
    }

    if (node.NodeType == NodeType.FieldStart)
    {
	Field field = ((FieldStart)node).GetField();
	builder.MoveToField(field, true);
    }
    
    //builder.MoveToField()
    // Insert image with or without rescaling, based on the previously done analysis.    
    builder.InsertImage(imageStream, width, height);

    // Delete the form-field as its no-longer required.
    formField.RemoveField();

    // Signal the caller that the image was succesfully inserted at merge field position.
    return true;
}

Superb, works perfectly.
Thank you

@ABew

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.