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

Free Support Forum - aspose.com

Does Aspose.Cells support previewing before printing?

Does Aspose.Cells support previewing before printing?1.png (92.2 KB)
Like this.

Aspose.Cells is API and not an interface so you can not preview like MS Excel. However, you can convert a worksheet to image and assume it to be the print preview, then you can print as per your requirements.

Thank you for your reply.

But ToImage gets a different image than print preview pagination.

Attached is the test code, including the Excel file used, the image obtained from ToImage, the screenshot of print preview and the screenshot of print.ToImageTest.zip (879.1 KB)

We have investigated bit more. Aspose.Cells API supports getting the count of number of pages for Print Preview. See the document below for example code:

You can combine this feature to set number of images you need for the output images. For example below code creates same images as the Print Preview images you have shared.

        Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(dataDir + "sampleSpecificPagesToImages.xlsx");
        Aspose.Cells.Rendering.ImageOrPrintOptions imgOptions = new Aspose.Cells.Rendering.ImageOrPrintOptions();
        Aspose.Cells.Rendering.WorkbookPrintingPreview preview = new Aspose.Cells.Rendering.WorkbookPrintingPreview(workbook, imgOptions);
        Console.WriteLine("Workbook page count: " + preview.EvaluatedPageCount);

        Aspose.Cells.Rendering.SheetPrintingPreview preview2 = new Aspose.Cells.Rendering.SheetPrintingPreview(workbook.Worksheets[0], imgOptions);
        Console.WriteLine("Worksheet page count: " + preview2.EvaluatedPageCount);
        // Open a template excel file
        Aspose.Cells.Workbook book = new Aspose.Cells.Workbook(dataDir + "sampleSpecificPagesToImages.xlsx");

        // Get the first worksheet.
        Aspose.Cells.Worksheet sheet = book.Worksheets[0];

        // Define ImageOrPrintOptions
        Aspose.Cells.Rendering.ImageOrPrintOptions imgOptions1 = new Aspose.Cells.Rendering.ImageOrPrintOptions();

        // Specify the image format
        imgOptions1.ImageType = Aspose.Cells.Drawing.ImageType.Jpeg;
        //This line makes the difference by setting number of pages
        imgOptions1.PageCount = preview.EvaluatedPageCount;

        // Render the sheet with respect to specified image/print options
        Aspose.Cells.Rendering.SheetRender sr = new Aspose.Cells.Rendering.SheetRender(sheet, imgOptions1);

        for (int i = 0; i < sr.PageCount; i++)
            Bitmap bitmap1 = sr.ToImage(i);

            // Save the image file
            bitmap1.Save(dataDir + "ToImage_" + i + ".jpg");
        Console.WriteLine("SpecificPagesToImage executed successfully.");

Let us know your feedback.

Thank you for your reply.

Execute ToImage in your code to get nine images.

But printing is printing 6 pages

It is printing 6 images on my end. Output images are attached:
Test Excel.zip (206.9 KB)

Could you try to set PageCount value as 6 by replacing below codes

imgOptions1.PageCount = preview.EvaluatedPageCount; 


imgOptions1.PageCount = 6;

and let us know your feedback.

I modified the code as you suggested.

The code is attached.

The result of the operation is different from the expected result.

Once again Exce previews 6 pages and the image is generated 9 pages.

Please help me modify the code.

Thank you very muchTest.zip (20.5 KB)

We are still getting 6 output images with your project, as attached. ToImage.zip (221.2 KB)

Could you please share the 9 output images for our analysis.

Test.zip (5.4 MB)
Attached is the source code of the program.
The resulting image is also inside.
I used the 20.5 version of the DLL

The resulting images are in the Debug directory

Test with the latest 20.7 release

The problem remains

We are analyzing this issue and need further assistance as follows:

Please make sure that:

  1. The DPI setting of your manchine is 100%
  2. The used font “等线” is installed

Then, run the following code:

Console.WriteLine("DPI :" + CellsHelper.DPI);
Workbook wb = new Workbook("Test.xlsx");
Console.WriteLine("Default font: " + wb.DefaultStyle.Font);

Cells cells = wb.Worksheets[0].Cells;

Console.WriteLine("Column A width : {0}, width  in Pixel: {1}, width in Inch: {2}.",
	cells.GetColumnWidth(0), cells.GetColumnWidthPixel(0), cells.GetColumnWidthInch(0));
Console.WriteLine("Column I width : {0}, width  in Pixel: {1}, width in Inch: {2}.",
	cells.GetColumnWidth(8), cells.GetColumnWidthPixel(8), cells.GetColumnWidthInch(8));


Share us the Console output text of above code and the saved pdf file “output.pdf”

I use the Windows 10 system.

Don’t know how to set DPI

“等线” fonts have been installed

The test code and output are attachedTest.zip (5.8 MB)

Thank you for sharing more information. We are analyzing it and will share our feedback soon.

We have analyzed your feedback and observed that “等线” is not used in the PDF. This issue seems to be related to fonts, therefore you may try the scenario again by adding following code in the beginning of your main().

 //set DengXian Regular font path on your machine 
string DengXianFontPath = //e.g. @"C:\Windows\Fonts\Deng.ttf" on my side
byte[] fontData = File.ReadAllBytes(DengXianFontPath);
Console.WriteLine("Font Data length: " + fontData.Length);
if(fontData.Length > 0)
    FontConfigs.SetFontSources(new FontSourceBase[] { new FileFontSource(DengXianFontPath) });

//Rest of the code is here

Please note that although, it seems that the font “等线” is installed in your environment according to the screenshot. But it seems that the font can’t be accessed from the program. So we suggested the above code at the beginning of your code.

Also we noted that your DPI is 96 which is OK. However you may test the scenario by setting DPI in Windows 10 as shown the attached image. This setting is there in Windows->Settings.
Win10_DPI_Setting.jpg (178.0 KB)

My Scale and layout was set to 100% before.

Add your suggested code.

The problem remains.

I don’t think it was the font that caused the problem.

Please help me with this problem.

Or print preview in the Aspose
.cells class library.

All the code and screenshots are attached.Test.zip (6.4 MB)
Attached is a screenshot of the Excel print preview

We have tested it again and shared the output on our side, everything is OK. And DengXian font is used in the output pdf file. Output_Aspose_side.zip (171.2 KB)

Could you please try your program on another machine with DPI setting (Display scaling) is 100% and font “等线” is installed.

Also, please try the following code on your side: We have only changed the default font name “等线” to “DengXian”(English name of the font)
ChangeDefaultFont.zip (1.2 KB)

Let us know your feedback.

I tested the program on another computer.

The result of the execution is the same as before.

What version of Aspose.Cells.dll did you use?

We have used the latest version of Aspose.Cells 20.7. We are analyzing this feedback and will share our comments soon.

Thank you for following up on this issue.

Waiting for your solution.

This problem has been bothering me for a long time