C++: Convert PDF to Images

The PDF file format is ideal for most occasions. But still, you may encounter situations where you need to convert PDF to images. Once you convert a certain PDF page into an image, you can post it on social media, upload or transfer it in devices that can only display images, or embed it in your Word document or PowerPoint presentation. In this article, you will learn how to programmatically convert PDF to images in C++ using Spire.PDF for C++.

Install Spire.PDF for C++

There are two ways to integrate Spire.PDF for C++ into your application. One way is to install it through NuGet, and the other way is to download the package from our website and copy the libraries into your program. Installation via NuGet is simpler and more recommended. You can find more details by visiting the following link.

Integrate Spire.PDF for C++ in a C++ Application

Convert a Specific Page to an Image in C++

Spire.PDF for C++ offers the PdfDocument->SaveAsImage(int pageIndex) method to convert a particular page into image stream. The stream can be then saved as an image file with the desired extension like PNG, JPG, and BMP. The following are the detailed steps.

  • Create a PdfDocument instance.
  • Load a PDF file using PdfDocument->LoadFromFile() method.
  • Convert a specific page into image stream using PdfDocument->SaveAsImage() method.
  • Save the image steam as a JPG file using Stream->Save() method.
  • C++
#include "Spire.Pdf.o.h";

using namespace Spire::Pdf;
using namespace std;

int main() {

	//Specify input and output file paths
	wstring inputFile = L"C:\\Users\\Administrator\\Desktop\\sample.pdf";
	wstring outputFile = L"C:\\Users\\Administrator\\Desktop\\Output\\ToImage";

	//Create a PdfDocument object
	PdfDocument* doc = new PdfDocument();

	//Load a PDF file
	doc->LoadFromFile(inputFile.c_str());

	//Convert a specific page as image
	Stream* image = doc->SaveAsImage(0, PdfImageType::Bitmap);

	//Write image to a .jpg file
	wstring fileName = outputFile + L".jpg";
	image->Save(fileName.c_str());

	doc->Close();
	delete doc;
}

C++: Convert PDF to Images

Convert an Entire PDF to Multiple Images in C++

In order to save the whole PDF as separate individual images, you just need to put the conversion part inside a loop statement. The follows are the detailed steps.

  • Create a PdfDocument instance.
  • Load a PDF file using PdfDocument->LoadFromFile() method.
  • Loop through the pages in the document and convert each of them into image stream using PdfDocument->SaveAsImage(int pageIndex) method.
  • Save the image steam as JPG files using Stream->Save() method.
  • C++
#include "Spire.Pdf.o.h";

using namespace Spire::Pdf;
using namespace std;

int main() {

	//Specify input and output file paths
	wstring inputFile = L"C:\\Users\\Administrator\\Desktop\\sample.pdf";
	wstring outputFile = L"C:\\Users\\Administrator\\Desktop\\Output\\ToImg-";

	//Create a PdfDocument object
	PdfDocument* doc = new PdfDocument();

	//Load a PDF file
	doc->LoadFromFile(inputFile.c_str());

	//Iterate through the pages in the document
	for (int i = 0; i < doc->GetPages()->GetCount(); i++) {

		//Save a specific page as image
		Stream* image = doc->SaveAsImage(i);

		//Write image to a .jpg file
		wstring fileName = outputFile + to_wstring(i) + L".jpg";
		image->Save(fileName.c_str());
	}
	doc->Close();
	delete doc;
}

C++: Convert PDF to Images

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.