Preview workbook with JavaScript via C++

There may be cases where Excel files with millions of pages need to be converted to PDF or images. Processing such files will consume a lot of time and resources. In such cases, the Workbook and Worksheet Print Preview feature might prove to be useful. Before converting such files, the user can check the total number of pages and then decide whether the file is to be converted or not. This article focuses on using the WorkbookPrintingPreview and SheetPrintingPreview classes to find out the total number of pages.

Aspose.Cells provides the print preview feature. For this, the API provides WorkbookPrintingPreview and SheetPrintingPreview classes. To create the print preview of the whole workbook, create an instance of the WorkbookPrintingPreview class by passing Workbook and ImageOrPrintOptions objects to the constructor. The WorkbookPrintingPreview class provides an evaluatedPageCount method which returns the number of pages in the generated preview. Similar to WorkbookPrintingPreview class, the SheetPrintingPreview class is used to generate a print preview for a specific worksheet. To create the print preview of a worksheet, create an instance of the SheetPrintingPreview class by passing Worksheet and ImageOrPrintOptions objects to the constructor. The SheetPrintingPreview class also provides an evaluatedPageCount method which returns the number of pages in the generated preview.

The following code snippet demonstrates the use of both WorkbookPrintingPreview and SheetPrintingPreview classes by using the sample excel file.

Sample Code

<!DOCTYPE html>
<html>
    <head>
        <title>Aspose.Cells Example - Printing Preview</title>
    </head>
    <body>
        <h1>Printing Preview Example</h1>
        <input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
        <button id="runExample">Run Example</button>
        <a id="downloadLink" style="display: none;">Download Result</a>
        <div id="result"></div>
    </body>

    <script src="aspose.cells.js.min.js"></script>
    <script type="text/javascript">
        const { Workbook, ImageOrPrintOptions, WorkbookPrintingPreview, SheetPrintingPreview } = AsposeCells;
        
        AsposeCells.onReady({
            license: "/lic/aspose.cells.enc",
            fontPath: "/fonts/",
            fontList: [
                "arial.ttf",
                "NotoSansSC-Regular.ttf"
            ]
        }).then(() => {
            console.log("Aspose.Cells initialized");
        });

        document.getElementById('runExample').addEventListener('click', async () => {
            const fileInput = document.getElementById('fileInput');
            if (!fileInput.files.length) {
                document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
                return;
            }

            const file = fileInput.files[0];
            const arrayBuffer = await file.arrayBuffer();

            // Instantiating a Workbook object from the uploaded file
            const workbook = new Workbook(new Uint8Array(arrayBuffer));

            // Creating image/print options
            const imgOptions = new ImageOrPrintOptions();

            // Workbook printing preview
            const preview = new WorkbookPrintingPreview(workbook, imgOptions);
            const workbookPageCount = preview.evaluatedPageCount;
            console.log("Workbook page count: " + workbookPageCount);

            // Worksheet printing preview for first worksheet
            const preview2 = new SheetPrintingPreview(workbook.worksheets.get(0), imgOptions);
            const worksheetPageCount = preview2.evaluatedPageCount;
            console.log("Worksheet page count: " + worksheetPageCount);

            document.getElementById('result').innerHTML = `<p>Workbook page count: ${workbookPageCount}</p><p>Worksheet page count: ${worksheetPageCount}</p>`;
        });
    </script>
</html>

The following is the output generated by executing the above code.

Console Output

  
Workbook page count: 1  
Worksheet page count: 1  

Advance topics