Get and Set Page Properties

Aspose.PDF for Rust lets you read and set properties of pages in a PDF file. 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.

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.

Aspose.PDF for Rust via C++ allows you to count Pages with page_count function.

The next code snippet is designed to open a PDF document, retrieve its page count, and then print the result.

The page_count method is called to get the total number of pages in the PDF document. This is useful for tasks that need to know the length of the document, such as when extracting specific pages or performing operations across all pages. This method is a straightforward way to query the document’s structure.

To get the number of pages in a PDF file:


  use asposepdf::Document;

  fn main() -> Result<(), Box<dyn std::error::Error>> {
      // Open a PDF-document from file
      let pdf = Document::open("sample.pdf")?;

      // Return page count in PDF-document
      let count = pdf.page_count()?;

      // Print the page count
      println!("Count: {}", count);

      Ok(())
  }

Set Page Size

In this example the method pdf.PageSetSize() changes the size of the first page of the PDF document. The PageSizeA1 constant ensures that the first page is set to the A1 paper size. This is useful when converting documents to a standardized format or ensuring that specific content fits correctly on pages.

  1. Opening the PDF Document with open method.
  2. Setting the Page Size with page_set_size function.
  3. Saving the Document using save_as method.

    use asposepdf::{Document, PageSize};

    fn main() -> Result<(), Box<dyn std::error::Error>> {
        // Open a PDF-document with filename
        let pdf = Document::open("sample.pdf")?;

        // Set the size of a page in the PDF-document
        pdf.page_set_size(1, PageSize::A1)?;

        // Save the previously opened PDF-document with new filename
        pdf.save_as("sample_page1_set_size_A1.pdf")?;

        Ok(())
    }