Rotate PDF via C++

Change PDF page orientation. Use Aspose.PDF for C++ to modify PDF documents programmatically

Rotate PDF Pages with C++

Do you need to rotate PDF? Programmatic modification of PDF documents is an essential part of modern digital workflows. With C++ libraries like Aspose.PDF – are stand-alone solutions that don’t rely on other software and are ready for commercial use. They cover all possible needs of professional C++ developers.

This C++ library enables developers to rotate PDF documents quickly and efficiently through programming. This library shows you how easily to update or change the page orientation of pages in an existing PDF file programmatically with C++. It is a standalone solution for processing PDF documents in C++ that does not require the installation of any third-party software.

In order to to rotate PDF file, we’ll use Aspose.PDF for C++ API which is a feature-rich, powerful and easy-to-use document manipulation API for CPP platform. Open NuGet package manager, search for Aspose.PDF and install. You may also use the following command from the Package Manager Console.

Package Manager Console

PM > Install-Package Aspose.PDF.Cpp

How to Rotate PDF documents


You need Aspose.PDF for C++ to try the code in your environment.

  1. Load the PDF with an instance of Document.
  2. Move page upper in order to compensate changing page size.
  3. Set old and new page height.
  4. Setting the page rotation angle.
  5. Save the output PDF file.

If you want to rotate PDF files for your tasks, this solution can help. With this powerful Aspose.PDF C++ library, you can update or change the page orientation of pages while keeping its quality. This professional tool uses C++ to rotate PDF effectively. The following example demonstrates how to rotate PDF document in C++:

Rotate PDF - C++

This sample code shows how to Rotate PDF Pages - C++

Input file:

File not added

Output format:

PDF

Output file:


void ChangePageOrientation() {

    String _dataDir("C:\\Samples\\");
    String inputFileName("ChangeOrientation.pdf");
    String outputFileName("ChangeOrientation_out.pdf");
    // Open document
    auto document = MakeObject<Document>(_dataDir + inputFileName);

    for (auto page : document->get_Pages())
    {

        auto r = page->get_MediaBox();
        double newHeight = r->get_Width();
        double newWidth = r->get_Height();
        double newLLX = r->get_LLX();

        //  We must to move page upper in order to compensate changing page size
        // (lower edge of the page is 0,0 and information is usually placed from the
        //  Top of the page. That's why we move lover edge upper on difference between
        //  Old and new height.

        double newLLY = r->get_LLY() + (r->get_Height() - newHeight);
        page->set_MediaBox(MakeObject<Rectangle>(newLLX, newLLY, newLLX + newWidth, newLLY + newHeight));
        // Sometimes we also need to set CropBox (if it was set in original file)
        page->set_CropBox(MakeObject<Rectangle>(newLLX, newLLY, newLLX + newWidth, newLLY + newHeight));

        // Setting Rotation angle of page
        page->set_Rotate(Rotation::on90);
    }

    // Save output file
    document->Save(_dataDir + outputFileName);
}

About Aspose.PDF for C++ API

Aspose.PDF for C++ is a powerful processing library that enables developers to create, read and manipulate PDF documents without using Adobe Acrobat. It provides a wide range of features such as creating forms, adding/compressing text, manipulating PDF pages, adding annotations, handling custom fonts and much more.

Aspose.PDF for C++ is a library that enables the developers to add PDF processing capabilities to their applications. API can be used to build any type of 32-bit and 64-bit applications to generate or read, convert and manipulate PDF files without the use of Adobe Acrobat.

You can find detailed explanation & examples for every class & method in Aspose.PDF for C++ library in the API reference. And also recommend to view a Documentation.