Working with Page Sets

Working with Page Sets

This guide shows how to describe a subset of a document’s pages – an arbitrary selection of page indices, rather than a single contiguous range – using PageSet in Aspose.Words FOSS for .NET. PageSet is a sealed class in the Aspose.Words.Saving namespace that describes a random set of pages.


Constructing a PageSet

PageSet has three constructors: PageSet(page) builds a one-page set from a single page index; PageSet(pages) builds a set from a collection of exact page indices; and PageSet(ranges) builds a set from a collection of page ranges. Pick the constructor that matches how the page selection is expressed in your scenario – a single page, a scattered list of pages, or contiguous spans.


Built-In Sets: All, Even, and Odd

Three static properties provide ready-made sets without constructing one manually: PageSet.All returns a set with every page of the document in its original order, PageSet.Even returns a set with only the even-numbered pages, and PageSet.Odd returns a set with only the odd-numbered pages.


Enumerating a Page Set

PageSet implements IEnumerable<int>, so any PageSet instance – whether constructed directly or obtained from All, Even, or Odd – can be iterated with GetEnumerator() (or a foreach loop) to walk the page indices it contains.


Tips and Best Practices

  • Use PageSet.All, Even, or Odd for the common cases instead of constructing an explicit index list, unless the selection doesn’t match one of those three patterns.
  • Choose the PageSet(pages) constructor for a scattered, non-contiguous page selection, and PageSet(ranges) when the selection is naturally expressed as one or more contiguous spans.
  • Because PageSet implements IEnumerable<int>, it can be consumed anywhere an IEnumerable<int> of page indices is expected, without any extra conversion step.

Common Issues

IssueCauseFix
A PageSet built from PageSet.Even or Odd appears empty when enumerated on a short documentThe document has fewer even (or odd) pages than expectedConfirm the document’s actual page count before relying on Even/Odd
Unsure which constructor to use for a mix of individual pages and rangesPageSet(pages) and PageSet(ranges) each accept only one shape of inputBuild separate PageSet instances for the indices and the ranges, or normalize the selection to a single shape before constructing it

FAQ

How do I get a PageSet containing every page of a document?

Use the static PageSet.All property.

How do I select only even or odd pages?

Use PageSet.Even or PageSet.Odd.

How do I build a PageSet from an arbitrary list of page numbers?

Use the PageSet(pages) constructor with a collection of exact page indices.

Can I iterate over the pages a PageSet contains?

Yes – PageSet implements IEnumerable<int>, so GetEnumerator() or a foreach loop walks the page indices it describes.


API Reference Summary

ClassDescription
PageSetDescribes a set of pages; constructors for a single page, exact indices, or ranges; All, Even, Odd, GetEnumerator()

See Also