We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

ImageOrPrintOptions IsCellAutoFit not working when creating an image from cell range

Hi,

We are evaluating your trial version for our project needs.
I was trying to create an image file from the excel cell range and getting an issue with the cell values
rendering. When the image gets created the cell values are trimmed to the columns width even though i have set IsCellAutoFit=true. (you can see this behavior in Name and Industry columns in the attached image).
But we need the cell values to be visible for all the columns, without modifying the columns width in the excel sheet.
I have attached the image for your reference.
image.png (33.3 KB)

Please let us know what we have to do to resolve this issue.
Below is the code which converts excel range to image.

    public Bitmap GenerateImageFromExcelCellRange(string sourceFilePath, string sheetName, ExcelImageType excelImageType, string firstCellRange, string lastCellRange,
            int? desiredWidth = null, int? desiredHeight = null, string imageOutputDir = null)
        {
            Workbook workbook = new Workbook(sourceFilePath);
            Worksheet worksheet = workbook.Worksheets[sheetName];

            #region image settings

            // Set all margins as 0
            worksheet.PageSetup.LeftMargin =
            worksheet.PageSetup.RightMargin =
            worksheet.PageSetup.TopMargin =
            worksheet.PageSetup.BottomMargin = 0;

            // Set OnePagePerSheet option as true
            ImageOrPrintOptions options = new ImageOrPrintOptions();
            options.OnePagePerSheet = true;
            options.ImageType = ImageType.Png;// Always use Png as its file size is 8 times lesser than of Jpeg.
            options.HorizontalResolution = 200;
            options.VerticalResolution = 200;
            options.IsCellAutoFit = true;

            if (desiredHeight != null && desiredWidth != null)
                options.SetDesiredSize(Convert.ToInt32(desiredWidth), Convert.ToInt32(desiredHeight));

            #endregion

            if (excelImageType == ExcelImageType.TabularData)
            {
                // Set the print area with your desired range
                worksheet.PageSetup.PrintArea = string.Concat(firstCellRange, ":", lastCellRange);// "D8:G16";
                
                // Take the image of your worksheet
                SheetRender sr = new SheetRender(worksheet, options);

                if (IsSaveImageToFolderRequired(imageOutputDir))
                    sr.ToImage(0, imageOutputDir + "excellCellRange_TabularData.png");

                return sr.ToImage(0);
            }
            else if (excelImageType == ExcelImageType.ChartData)
            {
                Aspose.Cells.Charts.Chart chart = worksheet.Charts[0];

                if (IsSaveImageToFolderRequired(imageOutputDir))
                    chart.ToImage(imageOutputDir + "Chart.png", System.Drawing.Imaging.ImageFormat.Png);

                return chart.ToImage(options);
            }
            else
            {

            }

           return null;
        }

@PrathapSV,

Thanks for the sample code and details.

Please provide your template Excel file (you may zip prior attaching) so we could evaluate your issue. Moreover, please simplify your sample code a bit (we might not compile it as we are not sure about some objects, variables and their data) and provide either a console application or updated sample code (runnable), so we could evaluate your issue precisely and to consequently figure it out soon.