Render Sequence of Pages using PageIndex and PageCount Properties of ImageOrPrintOptions
Render Sequence of Pages using PageIndex and PageCount Properties of ImageOrPrintOptions
You can render a sequence of pages of your Excel file to images using Aspose.Cells with ImageOrPrintOptions.PageIndex and ImageOrPrintOptions.PageCount 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.
The following sample code loads the sample Excel file and renders only pages 4, 5, 6, and 7 using the ImageOrPrintOptions.PageIndex and ImageOrPrintOptions.PageCount properties. The following are the images of the rendered pages generated by the sample code.
![]() |
![]() |
---|---|
![]() |
![]() |
Sample Code
source_directory = "Examples/SampleFiles/SourceDirectory/" | |
output_directory = "Examples/SampleFiles/OutputDirectory/" | |
# Load the Sample Workbook | |
workbook = Workbook(source_directory + "sampleImageOrPrintOptions_PageIndexPageCount.xlsx") | |
# Access the first worksheet | |
worksheet = workbook.getWorksheets().get(0) | |
# Specify image or print options | |
# We want to print pages 4, 5, 6, 7 | |
imageOrPrintOptions = ImageOrPrintOptions() | |
imageOrPrintOptions.setPageIndex(3) | |
imageOrPrintOptions.setPageCount(4) | |
imageOrPrintOptions.setImageFormat(ImageFormat.getPng()) | |
# Create sheet render object | |
sheetRender = SheetRender(worksheet, imageOrPrintOptions) | |
# Print all the pages as images | |
i = imageOrPrintOptions.getPageIndex() | |
while i < sheetRender.getPageCount(): | |
sheetRender.toImage(i, output_directory + "outputImage-" + str(i+1) + ".png") | |
i += 1 |