Hi,
The following file Amasia2 Inv 06-28-2021.docx (180.1 KB) is converting badly.
First page is a bit breaking the content of the table, I added screenshot comparing original page vs the converted page (bad conversion.png (81.8 KB)). I’m converting a docx file and creating a png per page.
Another problem, the 4’th page isn’t being converted, although it does contain a few break lines / whitespaces (not actual text).
Can you please help?
I’m using paid version 21.10.1.
Here’s the 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);
}
}
}
Also, can you please provide code for multiple pages conversion? Ans not 1 by 1.
Thank you very much,
Nir