Load Images using Byte[], InputStream or URLs & Build Report from Word Template (Java) | Avoid Bitmap Lack of System Resources FileFormat 0 Issue

Hi,

Sometimes the generation of a Word document fails with this error:

Could not create the bitmap with the specified parameters. Possible lack of system resources.: Parameter is not valid. FileFormat=0

Any ideas what can cause this error? I only found one topic about this error in this forum.

We use Aspose-words-20.4-jdk17.

@bottemav,

Please try the latest 20.8 version of Aspose.Words for Java and see how it goes on your end? In case the problem still remains, please ZIP and upload your sample Word document (you are getting this problem with) and a standalone simple Java application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach here for testing. We will then investigate the issue on our end and provide you more information.

Hi,

I found the cause: an image element with an invalid url.

I created a testcase:
asposeBitmap.zip (14.6 KB)

@bottemav,

Yes, the problem appears to be because of a an invalid image link. In this case, can you please elaborate a bit more, what output do you expect from Aspose.Words?

We have also logged this in our issue tracking system. Your ticket number is WORDSNET-21006. We will further look into this scenario and will keep you updated on the status of the linked issue.

Hi @awais.hafeez,

We would like that no image is generated in that case and that a warning or error message appears in the logging of the Java program, not in Word document.

@bottemav,

We have logged these details in our issue tracking system and will keep you posted here on any further updates.

Can you please also check if INLINE_ERROR_MESSAGES option of Report Build Options Class solves the problem of error messages on your end?

I am quoting option’s description here for your reference:

The INLINE_ERROR_MESSAGES option can be used to specify that the engine should inline template syntax error messages into output documents. If this option is not set, the engine throws an exception when encounters a syntax error.

@bottemav,

It is to inform you that we have completed the analysis of WORDSNET-21006 and here are the details:

A simple suggestion would be to load the image separately and make use of a byte array (or InputStream) and pass image data to the reporting engine instead of String. Another option is to consider the following modified code:

Document doc = new Document("C:\\Temp\\asposeBitmap\\badUrl-v2.docx");

doc.setResourceLoadingCallback(new ImageFromUrlLoadingCallback());

ReportingEngine engine = getReportingEngine();
Object[] sources = new Object[]{new JsonDataSource(new ByteArrayInputStream("{}".getBytes(StandardCharsets.UTF_8)))};
String[] names = new String[]{"ds"};
engine.buildReport(doc, sources, names);

doc.save("C:\\Temp\\asposeBitmap\\badUrl-v2 Out.docx");

private static ReportingEngine getReportingEngine() {
    ReportingEngine engine = new ReportingEngine();
    engine.setOptions(ReportBuildOptions.ALLOW_MISSING_MEMBERS);
    engine.getKnownTypes().add(BigDecimal.class);
    engine.getKnownTypes().add(Double.class);
    engine.getKnownTypes().add(Integer.class);
    engine.getKnownTypes().add(String.class);
    return engine;
}

private static class ImageFromUrlLoadingCallback implements IResourceLoadingCallback {
    @Override
    public int resourceLoading(ResourceLoadingArgs args) throws Exception {
        if (args.getResourceType() != ResourceType.IMAGE)
            return ResourceLoadingAction.DEFAULT;

        try {
            args.setData(loadResourceFromUrl(args.getOriginalUri()));
            return ResourceLoadingAction.USER_PROVIDED;
        } catch (Exception ex) {
            // Here, an exception can be logged in any way instead of printing to console.
            System.out.println(ex.getClass().getCanonicalName() + ": " + ex.getMessage());

            // This skips default image loading.
            return ResourceLoadingAction.SKIP;
        }
    }

    private static byte[] loadResourceFromUrl(String urlSpec) throws Exception {
        try (InputStream inputStream = new URL(urlSpec).openStream()) {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[8192];
            int length;

            while ((length = inputStream.read(buffer, 0, buffer.length)) != -1)
                outputStream.write(buffer, 0, length);

            return outputStream.toByteArray();
        }
    }
}

With this second approach, you can fully control how images are loaded from URLs and handle possible errors.

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