Converting a Word document to images can have several benefits. Firstly, it enables you to preserve the original formatting and layout of the document, which is especially useful when sharing the document on different devices or systems. Secondly, it prevents unauthorized changes to the content, which ensures the document's integrity. Lastly, converting a Word document to images makes the content accessible for people who do not have access to Microsoft Word. In this article, we will explain how to convert Word documents to images in C++ using Spire.Doc for C++.
- Convert a Word Document to JPEG, PNG or BMP Images in C++
- Convert a Specific Page of a Word Document to IPEG, PNG or BMP Images in C++
- Convert a Word Document to SVG Images in C++
Install Spire.Doc for C++
There are two ways to integrate Spire.Doc 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.Doc for C++ in a C++ Application
Convert a Word Document to JPEG, PNG or BMP Images in C++
Spire.Doc for C++ offers the Document->SaveImageToStreams(ImageType type) method which enables you to convert a Word document to a vector of image streams. Then you can iterate through the vector of image streams, and save each image stream to an image file such as a JPEG file, a PNG file, or a BMP file. The detailed steps are as follows:
- Initialize an instance of the Document class.
- Load a Word document using the Document->LoadFromFile(LPCWSTR_S fileName) method.
- Convert the Word document to a vector of image streams using the Document->SaveImageToStreams(ImageType type) method.
- Iterate through the vector of image streams.
- Convert each image stream to an Image object using the Image::FromStream(intrusive_ptr<Stream> stream) method.
- Save the Image object to a JPEG, PNG, BMP, or another type of image file using the Image->Save(LPCWSTR_S filename, intrusive_ptr<ImageFormat> format) method.
- C++
#include "Spire.Doc.o.h"; using namespace Spire::Doc; using namespace std; int main() { //Initialize an instance of the Document class intrusive_ptr<Document> document = new Document(); //Load a Word document document->LoadFromFile(L"Input.docx"); //Convert the Word document to image streams and store the result in a vector std::vector<intrusive_ptr<Stream>> images = document->SaveImageToStreams(ImageType::Bitmap); //Iterate through the vector of image streams for (int i = 0; i < images.size(); i++) { //Save each image stream to a PNG image std::vector<byte> imgBytes = images[i]->ToArray(); std::ofstream outFile(L"Document\\" + to_wstring(i) + L".png", std::ios::binary); if (outFile.is_open()) { outFile.write(reinterpret_cast<const char*>(imgBytes.data()), imgBytes.size()); outFile.close(); } } document->Close(); }
Convert a Specific Page of a Word Document to JPEG, PNG or BMP Images in C++
In addition to converting a whole Word document to images, Spire.Doc for C++ also enables you to convert a specific page of a Word document to an image by using the Document->SaveImageToStreams(int pageIndex, ImageType type) method. The detailed steps are as follows:
- Initialize an instance of the Document class.
- Load a Word document using the Document->LoadFromFile(LPCWSTR_S fileName) method.
- Convert a specific page of the Word document to an image stream using the Document->SaveImageToStreams(int pageIndex, ImageType type) method.
- Convert the image stream to an Image object using the Image::FromStream(intrusive_ptr<Stream> stream) method.
- Save the Image object to a JPEG, PNG, BMP, or another type of image file using the Image->Save(LPCWSTR_S filename, intrusive_ptr<ImageFormat> format) method.
- C++
#include "Spire.Doc.o.h"; using namespace Spire::Doc; using namespace std; int main() { //Initialize an instance of the Document class intrusive_ptr<Document> document = new Document(); //Load a Word document document->LoadFromFile(L"Input.docx"); //Convert the first page of the Word document to a image stream intrusive_ptr<Stream> imageStream = document->SaveImageToStreams(0, ImageType::Bitmap); //Obtain image data in the default format of png, you can use it to convert other image format std::vector<byte> imgBytes = imageStream->ToArray(); std::ofstream outFile(L"Page\\ToImage.png", std::ios::binary); if (outFile.is_open()) { outFile.write(reinterpret_cast<const char*>(imgBytes.data()), imgBytes.size()); outFile.close(); } document->Close(); imageStream->Dispose(); }
Convert a Word Document to SVG Images in C++
You can convert a Word document to SVG images easily by using the Document->SaveToFile(LPCWSTR_S fileName, FileFormat fileFormat) method. The detailed steps are as follows:
- Initialize an instance of the Document class.
- Load a Word document using the Document->LoadFromFile(LPCWSTR_S fileName) method.
- Save the document to SVG images using the Document->SaveToFile(LPCWSTR_S fileName, FileFormat fileFormat) method.
- C++
#include "Spire.Doc.o.h"; using namespace Spire::Doc; using namespace std; int main() { //Initialize an instance of the Document class intrusive_ptr<Document> document = new Document(); //Load a Word document document->LoadFromFile(L"Input.docx"); wstring fileName = L"SVG\\ToSvg.svg"; //Save the document to SVG images document->SaveToFile(fileName.c_str(), FileFormat::SVG); document->Close(); }
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.