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

Free Support Forum - groupdocs.com

Weird bug when converting a doc file to a png

Hi,
I use GroupDocs.Coversion 21.10.1. and tried to convert this file into a png.
doc file: 1_test.7z (4.1 KB)
The issue was reproduced :
OS (running on AWS): Amazon FARGATE so it’s Linux/Unix, Amazon Linux 2.0.20220121, Java 11, JDK Amazon Corretto 11.0.11

Here’s the exception stacktrace:
com.groupdocs.conversion.internal.c.a.ms.System.ArgumentException: An element with the same key already exists in the dictionary.
at com.groupdocs.conversion.internal.c.a.ms.System.Collections.Generic.Dictionary.addItem(Unknown Source) ~[groupdocs-conversion-21.10.1.jar:21.10.1]
at com.groupdocs.conversion.converting.PossibleConversionsProvider.init(Unknown Source) ~[groupdocs-conversion-21.10.1.jar:21.10.1]
at com.groupdocs.conversion.converting.PossibleConversionsProvider.initStatic(Unknown Source) ~[groupdocs-conversion-21.10.1.jar:21.10.1]
at com.groupdocs.conversion.Converter.init(Unknown Source) ~[groupdocs-conversion-21.10.1.jar:21.10.1]
at com.groupdocs.conversion.Converter.(Unknown Source) ~[groupdocs-conversion-21.10.1.jar:21.10.1]
at com.groupdocs.conversion.Converter.(Unknown Source) ~[groupdocs-conversion-21.10.1.jar:21.10.1]

CONVERSION CODE:

import com.groupdocs.conversion.Converter;
import com.groupdocs.conversion.filetypes.FileType;
import com.groupdocs.conversion.options.convert.ImageConvertOptions;

import java.io.InputStream;

public class FileConvertor2 {

private static final Logger logger = Logger.getLogger(FileConvertor2.class);

public static int convert(InputStream inputStream, FileFormat inputFormat, FileFormat outputFormat) throws Exception {
    try {
        final Converter converter = new Converter(inputStream);
        final ImageConvertOptions options = new ImageConvertOptions();
        tryToEnhanceQuality(options, inputFormat);
        options.setFormat(FileType.fromExtension(outputFormat.getExtension()));
        options.setPagesCount(1); // currently we convert just 1 page separately
        final int totalDocumentPages = converter.getDocumentInfo().getPagesCount();
        for (int i = 1; i <= totalDocumentPages; i++) {
            try (ByteBufferOutputStream outputStream = new ByteBufferOutputStream()) {
                options.setPageNumber(i); // The page index to be converted
                converter.convert(outputStream, options);
                // Save the output stream to an external source....
            } catch (Exception e) {
                logger.error(e, "Failed to convert page #%s to %s", i, options.getFormat().getExtension());
                throw e;
            }
        }
        return totalDocumentPages;
    } catch (Exception e) {
        logger.error(e, "Failed to convert file to %s", outputFormat.getExtension());
        throw e;
    }
}

private static void tryToEnhanceQuality(ImageConvertOptions options, FileFormat inputFormat) {
    int resolutionDPIVal = -1;
    // Approximate print-size of PDF: width 8.27 X  height 11.69 INCH
    switch (inputFormat) {
        case DOC:
        case DOCX:
        case JPG:
        case JPEG:
            // Why 300?
            // Because the maximum resolution we aim is 2200 X 3000 which is about 220-300 DPI.
            // Also, the default DPI value of a Microsoft Word doc is 220 DPI (Windows 10) and we want to maintain/enhance that quality when converting to image
            resolutionDPIVal = 300;
            break;
        default:
            break;
    }
    if (resolutionDPIVal > 0) {
        options.setHorizontalResolution(resolutionDPIVal);
        options.setVerticalResolution(resolutionDPIVal);
    }
}

}

Thank you

@nirm

Could you please share a working console application?

That’s all the info I have, this was taken from our cloud logs. Not really sure what more data do you want.
I used the conversion code that is included above.

1 Like

I added a small java file which is my conversion application: FileConvertor.7z (1.2 KB)
Please try to reproduce the “bug” on a Linux machine and not Windows using my java class and input file.
Input file again: 1_test.7z (4.1 KB)
Here’s another file to try: 2_test.zip (142.6 KB)
Thank you

1 Like

@nirm

Thank you for sharing the concerned files/details. We are investigating this issue with ticket ID CONVERSIONJAVA-1567. You will be notified in case of any update.

We tried on multiple machines (e.g. Kali Linux). It didn’t give any exception. However, we think it’s a environment related or API configuration issue.
However, in order to further investigate it, we need an example project. We’ll test that at our end.