Let us explore the scenarios related to rotation in PDF documents. There might be situations where you might have an inverted scanned PDF document that you need to fix. You can fix such a document by rotating the pages of the document. You might also want to add text or image watermarks to a PDF document by rotating them. To that end, this article will teach you how to rotate PDF pages, text, and images programmatically using C++.
- C++ API for Rotating PDF Pages, Text, and Images
- Rotate PDF Pages using C++
- Rotating Text on PDF Pages using C++
- Rotate an Image on a PDF Page using C++
C++ API for Rotating PDF Pages, Text, and Images
Aspose.PDF for C++ is a C++ library that allows you to create, read and update PDF documents. In addition to this, the API supports rotating PDF pages, text, and images. You can either install the API through NuGet or download it directly from the downloads section.
PM> Install-Package Aspose.PDF.Cpp
Rotate PDF Pages using C++
Rotating pages with Aspose.PDF for C++ is a breeze. With just a few lines of code, you can rotate all the pages of the PDF document. The following are the steps to rotate PDF pages using C++.
- Load the source PDF document using the Document class.
- Loop through the pages of the document. In case you want to rotate only selected pages, you can define that logic within this loop.
- Rotate each page using the Page->set_Rotate(Rotation value) method.
- Save the output PDF file using the Document->Save(System::String outputFileName, SaveFormat format) method.
The following sample code shows how to rotate PDF pages using C++.
Rotating Text on PDF Pages using C++
While adding text to PDF documents, you can change many text properties, including its angle. In the following example, we will add text to a PDF page and rotate it at an angle of 45 degrees. The following are the steps to achieve that.
- Create an instance of the Document class to represent a new PDF document.
- Add a new page to the PDF document.
- Create an instance of the TextFragment class with the text you want to add.
- Set the position, angle, and other properties of the text.
- Add text to the PDF page using the TextBuilder->AppendText(System::SharedPtr<TextFragment> textFragment) method.
- Save the output PDF file using the Document->Save(System::String outputFileName, SaveFormat format) method.
The following sample code demonstrates how to rotate text on a PDF page using C++.
Rotate an Image on a PDF Page using C++
While adding images to PDF documents, you can set many properties like height, width, opacity, rotation, etc. In this example, we will add an image to a PDF page and rotate it at an angle of 90 degrees. The following are the steps to rotate an image on a PDF page.
- Load the source PDF document using the Document class.
- Create an instance of the ImageStamp class with the image you want to add.
- Set rotation and other properties of the image.
- Add the image to the PDF page using Document->get_Pages()->idx_get(1)->AddStamp(System::SharedPtr<Stamp> stamp) method.
- Save the output PDF file using the Document->Save(System::String outputFileName, SaveFormat format) method.
The following sample code shows how to rotate an image on a PDF page using C++.
Get a Free License
You can try the API without evaluation limitations by requesting a free temporary license.
Conclusion
In this article, you have learned how to rotate pages, text, and images in PDF documents using C++. Aspose.PDF for C++ is a vast API that provides many additional features for working with PDF files. You can explore the API in detail by visiting the official documentation. In case of any questions, please feel free to reach us on our free support forum.