Read and change PDF page size in C++
Contents
[
Hide
]
Read PDF page dimensions, compare rectangles with and without rotation, and set a page size using Aspose.PDF for C++.
Read the first page size
- Open the input PDF as a Document.
- Select page 1 and call GetPageRect() with true.
- Print the Rectangle width and height.
#include <iostream>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Rectangle.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
using namespace System;
using namespace Aspose::Pdf;
// Port of ChangeSizeExamples.getPageSize.
void GetPageSize(System::String inputFile)
{
auto document = MakeObject<Document>(inputFile);
auto rectangle = document->get_Pages()->idx_get(1)->GetPageRect(true);
std::cout << rectangle->get_Width() << " : " << rectangle->get_Height() << std::endl;
}
Compare page size with rotation
The rotation change is made in memory only; this function does not save a PDF.
- Open the input PDF as a Document.
- Set page 1 rotation to on90 using Rotation.
- Call GetPageRect() with false and print the dimensions.
- Call GetPageRect() with true and print the dimensions.
#include <iostream>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Rectangle.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Rotation.h>
using namespace System;
using namespace Aspose::Pdf;
// Port of ChangeSizeExamples.getPageSizeRotation.
void GetPageSizeRotation(System::String inputFile)
{
auto document = MakeObject<Document>(inputFile);
auto page = document->get_Pages()->idx_get(1);
page->set_Rotate(Rotation::on90);
auto rectangle = page->GetPageRect(false);
std::cout << rectangle->get_Width() << " : " << rectangle->get_Height() << std::endl;
rectangle = page->GetPageRect(true);
std::cout << rectangle->get_Width() << " : " << rectangle->get_Height() << std::endl;
}
Set the first page size
The dimensions are in PDF points. This changes the page geometry; it does not explicitly scale the existing page content.
- Open the input PDF as a Document.
- Select the first Page and print its page-box dimensions.
- Call SetPageSize() with width 597.6 and height 842.4.
- Print the resulting page-box dimensions.
- Save the result with Save().
#include <iostream>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Rectangle.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
using namespace System;
using namespace Aspose::Pdf;
// Port of ChangeSizeExamples.setPageSize.
void SetPageSize(System::String inputFile, System::String outputFile)
{
auto document = MakeObject<Document>(inputFile);
auto page = document->get_Pages()->idx_get(1);
std::cout << "Before set" << std::endl;
std::cout << "CropBox: " << page->get_CropBox()->get_Width() << " x " << page->get_CropBox()->get_Height() << std::endl;
std::cout << "TrimBox: " << page->get_TrimBox()->get_Width() << " x " << page->get_TrimBox()->get_Height() << std::endl;
std::cout << "ArtBox: " << page->get_ArtBox()->get_Width() << " x " << page->get_ArtBox()->get_Height() << std::endl;
std::cout << "BleedBox: " << page->get_BleedBox()->get_Width() << " x " << page->get_BleedBox()->get_Height() << std::endl;
std::cout << "MediaBox: " << page->get_MediaBox()->get_Width() << " x " << page->get_MediaBox()->get_Height() << std::endl;
page->SetPageSize(597.6, 842.4);
std::cout << "After set" << std::endl;
std::cout << "CropBox: " << page->get_CropBox()->get_Width() << " x " << page->get_CropBox()->get_Height() << std::endl;
std::cout << "TrimBox: " << page->get_TrimBox()->get_Width() << " x " << page->get_TrimBox()->get_Height() << std::endl;
std::cout << "ArtBox: " << page->get_ArtBox()->get_Width() << " x " << page->get_ArtBox()->get_Height() << std::endl;
std::cout << "BleedBox: " << page->get_BleedBox()->get_Width() << " x " << page->get_BleedBox()->get_Height() << std::endl;
std::cout << "MediaBox: " << page->get_MediaBox()->get_Width() << " x " << page->get_MediaBox()->get_Height() << std::endl;
document->Save(outputFile);
}