Hello,
we use Aspose Cells Java version 24.12 to extract as Image a range from an Excel file.
To save the image we use the class ImageOrPrintOptions.
We realized that the layout of the image change based on the “Only Area” property.
If the “Only Area” property is set to false, the layout reflects that of Excel.
If the “Only Area” property is set to true, the layout have some difference respect the Excel.
We have used this property because we thought that was necessary for create the image only for specific range, but now we have some dubs.
Perhaps, is this the code to export only the specific range?
PageSetup pageSetup = ws.getPageSetup();
pageSetup.setPrintArea(range.getAddress());
This is the "Only Area "definition: If this property is true , one Area will be output, and no scale will take effect.
Could you help us to understand the means of the definition and why the layout changes, please?
We attached the file IDS-2617.zip that contains:
- TestGetImage.java → you can use the tests getImageForIds2617PNG and getImageForIds2617EMF to reproduce the case
- IDS-2617.xlsx → the Excel file
- image-only-area-FALSE.emf → result in emf using Only Area eq FALSE
- image-only-area-TRUE.emf → result in emf using Only Area eq TRUE
- image-only-area-FALSE.png → result in png using Only Area eq FALSE
- image-only-area-TRUE.png → result in png using Only Area eq TRUE
IDS-2617.zip (159.6 KB)
Many thanks.
Regards,
Giulio
@giulio.andolfi
The “Only Area” property in Aspose.Cells determines how the specified range is rendered when exporting to an image. When this property is set to true
, only the defined area is outputted, and any scaling that might normally apply to the entire worksheet is ignored. This can lead to differences in layout compared to when the property is set to false
, where the entire worksheet layout is preserved.
Here’s a breakdown of how the “Only Area” property affects the output:
-
When “Only Area” is set to false
:
- The layout of the image closely matches what you see in Excel. This means that any formatting, scaling, and layout adjustments that are applied to the entire worksheet will also be reflected in the exported image.
-
When “Only Area” is set to true
:
- The output will only include the specified range, and the layout may differ from what is seen in Excel. This is because the rendering engine does not apply the same scaling and layout rules that would normally be applied to the entire worksheet. As a result, the image may not reflect the same visual structure as the original Excel file.
To export a specific range while ensuring that the layout is as expected, you can set the print area of the worksheet to the desired range. Here’s an example of how to do this in Java:
// Access the first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);
// Set the print area with your desired range
worksheet.getPageSetup().setPrintArea("E8:H10");
// Set image options
ImageOrPrintOptions options = new ImageOrPrintOptions();
options.setOnePagePerSheet(true); // This ensures the entire range is captured
options.setImageType(ImageType.JPEG); // Set the desired image type
// Render the worksheet to an image
SheetRender sr = new SheetRender(worksheet, options);
sr.toImage(0, "output.jpg");
In this code, you set the print area to the range you want to export, and then configure the image options accordingly. This should help you achieve the desired output while understanding the implications of the “Only Area” property.
If you continue to experience layout discrepancies, it may be beneficial to experiment with the settings or review the specific formatting applied to the cells within the range.
For further assistance, you might want to check the provided example code in the Aspose.Cells GitHub repository, which demonstrates how to export a range of cells to an image [1].
Sources:
[1]: ExportRangeofCells.java
@giulio.andolfi,
Thanks for the sample file, code snippet and image files.
After initial testing, I am able to reproduce the issue as you mentioned by using your template Excel file and sample code snippet. I found when creating images from Excel worksheet range, it renders different layouts with Boolean property “Only Area”. If the “Only Area” property of ImageOrPrintOptions is set to false, the layout reflects that of Excel. If the “Only Area” property of ImageOrPrintOptions is set to true, the layout have some difference respect to the Excel.
We will evaluate if it is expected behavior or something else.
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-46229
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.
Hello @amjad.sahi,
Perfect, let me know when there are news, please.
Thanks
@giulio.andolfi,
Sure, we will keep you posted with new updates once available on it.