Knowledgebase (2311)
Children categories
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.
PDF documents generated through code are consistent in terms of formatting, layout, and content, ensuring a professional look. Automating the creation of PDF documents reduces the time and effort required to produce them manually. Nowadays, most invoices, receipts and other financial documents are generated programmatically. In this article, you will learn how to create PDF documents from scratch 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
Background Knowledge
A page in Spire.PDF for C++ (represented by PdfPageBase class) consists of client area and margins all around. The content area is for users to write various contents, and the margins are usually blank edges.
As shown in the figure below, the origin of the coordinate system on the page is located at the top left corner of the client area, with the x-axis extending horizontally to the right and the y-axis extending vertically down. All elements added to the client area must be based on the specified coordinates.

In addition, the following table lists the important classes and methods, which can help you easily understand the code snippet provided in the following section.
| Member | Description |
| PdfDocument class | Represents a PDF document model. |
| PdfPageBase class | Represents a page in a PDF document. |
| PdfSolidBrush class | Represents a brush that fills any object with a solid color. |
| PdfTrueTypeFont class | Represents a true type font. |
| PdfStringFormat class | Represents text format information, such as alignment, characters spacing and indent. |
| PdfTextWidget class | Represents the text area with the ability to span several pages. |
| PdfTextLayout class | Represents the text layout information. |
| PdfDocument->GetPages()->Add() method | Adds a page to a PDF document. |
| PdfPageBase->GetCanvas()->DrawString() method | Draws string on a page at the specified location with specified font and brush objects. |
| PdfLayoutWidget->Draw() method | Draws widget on a page at the specified location. |
| PdfDocument->Save() method | Saves the document to a PDF file. |
Create a PDF Document from Scratch in C++
Despite the fact that Spire.PDF for C++ enables users to add various elements to PDF documents, this article demonstrates how to create a simple PDF document with only plain text. The following are the detailed steps.
- Create a PdfDocument object.
- Add a page using PdfDocument->GetPages()->Add() method.
- Create brush and font objects.
- Draw string on the page at a specified coordinate using PdfPageBase->GetCanvas()->DrawString() method.
- Create a PdfTextWidget object to hold a chunk of text.
- Convert the text widget to an object of PdfLayoutWidget class and draw it on the page using PdfLayoutWidget->Draw() method
- Save the document to a PDF file using PdfDocument->Save() method.
- C++
#include "Spire.Pdf.o.h";
using namespace Spire::Pdf;
using namespace std;
wstring readFileIntoWstring(const string& path) {
ifstream input_file(path);
if (!input_file.is_open()) {
cerr << "Could not open the file - '"
<< path << "'" << endl;
exit(EXIT_FAILURE);
}
string s1 = string((std::istreambuf_iterator<char>(input_file)), std::istreambuf_iterator<char>());
wstring ws(s1.begin(), s1.end());
return ws;
}
int main() {
//Create a PdfDocument object
intrusive_ptr<PdfDocument> doc = new PdfDocument();
//Add a page
intrusive_ptr<PdfPageBase> page = doc->GetPages()->Add(PdfPageSize::A4(), new PdfMargins(35));
//Specify title text
wstring titleText = L"What is MySQL";
//Create solid brushes
intrusive_ptr<PdfSolidBrush> titleBrush = new PdfSolidBrush(new PdfRGBColor(Color::GetPurple()));
intrusive_ptr<PdfSolidBrush> paraBrush = new PdfSolidBrush(new PdfRGBColor(Color::GetBlack()));
//Create true type fonts
intrusive_ptr<PdfTrueTypeFont> titleFont = new PdfTrueTypeFont(L"Times New Roman", 18, PdfFontStyle::Bold, true);
intrusive_ptr<PdfTrueTypeFont> paraFont = new PdfTrueTypeFont(L"Times New Roman", 12, PdfFontStyle::Regular, true);
//Set the text alignment via PdfStringFormat class
intrusive_ptr<PdfStringFormat> format = new PdfStringFormat();
format->SetAlignment(PdfTextAlignment::Center);
//Draw title on the page
page->GetCanvas()->DrawString(titleText.c_str(), titleFont, titleBrush, page->GetClientSize()->GetWidth() / 2, 20, format);
//Get paragraph text from a .txt file
wstring paraText = readFileIntoWstring("C:\\Users\\Administrator\\Desktop\\content.txt");
//Create a PdfTextWidget object to hold the paragraph content
intrusive_ptr<PdfTextWidget> widget = new PdfTextWidget(paraText.c_str(), paraFont, paraBrush);
//Create a rectangle where the paragraph content will be placed
intrusive_ptr<RectangleF> rect = new RectangleF(0, 50, (float)page->GetClientSize()->GetWidth(), (float)page->GetClientSize()->GetHeight());
//Set the PdfLayoutType to Paginate to make the content paginated automatically
intrusive_ptr<PdfTextLayout> layout = new PdfTextLayout();
layout->SetLayout(PdfLayoutType::Paginate);
//Draw paragraph text on the page
Object::Convert<PdfLayoutWidget>(widget)->Draw(page, rect, layout);
//Save to file
doc->SaveToFile(L"output/CreatePdfDocument.pdf");
doc->Dispose();
}

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.
Comments are a commonly used feature in situations where multiple people are collaborating on the same document simultaneously. By using comments, collaborators can offer their thoughts or ideas directly in the document without making any changes to the actual text. This allows authors to review and consider the feedback before making any revisions. Additionally, comments provide an efficient way to identify specific issues within the document, as each comment is linked directly to the corresponding text or location in the document. In this article, we will explain how to work with comments in Word, specifically, how to add, reply to and delete comments in Word documents using Spire.Doc for C++.
- Add a Comment to a Paragraph in Word in C++
- Add a Comment to a Specific Text in Word in C++
- Add a Comment with a Picture in Word in C++
- Reply to a Comment in Word in C++
- Delete a Comment from Word 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
Add a Comment to a Paragraph in Word in C++
You can add a comment to a paragraph to provide a suggestion or additional information about the content of the paragraph. The following steps explain how to add a comment to a specific paragraph in a Word document using Spire.Doc for C++:
- Initialize an instance of the Document class.
- Load a Word document using the Document->LoadFromFile(LPCWSTR_S fileName) method.
- Get a specific section of the document using the Document->GetSections()->GetItemInSectionCollection(int index) method.
- Get a specific paragraph of the section using the Section->GetParagraphs()->GetItemInParagraphCollection(int index) method.
- Add a comment to the paragraph using the Paragraph->AppendComment(LPCWSTR_S text) method.
- Set the author and ID for the comment using the Comment->GetFormat()->SetAuthor(LPCWSTR_S value) and Comment->GetFormat()->SetCommentId(int value) methods.
- Save the result document using the Document->SaveToFile(LPCWSTR_S fileName, FileFormat fileFormat) method.
- C++
#include "Spire.Doc.o.h"
using namespace Spire::Doc;
int main()
{
//Initialize an instance of the Document class
intrusive_ptr<Document> document = new Document();
//Load a Word document
document->LoadFromFile(L"Input.docx");
//Get the first section of the document
intrusive_ptr<Section> section = document->GetSections()->GetItemInSectionCollection(0);
//Get the second paragraph of the first section
intrusive_ptr<Paragraph> paragraph = section->GetParagraphs()->GetItemInParagraphCollection(1);
//Add a comment to the paragraph
intrusive_ptr<Spire::Doc::Comment> comment = paragraph->AppendComment(L"This comment is added using Spire.Doc for Cpp.");
//Set the author and ID for the comment
comment->GetFormat()->SetAuthor(L"E-iceblue");
comment->GetFormat()->SetCommentId(0);
comment->GetFormat()->SetInitial(L"CM");
//Save the result document
document->SaveToFile(L"AddCommentToParagraph.docx", FileFormat::Docx2019);
document->Close();
}

Add a Comment to a Specific Text in Word in C++
In addition to adding comments to paragraphs, Spire.Doc for C++ is also capable of adding comments to specific text in Word documents. The following steps explain how to add a comment to a specific text in a Word document using Spire.Doc for C++:
- Initialize an instance of the Document class.
- Load a Word document using the Document->LoadFromFile(LPCWSTR_S fileName) method.
- Find the specific text in the document using the Document->FindString(LPCWSTR_S stringValue, bool caseSensitive, bool wholeWord) method.
- Initialize an instance of the Comment class to create a new comment. Then set the content, author, and ID for the comment.
- Initialize an instance of the CommentMark class and set its type as CommentMarkType::CommentStart to create a comment start mark. Then set the ID of the comment start mark to be the same as the ID of the comment.
- Initialize an instance of the CommentMark class and set its type as CommentMarkType::CommentEnd to create a comment end mark. Then set the ID of the comment end mark to be the same as the ID of the comment.
- Get the owner paragraph of the found text. Then add the comment to the owner paragraph using the Paragraph->GetChildObjects()->Add(intrusive_ptr<IDocumentObject> entity) method.
- Get the index position of the found text in the owner paragraph using the Paragraph->GetChildObjects()->IndexOf(intrusive_ptr<IDocumentObject> entity) method.
- Insert the comment start mark before the index position of the found text using the Paragraph->GetChildObjects()->Insert(int index, intrusive_ptr<IDocumentObject> entity) method.
- Insert the comment end mark after the index position of the found text using the Paragraph->GetChildObjects()->Insert(int index, intrusive_ptr<IDocumentObject> entity) method.
- Save the result document using the Document->SaveToFile(LPCWSTR_S fileName, FileFormat fileFormat) method.
- C++
#include "Spire.Doc.o.h"
using namespace Spire::Doc;
int main()
{
//Initialize an instance of the Document class
intrusive_ptr<Document> document = new Document();
//Load a Word document
document->LoadFromFile(L"Input.docx");
//Find a specific text
intrusive_ptr<TextSelection> find = document->FindString(L"Microsoft Office", false, true);
//Create a comment and set its content, author and ID
intrusive_ptr<Comment> comment = new Comment(document);
comment->GetBody()->AddParagraph()->SetText(L"A software developed by Microsoft in 1988.");
comment->GetFormat()->SetAuthor(L"George");
comment->GetFormat()->SetCommentId(0);
//Create a comment start mark
intrusive_ptr<CommentMark> commentmarkStart = new CommentMark(document);
commentmarkStart->SetType(CommentMarkType::CommentStart);
//Set its ID to be the same as the ID of the comment
commentmarkStart->SetCommentId(0);
//Create a comment end mark
intrusive_ptr<CommentMark> commentmarkEnd = new CommentMark(document);
commentmarkEnd->SetType(CommentMarkType::CommentEnd);
//Set its ID to be the same as the ID of the comment
commentmarkEnd->SetCommentId(0);
//Get the found text as a single text range
intrusive_ptr<TextRange> range = find->GetAsOneRange();
//Get the owner paragraph of the text range
intrusive_ptr<Paragraph> para = range->GetOwnerParagraph();
//Add the comment to the owner paragraph
para->GetChildObjects()->Add(comment);
//Get the index position of the text range in the owner paragraph
int index = para->GetChildObjects()->IndexOf(range);
//Insert the comment start mark before the index position of the text range
para->GetChildObjects()->Insert(index, commentmarkStart);
//Insert the comment end mark after the index position of the text range
para->GetChildObjects()->Insert(index + 2, commentmarkEnd);
//Save the result document
document->SaveToFile(L"AddCommentForSpecificText.docx", FileFormat::Docx2019);
document->Close();
}

Add a Comment with a Picture in Word in C++
Sometimes, you may want to include a picture in your comment to supplement the textual content. The following steps explain how to add a comment with a picture to a Word document using Spire.Doc for C++:
- Initialize an instance of the Document class.
- Load a Word document using the Document->LoadFromFile(LPCWSTR_S fileName) method.
- Get a specific section of the document using the Document->GetSections()->GetItemInSectionCollection(int index) method.
- Get a specific paragraph of the section using the Section->GetParagraphs()->GetItemInParagraphCollection(int index) method.
- Add a comment to the paragraph using the Paragraph->AppendComment(LPCWSTR_S text) method. Then set the author name and ID for the comment.
- Initialize an instance of the DocPicture class.
- Load a picture using the DocPicture->LoadImageSpire(LPCWSTR_S imgFile) method. Then set the width and height for the picture.
- Add a paragraph to the comment. Then add the picture to the paragraph using the Paragraph->GetChildObjects()->Add() method.
- Save the result document using the Document->SaveToFile(LPCWSTR_S fileName, FileFormat fileFormat) method.
- C++
#include "Spire.Doc.o.h"
using namespace Spire::Doc;
int main()
{
//Initialize an instance of the Document class
intrusive_ptr<Document> document = new Document();
//Load a Word document
document->LoadFromFile(L"Input.docx");
//Get the first section of the document
intrusive_ptr<Section> section = document->GetSections()->GetItemInSectionCollection(0);
//Get the first paragraph of the first section
intrusive_ptr<Paragraph> paragraph = section->GetParagraphs()->GetItemInParagraphCollection(0);
//Add a comment to the paragraph
intrusive_ptr<Comment> comment = paragraph->AppendComment(L"Spire.Doc for Cpp");
comment->GetFormat()->SetAuthor(L"E-iceblue");
comment->GetFormat()->SetCommentId(0);
//Load a picture
intrusive_ptr<DocPicture> docPicture = new DocPicture(document);
docPicture->LoadImageSpire(L"Doc-CPP.png");
//Set picture width and height
docPicture->SetWidth(100);
docPicture->SetHeight(100);
//Add the picture to the comment
intrusive_ptr<Paragraph> picParagraph = comment->GetBody()->AddParagraph();
picParagraph->GetChildObjects()->Add(docPicture);
//Save the result document
document->SaveToFile(L"AddCommentWithPicture.docx", FileFormat::Docx2019);
document->Close();
}

Reply to a Comment in Word in C++
When reviewing a comment added by others, you may want to reply to it to provide your insights. The following steps explain how to reply to an existing comment in a Word document using Spire.Doc for C++:
- Initialize an instance of the Document class.
- Load a Word document using the Document->LoadFromFile(LPCWSTR_S fileName) method.
- Get a specific comment in the document using the Document-> GetComments()->GetItem(int index) method.
- Initialize an instance of the Comment class to create a new comment. Then set the content, author, and ID for the comment.
- Add the new comment as a reply to the specific comment using the Comment->ReplyToComment(intrusive_ptr<Comment> replyComment) method.
- Save the result document using the Document->SaveToFile(LPCWSTR_S fileName, FileFormat fileFormat) method.
- C++
#include "Spire.Doc.o.h"
using namespace Spire::Doc;
int main()
{
//Initialize an instance of the Document class
intrusive_ptr<Document> document = new Document();
//Load a Word document
document->LoadFromFile(L"AddCommentToParagraph.docx");
//Get the first comment of the document
intrusive_ptr<Comment> comment = document->GetComments()->GetItem(0);
//Create a comment and set its author, ID and content
intrusive_ptr<Comment> replyComment = new Comment(document);
replyComment->GetFormat()->SetAuthor(L"Michael");
replyComment->GetFormat()->SetCommentId(1);
replyComment->GetBody()->AddParagraph()->AppendText(L"Spire.Doc is a wonderful Word library.");
//Add the new comment as a reply to the first comment
comment->ReplyToComment(replyComment);
//Save the result document
document->SaveToFile(L"ReplyToComment.docx", FileFormat::Docx2019);
document->Close();
}

Delete a Comment from Word in C++
After you have finished revising the file and want to publish a formal version, you may need to remove the comments from the file. The following steps show you how to delete a comment from a Word document using Spire.Doc for C++:
- Initialize an instance of the Document class.
- Load a Word document using the Document->LoadFromFile(LPCWSTR_S fileName) method.
- Delete a specific comment from the document using the Document->GetComments()->RemoveAt(int index) method.
- Save the result document using the Document->SaveToFile(LPCWSTR_S fileName, FileFormat fileFormat) method.
- C++
#include "Spire.Doc.o.h"
using namespace Spire::Doc;
int main()
{
//Initialize an instance of the Document class
intrusive_ptr<Document> document = new Document();
//Load a Word document
document->LoadFromFile(L"AddCommentToParagraph.docx");
//Delete the first comment from the document
document->GetComments()->RemoveAt(0);
//Save the result document
document->SaveToFile(L"DeleteComment.docx", FileFormat::Docx2019);
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.