Render Sequence of Pages using PageIndex and PageCount Properties of ImageOrPrintOptions with C++

Possible Usage Scenarios

You can render a sequence of pages of your Excel file to images by using Aspose.Cells with ImageOrPrintOptions.GetPageIndex() and ImageOrPrintOptions.GetPageCount() properties. These properties are useful when there are so many e.g. thousands of pages in your worksheet but you want to render some of them only. This will not only save the processing time but will also save the memory consumption of the rendering process.

Render Sequence of Pages using PageIndex and PageCount Properties of ImageOrPrintOptions

The following sample code loads the sample Excel file and renders only pages 4, 5, 6 and 7 using the ImageOrPrintOptions.GetPageIndex() and ImageOrPrintOptions.GetPageCount() properties. Here are the rendered pages generated by the code.

todo:image_alt_text todo:image_alt_text
todo:image_alt_text todo:image_alt_text

Sample Code

#include <iostream>
#include <string>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
using namespace Aspose::Cells::Drawing;

int main()
{
    Aspose::Cells::Startup();

    U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
    U16String outDir(u"..\\Data\\02_OutputDirectory\\");

    Workbook wb(srcDir + u"sampleImageOrPrintOptions_PageIndexPageCount.xlsx");

    Worksheet ws = wb.GetWorksheets().Get(0);

    ImageOrPrintOptions opts;
    opts.SetPageIndex(3);
    opts.SetPageCount(4);
    opts.SetImageType(ImageType::Png);

    SheetRender sr(ws, opts);

    for (int i = opts.GetPageIndex(); i < sr.GetPageCount(); i++)
    {
        std::wstring pageNum = std::to_wstring(i + 1);
        U16String filePath = outDir + U16String(u"outputImage-") + 
            U16String(reinterpret_cast<const char16_t*>(pageNum.c_str())) + 
            U16String(u".png");
        sr.ToImage(i, filePath);
    }

    std::cout << "Images generated successfully!" << std::endl;

    Aspose::Cells::Cleanup();
}