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