Convert Table to EMF image and optimize the output

Hello Team,

We are using ASPOSE WORDS Java v24.2 to convert TABLE into EMF output using the below sample code.

byte[] htmlBytes = Files.readAllBytes("SampleTable.html");
HtmlLoadOptions options = new HtmlLoadOptions();
options.setConvertMetafilesToPng(false);
options.setEncoding(StandardCharsets.UTF_8);

Document doc = new Document(htmlBytes, options);
DocumentBuilder builder = new DocumentBuilder(doc);
PageSetup pageSetup = builder.getPageSetup();
double margin = 0;
pageSetup.setLeftMargin(margin);
pageSetup.setRightMargin(margin);
pageSetup.setTopMargin(margin);
pageSetup.setBottomMargin(margin);
pageSetup.setHeaderDistance(margin);
pageSetup.setFooterDistance(margin);
pageSetup.setPaperSize(PaperSize.CUSTOM);
pageSetup.setPageWidth(1150);
pageSetup.setPageHeight(695);

Section section = doc.getFirstSection();
Body body = section.getBody();
TableCollection tables = body.getTables();
if (tables.getCount() == 1) {
	Table table = tables.get(0);
	table.setLeftIndent(0);
	table.autoFit(AutoFitBehavior.AUTO_FIT_TO_WINDOW);
        ... // other table row and cell layout settings are performed
}

// Save as EMF format
ImageSaveOptions emfOptions = new ImageSaveOptions(SaveFormat.EMF);
emfOptions.setPageSet(new PageSet(0));
emfOptions.setResolution(72f);
emfOptions.setTransparent(true);
emfOptions.setPaperColor(new Color(255, 255, 255, 0));
emfOptions.setOptimized(true);
doc.save("Test.emf", emfOptions);

We have few queries regarding the EMF output using the above code.

  1. How do I validate the DPI of the EMF image set using this method emfOptions.setResolution(72f)? Because, when I open the image using MS Paint tool it always shows DPI as 96.
  2. Whether I use this method emfOptions.setOptimized(true); or not, the EMF output size remains the same which is 240Kb(approx.)? Does this method have any effect on vector images such as EMF? If yes, then how do I optimize the EMF image further more to obtain a more reduced size? I do not have a number but would like to know to what extent I can reduce the EMF size further.

SampleTable.zip (2.9 KB)

@oraspose

  1. Resolution does not have sense in EMF format because EMF format is vector format.

  2. Do you mean OptimizeOutput option? Aspose.Words.ImageSaveOptions does not have Optimized property.
    Setting ImageSaveOptions.OptimizeOutput property does not guaranty minimizing the output document size. All depends on the input document content.

When do we use this API ImageSaveOptions.setOptimizeOutput(boolean)?

@oraspose Generally it is not recommended to use this option unless you are sure the result will be improved. The accuracy of the content display may be affected if this property is set to true .

@alexey.noskov Thank you for this information.

1 Like