Aspose.PDF for .NET lets you read and set properties of pages in a PDF file in your .NET applications. This section shows how to get the number of pages in a PDF file, get information about PDF page properties such as color and set page properties. The examples given are in C# but you can use any .NET language such as VB.NET to achieve the same.
Get Number of Pages in a PDF File
When working with documents, you often want to know how many pages they contain. With Aspose.PDF this takes no more than two lines of code.
To get the number of pages in a PDF file:
- Open the PDF file using the
- Then use the
Countproperty (from the
Documentobject) to get the total number of pages in the document.
The following code snippet shows how to get the number of pages of a PDF file.
Get page count without saving the document
Sometimes we generate the PDF files on the fly and during PDF file creation, we may come across the requirement (creating Table Of Contents etc.) to get page count of PDF file without saving the file over system or stream. So in order to cater to this requirement, a method ProcessParagraphs(...) has been introduced in Document class. Please take a look over the following code snippet which shows the steps to get page count without saving the document.
Get Page Properties
Each page in a PDF file has a number of properties, such as the width, height, bleed-, crop- and trimbox. Aspose.PDF allows you to access these properties.
Understanding Page Properties: the Difference between Artbox, BleedBox, CropBox, MediaBox, TrimBox and Rect property
- Media box: The media box is the largest page box. It corresponds to the page size (for example A4, A5, US Letter, etc.) selected when the document was printed to PostScript or PDF. In other words, the media box determines the physical size of the media on which the PDF document is displayed or printed.
- Bleed box: If the document has bleed, the PDF will also have a bleed box. Bleed is the amount of color (or artwork) that extends beyond the edge of a page. It is used to make sure that when the document is printed and cut to size (“trimmed”), the ink will go all the way to the edge of the page. Even if the page is mistrimmed - cut slightly off the trim marks - no white edges will appear on the page.
- Trim box: The trim box indicates the final size of a document after printing and trimming.
- Art box: The art box is the box drawn around the actual contents of the pages in your documents. This page box is used when importing PDF documents in other applications.
- Crop box: The crop box is the “page” size at which your PDF document is displayed in Adobe Acrobat. In normal view, only the contents of the crop box are displayed in Adobe Acrobat.
For detailed descriptions of these properties, read the Adobe.Pdf specification, particularly 10.10.1 Page Boundaries.
- Page.Rect: the intersection (commonly visible rectangle) of the
DropBox. The picture below illustrates these properties.
For further details, please visit this page.
Accessing Page Properties
From there, it is possible to access either individual
Page objects using their index, or loop through the collection, using a foreach loop, to get all pages. Once an individual page is accessed, we can get its properties. The following code snippet shows how to get page properties.
Get a Particular Page of the PDF File
Aspose.PDF allows you to split a PDF into individual pages and save them as PDF files. Getting a specified page in a PDF file and saving it as a new PDF is a very similar operation: open the source document, access the page, create a new document and add the page to this.
- Specify the page index using the
- Create a new
- Add the
Pageobject to the new
- Save the output using
The following code snippet shows how to get a particular page from a PDF file and save it as a new file.
Determine Page Color
Page class provides the properties related to a particular page in a PDF document, including what type of colour - RGB, black and white, grayscale or undefined - the page uses.
All the pages of the PDF files are contained by the
PageCollection collection. The
ColorType property specifies the color of elements on page. To get or determine the color information for particular PDF page, use the
ColorType property. The following code snippet shows how to iterate through individual page of PDF file to get color information.