Inspect PDF page properties in C++

Inspect page counts, page boxes, rotation, and color types, and calculate a generated document’s page count before saving with Aspose.PDF for C++.

Inspect page boxes and rotation

The example uses C++17 structured bindings. For changes to page properties, see the page-size, cropping, and rotation guides in this section.

  1. Open the input PDF as a Document.
  2. Select the first Page.
  3. Read ArtBox, BleedBox, CropBox, MediaBox, TrimBox, and Rect.
  4. Print each Rectangle size and coordinates.
  5. Print the page number and rotation.
#include <iostream>
#include <utility>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Rectangle.h>

using namespace System;
using namespace Aspose::Pdf;

void GetPageProperties(System::String inputFile)
{
    auto document = MakeObject<Document>(inputFile);
    auto page = document->get_Pages()->idx_get(1);
    const std::pair<const char*, SharedPtr<Rectangle>> boxes[] = {
        {"ArtBox", page->get_ArtBox()},
        {"BleedBox", page->get_BleedBox()},
        {"CropBox", page->get_CropBox()},
        {"MediaBox", page->get_MediaBox()},
        {"TrimBox", page->get_TrimBox()},
        {"Rect", page->get_Rect()},
    };
    for (const auto& [name, box] : boxes) {
        std::cout << name << " : Height=" << box->get_Height()
        << ",Width=" << box->get_Width()
        << ",LLX=" << box->get_LLX() << ",LLY=" << box->get_LLY()
        << ",URX=" << box->get_URX() << ",URY=" << box->get_URY() << std::endl;
    }
    std::cout << "Page Number : " << page->get_Number() << std::endl;
    std::cout << "Rotate : " << static_cast<int>(page->get_Rotate()) << std::endl;
}

Read the page count

  1. Open the input PDF as a Document.
  2. Read Count from PageCollection.
  3. Print the page count.
#include <iostream>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>

using namespace System;
using namespace Aspose::Pdf;

void GetPageCount(System::String inputFile)
{
    auto document = MakeObject<Document>(inputFile);

    std::cout << "Page Count: " << document->get_Pages()->get_Count() << std::endl;
}

Calculate page count before saving

This creates a document in memory and does not require an input PDF or save an output file.

  1. Create a Document and add a page.
  2. Add 300 TextFragment paragraphs.
  3. Call ProcessParagraphs() to lay out the generated content.
  4. Read and print the PageCollection count.
#include <iostream>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Text/Font.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Generator/Paragraphs.h>
#include <Aspose.PDF.Cpp/Text/TextFragment.h>
#include <Aspose.PDF.Cpp/Text/TextState.h>

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Text;

void GetPageCountWithoutSaving()
{
    auto document = MakeObject<Document>();

    auto page = document->get_Pages()->Add();
    for (int i = 0; i < 300; i++) {
        page->get_Paragraphs()->Add(MakeObject<TextFragment>(u"Pages count test"));
    }
    document->ProcessParagraphs();
    std::cout << "Number of pages in document = " << document->get_Pages()->get_Count() << std::endl;
}

Inspect page color types

  1. Open the input PDF as a Document.
  2. Iterate through every Page.
  3. Read ColorType and compare it with the ColorType enumeration.
  4. Print the classification for each page.
#include <iostream>
#include <Aspose.PDF.Cpp/ColorType.h>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>

using namespace System;
using namespace Aspose::Pdf;

void GetPageColorType(System::String inputFile)
{
    auto document = MakeObject<Document>(inputFile);
    for (int pageNumber = 1; pageNumber <= document->get_Pages()->get_Count(); ++pageNumber) {
        auto colorType = document->get_Pages()->idx_get(pageNumber)->get_ColorType();
        const char* description = "undefined";
        switch (colorType) {
            case ColorType::BlackAndWhite: description = "Black and white"; break;
            case ColorType::Grayscale: description = "Gray Scale"; break;
            case ColorType::Rgb: description = "RGB"; break;
            case ColorType::Undefined: break;
        }
        std::cout << "Page # " << pageNumber << " is " << description << "." << std::endl;
    }
}