Image inside frame

Hi,

I have a problem with an image inside a frame, which is smaller than image placeholder. When I add an image in Word it will be resized to fit a frame, but when I add it with aspose, image is too big and is cut at the bottom and also it loses its proportions. If I had a frame size I could resize image manualy, but I couldn’t find a frame object inside aspose at all. I would really appreciate any help with this.

I use similar code to this:

Document document = new Document(stream);
NodeCollection shapes = _document.GetChildNodes(NodeType.Shape, true);
foreach (Shape shape in shapes)
{
    shape.ImageData.ImageBytes = File.ReadAllBytes("C:\Aspose\dotnet-logo.png");
}

In the attachment you can find my original file, image and result of the above code.

Hi Agnieszka,

Thanks for your inquiry. Your document contains INCLUDEPICTURE field. There is no need to use ImageData.ImageBytes as you are using in your code.

In your case, we suggest you please use LoadOptions.PreserveIncludePictureField propery as shown below and correct the field codes. The field codes should be { INCLUDEPICTURE “path of image” }. You can press Alt+F9 in Microsoft Word to toggle display of field codes on or off.

LoadOptions options = new LoadOptions();
options.PreserveIncludePictureField = true;
Document doc = new Aspose.Words.Document(MyDir + "Image.docx", options);
doc.Save(MyDir + "Out.docx");

Hi Tahir,

thank you for your answer, unfortunately it will not work for me. I simplified document and the code for you, but in our case document is provided by client as it is. We cannot modify it. When the document includes following string: “{INCLUDEPICTURE “LOGO” * MERGEFORMAT}”, we are supposed to replace it with actual image which is taken from some other source as byte array. We previously used Word.Interop for this and it was working fine, now we are trying to switch to Aspose and I’ve tried different solutions and nothing seems to work. I was trying with PreserveIncludePictureField and also I’ve tried removing picture and put a new one, but in all cases the image was cut. We cannot ask our clients to change all their documents, we need to handle this situation in code.

Many thanks

Hi Agnieszka,

Thanks for your inquiry. Please use FieldIncludePicture.SourceFullName property to get/set the location of the picture as shown below. You can use this property to replace “LOGO” in following field codes with path of image.

{INCLUDEPICTURE "LOGO" \* MERGEFORMAT}

Unfortunately, this option generates the incorrect output. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-13094. You will be notified via this forum thread once this issue is resolved. We apologize for your inconvenience.

LoadOptions loadOptions = new LoadOptions();
loadOptions.PreserveIncludePictureField = true;
Document _document = new Aspose.Words.Document(MyDir + "Image.docx", loadOptions);
DocumentBuilder builder = new DocumentBuilder(_document);
foreach (Field field in _document.Range.Fields)
{
    if (field.Type.Equals(FieldType.FieldIncludePicture))
    {
        FieldIncludePicture includePicture = (FieldIncludePicture)field;
        includePicture.SourceFullName = @"c:\\temp\\dotnet-logo.png";
        includePicture.Update();
    }
}
_document.UpdateFields();
_document.Save(MyDir + "Out.docx");

The issues you have found earlier (filed as WORDSNET-13094) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.

Hi Agnieszka,

Thanks for your patience. We have introduced a way to update a field ignoring the MERGEFORMAT switch. Please use Field.Update method (Boolean) as shown below to get the required output.

LoadOptions loadOptions = new LoadOptions();
loadOptions.PreserveIncludePictureField = true;
Document _document = new Aspose.Words.Document(MyDir + "Image.docx", loadOptions);
DocumentBuilder builder = new DocumentBuilder(_document);
foreach (Field field in _document.Range.Fields)
{
    if (field.Type.Equals(FieldType.FieldIncludePicture))
    {
        FieldIncludePicture includePicture = (FieldIncludePicture)field;
        includePicture.SourceFullName = MyDir + @"dotnet-logo.png";
        includePicture.Update(true);
    }
}
_document.Save(MyDir + "Out.docx");