Cannot load image from field 'Image'. The field contains data in unsupported format. buffer

Hi there,

I need to merge an image in .doc document template. I am receiving payload with all merge fields and image is coming in base64 format. I have added below code in HandleMergeField.java file.

public void imageFieldMerging(ImageFieldMergingArgs args) throws Exception {
		BufferedImage image = null;
        byte[] imageByte;
        try {
            
            imageByte = Base64.getDecoder().decode(args.getFieldValue().toString());
            ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
            // Now the mail merge engine will retrieve the image from the stream
            args.setImageStream(bis);
            bis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
 }

But I am getting Cannot load image from field ‘Image’. The field contains data in unsupported format. Need your help please.

@vapanchamukhi Could you please attach the problematic image here for testing? We will check the issue and provide you more information.

My analysis earlier was wrong. Merging of image is working fine but in html template I have an image embedded using img tag and when I am converting the document to doc or pdf after merging all merge fields it is getting corrupted. Template for testing I am using below html snippet.

<html>
<body>
    <img width=300 height=35 id="_x0000_i1025"
         src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"
         alt="Google logo">
</body>
</html>

Output pdf file after converting I have attached.

test1.pdf (22.5 KB)

@vapanchamukhi Unfortunately I cannot reproduce the issue on my side. The image is correctly loaded and embedded into the output PDF document. Could you please verify that Aspose.Words have access to load image from the web? You could use LoadOptions.ResourceLoadingCallback to control loading process.

What should I be checking to check wither Aspose.Words has access to load image from web? Is there any property I need to set for this?

As per example code below we are getting logo from location on server and setting it. Can’t we have images from web? If its possible how to do that?

case ResourceType.IMAGE: {
	// Replaces all images with a substitute
	String newImageFilename = "logo.png";
	System.out.println("\tImage will be substituted with: " + newImageFilename);
	BufferedImage newImage = ImageIO
			.read(new File(getDataDir(HtmlLinkedResourceLoadingCallback.class) + newImageFilename));
			

	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	ImageIO.write(newImage, "png", baos);
	baos.flush();

	byte[] imageBytes = baos.toByteArray();
	baos.close();

	args.setData(imageBytes);

	// New images will be used instead of presented in the document
	return ResourceLoadingAction.USER_PROVIDED;
}

@vapanchamukhi Actually, Aspose.Words automatically download images from web, but the images should be accessible. In your implementation of IResourceLoadingCallback you can configure credentials used to download the image from web.

hi,
i encounter the same problem. i use imageio to read from disk. doesn’t support this way?

@softboy Could you please provide code, template and the problematic image that will allow us to reproduce the problem? We will check the issue and provide you more information.

Hi,
How to make the image(which merged out from image field) 's width,height auto fit the table cell, which hosted in? In Ms word, After checking off the autofit table to contents in table properties dialog, then insert image to the cell, it’s can auto fit the table cell’s width && height

@softboy Aspose.Words also automatically autofit the image to the table cell. If possible, please provide your template, sample data and code that will allow us to reproduce the problem? We will check your scenario and provide you more information.