Inspect PDF page properties in C++
Contents
[
Hide
]
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.
- Open the input PDF as a Document.
- Select the first Page.
- Read ArtBox, BleedBox, CropBox, MediaBox, TrimBox, and Rect.
- Print each Rectangle size and coordinates.
- 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
- Open the input PDF as a Document.
- Read Count from PageCollection.
- 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.
- Create a Document and add a page.
- Add 300 TextFragment paragraphs.
- Call ProcessParagraphs() to lay out the generated content.
- 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
- Open the input PDF as a Document.
- Iterate through every Page.
- Read ColorType and compare it with the ColorType enumeration.
- 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;
}
}