We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Size of Include Picture field is changed after updating it using Java

Hi,

I am currently evaluating a suitable word technology for my company. So far, Aspose looks very good, but I fail with the pictures.
Need: Within a table with a fixed cell size I want to use a include picture with a placeholder in it. (e.g. a logo wich can have different orientations).
Whatever I try the logo ist distored after replacement and not rezised…

Include picture looks like:
{INCLUDEPICTURE “{myplaceholder}” \x * MERGEFORMAT}

My code is something like:

Document document = new Document(“FILENAME.docx”, new LoadOptions
{
PreserveIncludePictureField = true,
UpdateDirtyFields = true
});

foreach (Field field in document.Range.Fields)
{
if (FieldType.FieldIncludePicture.ToString().Equals(field.Type.ToString()))
{
var includePicture = (FieldIncludePicture) field;
var placeholderString = GetPlaceholder(includePicture.SourceFullName);

                if (!string.IsNullOrEmpty(placeholderString))
                {
                    String imageUrl = ReplacePlaceholderString(placeholderString, data);

                    if (!string.IsNullOrEmpty(imageUrl))
                    {
                        includePicture.ResizeHorizontally = true;
                        includePicture.ResizeVertically = true;
                        includePicture.IsDirty = true;
                     
                        String temp = "c:\\images\\testimage.png";
                        includePicture.SourceFullName = temp;


                        includePicture.Update();
                    }
                }
            }
        }

document.UpdateFields();
document.save(…);

The Image is replaced, but I can not achieve that the image is resized in a way that it fits into the cell and resized correct…

It would be great if you could give me a hint.

Greetings from cologne -Chris

@ffchristianbecker

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

  • Your input Word document and images.
  • 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.

aspose.test.zip (243.3 KB)

Attached you can find an example Java-Project and the input / output document.

Thank you very much!

@ffchristianbecker

We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-20261. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

Thank you for your fast update @tahir.manzoor.
Could you suggest me a workaround until this issue is fixed?
There is no need for me to use include picture at the beginning.

I need to replace a placeholder with a picture in a table cell with fix size. For me it could also work to ad a text in a cell like {logo.url} and then insert a picrure with correct size…
For a hint I would be very thankfull!

Best regards- Christian

@ffchristianbecker

Unfortunately, there is no workaround available for this issue.

You can use DocumentBuilder.InsertImage to insert the image from URL. Please use the following code example to achieve your requirement. Hope this helps you.

LoadOptions loadOptions = new LoadOptions();
loadOptions.setUpdateDirtyFields(true);
loadOptions.setPreserveIncludePictureField(true);

Document document = new Document(MyDir + "input.docx", loadOptions );
DocumentBuilder builder = new DocumentBuilder(document);

for (Field field : document.getRange().getFields()) {
    if (field.getType() == FieldType.FIELD_INCLUDE_PICTURE) {
        FieldIncludePicture includePicture = (FieldIncludePicture) field;
        String placeholderString = getPlaceholder(includePicture.getSourceFullName());
        builder.moveTo(field.getStart());
        builder.insertImage("https://assets.kununu.com/images/images_logos/flowfact-gmbh-d883c.gif");
        field.remove();
    }
}
document.save(MyDir + "output.docx");

You can also use find and replace feature of Aspose.Words to find the text and replace it with image. In this case, we suggest you following solution.

  1. Implement IReplacingCallback interface.
  2. In IReplacingCallback.Replacing, move the cursor to the matched node.
  3. Insert the image using DocumentBuilder.InsertImage method.
  4. Remove the matched node.

Please read the following articles.
Find and Replace
Moving the Cursor

1 Like

The issues you have found earlier (filed as WORDSNET-20261) have been fixed in this Aspose.Words for .NET 20.6 update and this Aspose.Words for Java 20.6 update.