Rendering excel sheet to image neglecting the images that sheet has - *moved to another topic

Hello Team,

I am trying to render each sheet in excel to image but what I noticed is that the rendered image is only made from the area where cells are filled or have a style/formatting (depends on using “MaxDataRow” or “MaxRow” this is not our topic/issue, it happens when using both of them) and ignoring whether the sheet has an image and the image is not in the area that has data or style/formatting.

If the entire image is within the area, it will be included. If only part of it is within the area, it will be cut off and only that part will be included. If the image or part of it is outside the area, it will not be included.

So in short, Rendered image does not contain sheet images unless images are within the area where cells are filled or has a style/formatting.

So please could you investigate this issue? and let me know if there is a problem or this is the normal behavior or I am missing something in rendering options / config and what should I do?

Thanks in advance.

@muhammadzaghloul

Could you please provide the code you are using to render the Excel sheets to images and specify the version of Aspose.Cells you are using?

@muhammadzaghloul,

The behavior you described is expected and is the same as in Microsoft Excel. I manually tested your scenario using Microsoft Excel. I filled a sheet and inserted an image into it. Then, I specified and set the printable area (using the Sheet tab of the Page Setup dialog). I deliberately included only a part of the image within the printable area I had set. When I took the print preview in MS Excel, it only included the part of the image that was within the printable area, and did not cover the entire image.

For your requirements, you may try to use or set the OnePagePerSheet option of ImageOrPrintOptions to address your needs. Refer to the document with example code for further guidance.
https://docs.aspose.com/cells/net/convert-worksheet-to-image-remove-whitespace-around-data/

@muhammadzaghloul,

I thoroughly evaluated your scenario/case. I manually created a template Excel file (attached) in MS Excel. It contains some data and an image at the bottom in the worksheet. I noticed that the MaxRow and MaxColumn attributes might ignore the image, which is the expected behavior. Therefore, by default, the rendered image for the printable data range/area using the SheetRender API will not cover the complete image. The same display will be shown in MS Excel when manually setting the relevant Page Setup options and then taking the print preview of the sheet.

To handle this situation, you need to ensure that when converting a specified area to an image using SheetRender, any images that extend beyond the calculated MaxRow and MaxColumn are fully included in the printable area. You may simply use the image’s LowerRightRow and LowerRightColumn attributes to get the image boundaries. If an image extends beyond, you may adjust maxRow and maxColumn accordingly to ensure the entire image is covered/rendered. This can be done by adjusting the printable area dynamically based on the values of those extended images.
e.g.,
Sample code:

//Load the Excel file
Workbook workbook = new Workbook("e:\\test2\\Bk_test1.xlsx");
Worksheet sheet = workbook.Worksheets[0];
Cells cells = sheet.Cells;

//Get the maximum row and column used in the sheet
int maxRow = cells.MaxRow;
int maxColumn = cells.MaxColumn;

//Set the initial printable area based on maxRow and maxColumn
sheet.PageSetup.PrintArea = cells[0, 0].Name + ":" + cells[maxRow, maxColumn].Name;

//Get the collection of pictures in the worksheet
PictureCollection pictures = sheet.Pictures;

//Adjust the printable area to include any images fully
foreach (Aspose.Cells.Drawing.Picture picture in pictures)
{
            //Get the bottom-right corner of the image
            int imageEndRow = picture.LowerRightRow;
            int imageEndColumn = picture.LowerRightColumn;

            //Expand the printable area if the image extends beyond the current maxRow or maxColumn
            if (imageEndRow > maxRow)
            {
                maxRow = imageEndRow;
            }
            if (imageEndColumn > maxColumn)
            {
                maxColumn = imageEndColumn;
            }
}
//Update the printable area to include the images fully
sheet.PageSetup.PrintArea = cells[0, 0].Name + ":" + cells[maxRow, maxColumn].Name;

//Create ImageOrPrintOptions
ImageOrPrintOptions options = new ImageOrPrintOptions
{
    OnePagePerSheet = true,  //Fit the entire content to one page
    OnlyArea = true,
    ImageType  = ImageType.Png, //Set the output image format
    PrintingPage = PrintingPageType.IgnoreBlank
};

//Create SheetRender object to render the sheet
SheetRender render = new SheetRender(sheet, options);

//Render the sheet to an image
render.ToImage(0, "e:\\test2\\out1.png");

Please find attached the input Excel file and output image in the zipped archive for your reference.
files1.zip (551.3 KB)

Refer to the provided code segment and update your own code accordingly. If you still encounter any issues, please let us know with details and sample files.