PDF is a popular format for sharing and printing documents due to its consistent layout. However, you might find yourself in situations where you want to generate a cover image of the PDF file or embed its pages on a web page. In such cases, converting the PDF file to image format will prove to be helpful. To that end, this article will teach you how to convert PDF pages to PNG images using C++.
- C++ API for Converting PDF Pages to PNG Images
- Convert PDF Pages to PNG Images using C++
- Convert a Single PDF Page to a PNG Image using C++
C++ API for Converting PDF Pages to PNG Images
Aspose.PDF for C++ is a C++ library that allows you to create, read and update PDF documents. Furthermore, the API supports converting PDF pages to PNG images. You can either install the API through NuGet or download it directly from the downloads section.
PM> Install-Package Aspose.PDF.Cpp
Convert PDF Pages to PNG Images using C++
The following are the steps to convert PDF pages to PNG images.
- Load the PDF file using the Document class.
- Iterate through the pages of the PDF file.
- Within the loop, create an instance of the FileStream class for the output image.
- Create an instance of the Resolution class.
- Create an instance of the PngDevice class.
- Using the Process (System::SharedPtr<Page> page, System::SharedPtr<System::IO::Stream> output) method of the PngDevice class, save the image of the PDF page.
The following sample code demonstrates how to save the pages of a PDF file as PNG images using C++.
Convert a Single PDF Page to a PNG Image using C++
The following are the steps to convert a single page of a PDF file to a PNG image.
- Load the PDF file using the Document class.
- Retrieve the page that you want to convert using the Document->get_Pages()->idx_get(int32_t index) method.
- Create an instance of the FileStream class for the output image.
- Instantiate an object of the Resolution class.
- Create an instance of the PngDevice class.
- Using the Process (System::SharedPtr<Page> page, System::SharedPtr<System::IO::Stream> output) method of the PngDevice class, save the image of the PDF page.
The following sample code shows how to convert a single PDF page to a PNG image 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 convert the pages of a PDF file to PNG images using C++. You can convert all the pages or selected pages of PDF files to PNG images. Aspose.PDF for C++ is a robust API with many additional features for automating your PDF-related workflows. You can explore the API in detail by visiting the official documentation. In case of any questions, please feel free to reach us at our free support forum.