Export Sheet Print Area to EMF image with desired size and resolution

Hello team,

I am trying to load a workbook with 1 sheet and export the print area to EMF output/image with a desired size and resolution. Based on these documentation Export Worksheet or Chart into Image with Desired Width and Height|Documentation and Converting Worksheet to Different Image Formats|Documentation, I was able to arrive at the below sample code in implementing my test case successfully but with some issues mentioned below.

Sample Code:

byte[] fileBytes = Files.readAllBytes(Paths.get("SampleWorkbook.xlsx"));
Workbook wb = new WorkBook(fileBytes);
Worksheet sheet = wb.getWorksheets().get(0);

PageSetup pageSetup = sheet.getPageSetup();
pageSetup.setTopMargin(0);
pageSetup.setBottomMargin(0);
pageSetup.setLeftMargin(0);
pageSetup.setRightMargin(0);
pageSetup.setPrintQuality(72); // I am setting a resolution of 72 DPI for my output, by default it is 96 DPI
pageSetup.customPaperSize(ReportUtils.pxToPt(400), ReportUtils.pxToPt(400)); //400px

String rangeName = "A1:P37";
pageSetup.setPrintArea(rangeName);

ImageOrPrintOptions imgOpt = new ImageOrPrintOptions();
imgOpt.setOnePagePerSheet(true);
imgOpt.setImageType(ImageType.EMF);
imgOpt.setTransparent(true);

SheetRender sr = new SheetRender(sheet, imgOpt);
sr.toImage(0, "SampleWorkbook.emf");

I have the following issues after checking the EMF output.

  • EMF size is not matching the provided 400 x 400 px size in fact it is showing as 1612 x 899 px
  • I am not sure if the 72 DPI resolution set is taking effect or not. How do I verify it?
  • EMF file size is 1.2 mb. How do I reduce the file size to the smallest value in KB?

I have attached the Sample Workbook used for testing and the generated EMF output by the above code.

Attachments: SampleWorkbook.zip (75.7 KB)

@oraspose
By using sample file and code for testing, we can reproduce the issue. It was found that after setting the expected size, the output image cannot obtain the expected size and resolution.

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): CELLSJAVA-45820

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

@John.He Thanks for trying out and logging a ticket for it.

By any chance, can we get this fixed in 24.01 release of Aspose.Cells library? Just being optimistic here considering the number of customers facing this issue when using our product will be large.

@oraspose,

You are welcome.

For your information, we published the Aspose.Cells for Java v24.1 release earlier today. So, your issue might be fixed in the next version.

@oraspose ,

ImageOrPrintOptions.setHorizontalResolution(int value) and ImageOrPrintOptions.setVerticalResolution(int value) are used to set image resolution.

ImageOrPrintOptions.setDesiredSize(int desiredWidth, int desiredHeight, boolean keepAspectRatio) is used to set desired image dimension.

If you set imgOpt.setOnePagePerSheet(true), your customed paper will be ignore. The dimenson of the output EMF image dimenson in inch only depends on content. So, you can only get desired resolution by ImageOrPrintOptions.setHorizontalResolution(int value) and ImageOrPrintOptions.setVerticalResolution(int value), or only get desired dimension by ImageOrPrintOptions.setDesiredSize(int desiredWidth, int desiredHeight, boolean keepAspectRatio).

        int desiredResolution = 72;
        Workbook wb = new Workbook(srcFile);
        Worksheet sheet = wb.getWorksheets().get(0);

        PageSetup pageSetup = sheet.getPageSetup();
        pageSetup.setTopMargin(0);
        pageSetup.setBottomMargin(0);
        pageSetup.setLeftMargin(0);
        pageSetup.setRightMargin(0);
//      pageSetup.setPrintQuality(72); // I am setting a resolution of 72 DPI for my output, by default it is 96 DPI
//      pageSetup.customPaperSize(400f / desiredResolution, 400f / desiredResolution); //400px, the set unit is inch

        String rangeName = "A1:P37";
        pageSetup.setPrintArea(rangeName);

        ImageOrPrintOptions imgOpt = new ImageOrPrintOptions();
        imgOpt.setOnePagePerSheet(true);
        imgOpt.setImageType(ImageType.EMF);
        imgOpt.setTransparent(true);
//      imgOpt.setDesiredSize(400, 400, false);
        imgOpt.setHorizontalResolution(desiredResolution);
        imgOpt.setVerticalResolution(desiredResolution);

        SheetRender sr = new SheetRender(sheet, imgOpt);
        sr.toImage(0, "output.emf");

Currently there is an issue to get desired dimension by ImageOrPrintOptions.setDesiredSize(int desiredWidth, int desiredHeight, boolean keepAspectRatio). We will fix it.

@oraspose

You can check the resolution in some Image viewers.

Use ImageOrPrintOptions.setOptimized(true) can reduce the file size of generated EMF image a little.

@peyton.xu Thanks for the response. Will check this out.

@oraspose
You are welcome. We will notify you immediately of any updates.

Our product is currently running Aspose.Cells 21.3 version and we are in process of upgrade to latest. In the meanwhile, Is there a way to set desired size with aspectRatio maintained for the EMF Image?

@oraspose

As previously replied, there are still some issues using ImageOrPrintOptions.setDesiredSize method at present. Once there are updates, we will notify you promptly.

Thank you for the response. Will watch this thread closely for any updates.

@oraspose
You are welcome.

@oraspose

Please check the prefix results with setDesiredSize(400, 400, false) and setDesiredSize(400, 400, true) for your reference. Please note, if keepAspectRatio is set to true, the dimension of output EMF can only match desired width or desired height.
prefix_EMF_images.zip (62.9 KB)

@peyton.xu Thanks for trying and providing the sample generated EMF for desired size. It looks good but the EMF image size looks larger when compared to the EMF image generated by ASPOSE.Words library by passing the HTML bytes and saving the document as EMF image.

Is their a way to render the print area of sheet with 72 DPI while exporting as EMF?

I have attached the sample HTML along with Aspose Words generated Word Document and EMF output based on the sample logic provided in this Incorrect Table Layout when saving HTML Table to DOCX and EMF.

Attachment: Sample Table using Aspose Words.zip (35.0 KB)

@oraspose,

Thanks for your feedback with resource files.

We have logged the archive with your existing ticket “CELLSJAVA-45820” into our database. We will look into it and get back to you soon.

1 Like

@oraspose

Please use the following code to get 72 DPI for the generated EMF image.

@oraspose

For reducing the file size of generated EMF image, we have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): CELLSJAVA-45828

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Thank you for logging a ticket for this issue. We will monitor this thread for further updates.

@oraspose
You are welcome. We will notify you immediately of any updates.

@peyton.xu I used this same logic to generate the EMF with 72 DPI but it looks like the EMF image still shows 96 dpi when previewed using MS Paint tool.

EMF Output: SampleWorkbook.zip (64.7 KB)

DPI validation using Paint tool: SampleWorkbook.png (86.8 KB)

Also, I noticed that the complete print area is not printed in the EMF when below API is used.

imgOpt.setHorizontalResolution(desiredResolution);
imgOpt.setVerticalResolution(desiredResolution);

I think the above API overrides the effect of this API imgOpt.setOnePagePerSheet(true); on the EMF generation process.

How to overcome this problem?