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.
@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.
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.
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?
As previously replied, there are still some issues using ImageOrPrintOptions.setDesiredSize method at present. Once there are updates, we will notify you promptly.
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?
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.
@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.