How to Convert Word Document to Images using C++

This topic shows, how to convert Word Document to Images using C++ in a very simple manner. You can convert specific or all pages of a Word document to any of image formats including Tiff, PNG, JPEG and BMP in C++.

Steps to Convert Word Document to Images using C++

  1. Use Aspose.Words.Cpp NuGet package
  2. Include reference to Aspose::Words and Saving namespaces
  3. Create an instance of Document Class to load the source Word file
  4. Set the SaveFormat to PNG
  5. Set the Page Ranges for saving as image and set Callback
  6. Save the Word File to Image in C++ by using Save method

In the following example, we have shown how to add DOCX comments in C++ using few API calls with no dependence on Microsoft Word or Office Interop API.

Code to Convert Word Document to Images using C++

#pragma once
#include <Aspose.Words.Cpp/Document.h>
#include <Aspose.Words.Cpp/DocumentBuilder.h>
#include <Aspose.Words.Cpp/Saving/DocSaveOptions.h>
#include <Aspose.Words.Cpp/Saving/SaveOutputParameters.h>
#include <system/io/file.h>
#include <Aspose.Words.Cpp/License.h>
#include <Aspose.Words.Cpp/Saving/ImageSaveOptions.h>
#include <Aspose.Words.Cpp/Saving/PageRange.h>
#include <Aspose.Words.Cpp/Saving/IPageSavingCallback.h>
#include <Aspose.Words.Cpp/Saving/ImageBinarizationMethod.h>
#include <Aspose.Words.Cpp/Saving/ImageColorMode.h>
#include <Aspose.Words.Cpp/Saving/ImagePixelFormat.h>
#include <Aspose.Words.Cpp/Saving/ImageSaveOptions.h>
#include <Aspose.Words.Cpp/Saving/PageRange.h>
#include <Aspose.Words.Cpp/Saving/PageSavingArgs.h>
#include <Aspose.Words.Cpp/Saving/PageSet.h>
using System::ArrayPtr;
using System::MakeArray;
using System::MakeObject;
using System::SharedPtr;
using System::String;
using namespace Aspose::Words;
using namespace Aspose::Words::Saving;
class WorkingWithDocSaveOptions
{
private:
class Word_Pages_To_Images : public IPageSavingCallback
{
public:
void PageSaving(SharedPtr<PageSavingArgs> args) override
{
args->set_PageFileName(String::Format(u"Page_{0}.png", args->get_PageIndex()));
}
};
public:
void WordToImageConversion()
{
// Setting license file name and path
System::String testLicenseFileName = u"Aspose.Total.NET.lic";
// Setting the Aspose.Words license initially
SharedPtr<License> wordsLicense = System::MakeObject<License>();
// Applying product license
wordsLicense->SetLicense(testLicenseFileName);
// Create an instance of Document class of Aspose.Words for C++
// to load the Word file for conveting to images
SharedPtr<Document> WordDocumentToImageUsingCPP = MakeObject<Document>(u"WordToImage.docx");
//Set ImageSaveOptions to convert document pages to image
SharedPtr<ImageSaveOptions> wordpagestoimage = MakeObject<ImageSaveOptions>(SaveFormat::Png);
// Set page ranges to convert all word pages to image
SharedPtr<PageRange> pagerange = MakeObject<PageRange>(0, WordDocumentToImageUsingCPP->get_PageCount() - 1);
wordpagestoimage->set_PageSet(MakeObject<PageSet>(1));
wordpagestoimage->set_PageSavingCallback(MakeObject<Word_Pages_To_Images>());
// Save Word document to PNG image in C++ using Save method
WordDocumentToImageUsingCPP->Save(u"output.png", wordpagestoimage);
}
};

In previous example, we saw How to Convert HTML to PDF using C++. In this topic, we have shown how to export Word Document to images in C++. You have seen, how easy it is to render document to PNG in C++ using few lines of code.

 English