Knowledgebase (2300)
Images can add visual variety to documents and convey information that is hard to express through text alone, such as complex concepts or emotions. They are a powerful tool to make your document easier to understand, more engaging, and more memorable. Whether you are designing a report or creating a marketing document, inserting images can enhance your communication with your readers and leave a lasting impression on them. In this article, you will learn how to insert images into Word documents in C++ using Spire.Doc for C++.
- Insert an Image in a Word Document in C++
- Insert an Image at a Specified Location in a Word Document 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
Insert an Image in a Word Document in C++
Spire.Doc for C++ offers the Paragraph->AppendPicture() method to insert an image into a Word document. The detailed steps are as follows:
- Initialize an instance of the Document class.
- Add a section using Document->AddSection() method.
- Add two paragraphs to the section using Section->AddParagraph() method.
- Add text to the paragraphs using Paragraph->AppendText() method and set formatting.
- Load an image using Image::FromFile() method.
- Add the image to the first paragraph using Paragraph->AppendPicture() method.
- Set width and height for the image using DocPicture->SetWidth() and DocPicture->SetHeight() methods.
- Set a text wrapping style for the image using DocPicture->SetTextWrappingStyle() method.
- Save the result document using Document->SaveToFile() method.
- C++
#include "Spire.Doc.o.h"
using namespace Spire::Doc;
using namespace Spire::Common;
int main()
{
//Initialize an instance of the Document class
Document* document = new Document();
//Add a section
Section* section = document->AddSection();
//Add the first paragraph
Paragraph* paragraph1 = section->AddParagraph();
//Add text to the paragraph and set formatting
TextRange* tr = paragraph1->AppendText(L"Spire.Doc for C++ is a professional Word library specifically designed for developers to create, read, write, convert, merge, split, and compare Word documents in C++ applications with fast and high-quality performance.");
tr->GetCharacterFormat()->SetFontName(L"Calibri");
tr->GetCharacterFormat()->SetFontSize(11.0f);
paragraph1->GetFormat()->SetLineSpacing(20.0f);
paragraph1->GetFormat()->SetAfterSpacing(10.0f);
//Add the second paragraph
Paragraph* paragraph2 = section->AddParagraph();
//Add text to the paragraph and set formatting
tr = paragraph2->AppendText(L"Almost all Word document elements are supported by Spire.Doc for C++, including pages, sections, headers, footers, digital signatures, footnotes, paragraphs, lists, tables, text, fields, hyperlinks, bookmarks, comments, images, style, background settings, document settings and protection. Furthermore, drawing objects including shapes, textboxes, images, OLE objects, Latex Math Symbols, MathML Code and controls are supported as well.");
tr->GetCharacterFormat()->SetFontName(L"Calibri");
tr->GetCharacterFormat()->SetFontSize(11.0f);
paragraph2->GetFormat()->SetLineSpacing(20.0f);
//Load an image
Spire::Common::Image* image = Image::FromFile(L"Spire.Doc.png");
//Add the image to the first paragraph
DocPicture* picture = paragraph1->AppendPicture(image);
//Set image width and height
picture->SetWidth(100);
picture->SetHeight(100);
//Set text wrapping style for the image
picture->SetTextWrappingStyle(TextWrappingStyle::Tight);
//Save the result document
document->SaveToFile(L"InsertImage.docx", FileFormat::Docx2013);
document->Close();
delete document;
}

Insert an Image at a Specified Location in a Word document in C++
Spire.Doc for C++ enables you to insert an image into a Word document and set its position by using the DocPicture->SetHorizontalPosition() and DocPicture->SetVerticalPosition() methods. The detailed steps are as follows:
- Initialize an instance of the Document class.
- Add a section using Document->AddSection() method.
- Add a paragraph to the section using Section->AddParagraph() method.
- Add text to the paragraph using Paragraph->AppendText() method and set formatting.
- Load an image using Image::FromFile() method.
- Add the image to the paragraph using Paragraph->AppendPicture() method.
- Set width and height for the image using DocPicture->SetWidth() and DocPicture->SetHeight() methods.
- Set the horizontal position and vertical position for the image using DocPicture->SetHorizontalPosition() and DocPicture->SetVerticalPosition() methods.
- Set a text wrapping style for the image using DocPicture->SetTextWrappingStyle() method (note that the position settings are not applicable when the text wrapping style is Inline).
- Save the result document using Document->SaveToFile() method.
- C++
#include "Spire.Doc.o.h"
using namespace Spire::Doc;
using namespace Spire::Common;
int main()
{
//Initialize an instance of the Document class
Document* document = new Document();
//Add a section
Section* section = document->AddSection();
//Add a paragraph to the section
Paragraph* paragraph = section->AddParagraph();
//Add text to the paragraph and set formatting
TextRange* tr = paragraph->AppendText(L"The sample demonstrates how to insert an image at a specified location in a Word document.");
tr->GetCharacterFormat()->SetFontName(L"Calibri");
paragraph->ApplyStyle(BuiltinStyle::Heading2);
//Load an image
Spire::Common::Image* image = Image::FromFile(L"Spire.Doc.png");
//Add the image to the paragraph
DocPicture* picture = paragraph->AppendPicture(image);
//Set image size
picture->SetWidth(100);
picture->SetHeight(100);
//Set image position
picture->SetHorizontalPosition(180.0F);
picture->SetVerticalPosition(60.0F);
//Set a text wrapping style for the image (note that the position settings are not applicable when the text wrapping style is Inline)
picture->SetTextWrappingStyle(TextWrappingStyle::Through);
//Save the result document
document->SaveToFile(L"InsertImageAtSpecifiedLocation.docx", FileFormat::Docx);
document->Close();
delete document;
}

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.
C++: Convert PowerPoint Presentations to Images (JPG, PNG, SVG)
2023-03-23 00:57:10 Written by KoohjiConverting PowerPoint presentations to images brings you multiple benefits. For example, it makes it easy for you to share the content with others who may not have access to PowerPoint software; it preserves the formatting of the original presentation, ensuring that the content appears exactly as intended; and it protects the content in the presentation from being edited or modified by others. In this article, you will learn how to convert a PowerPoint Presentation to different image formats in C++ using Spire.Presentation for C++.
- Convert PowerPoint Presentation to JPG or PNG Images
- Convert PowerPoint Presentation to JPG or PNG Images with Specific Size
- Convert PowerPoint Presentation to SVG Images
Install Spire.Presentation for C++
There are two ways to integrate Spire.Presentation 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.Presentation for C++ in a C++ Application
Convert PowerPoint Presentation to JPG or PNG Images in C++
Spire.Presentation for C++ offers the ISlide->SaveAsImage() method which enables you to convert the slides in a PowerPoint presentation to JPG or PNG images. The detailed steps are as follows:
- Initialize an instance of the Presentation class.
- Load a PowerPoint presentation using Presentation->LoadFromFile() method.
- Access the slide collection of the presentation using Presentation->GetSlides() method.
- Iterate through the slides in the collection.
- Save each slide to an image stream using ISlide->SaveAsImage() method.
- Save the image stream to a JPG or PNG file using Stream->Save() method.
- C++
#include "Spire.Presentation.o.h"
using namespace Spire::Presentation;
using namespace std;
int main()
{
//Initialize an instance of the Presentation class
Presentation* ppt = new Presentation();
//Load a PowerPoint presentation
ppt->LoadFromFile(L"Sample.pptx");
//Get the slide collection of the presentation
SlideCollection* slides = ppt->GetSlides();
//Iterate through the slides in the collection
for (int i = 0; i < slides->GetCount(); i++)
{
ISlide* slide = slides->GetItem(i);
//Save each slide to a PNG image
Stream* image = slide->SaveAsImage();
image->Save(( L"Images\\ToImage_img_" + to_wstring(i) + L".png").c_str());
}
ppt->Dispose();
delete ppt;
}

Convert PowerPoint Presentation to JPG or PNG Images with Specific Size in C++
You can convert the slides in a PowerPoint presentation to JPG or PNG images with a specific size using ISlide->SaveAsImage(int width, int height) method. The detailed steps are as follows:
- Initialize an instance of the Presentation class.
- Load a PowerPoint presentation using Presentation->LoadFromFile() method.
- Access the slide collection of the presentation using Presentation->GetSlides() method.
- Iterate through the slides in the collection.
- Save each slide to an image stream using ISlide->SaveAsImage(int width, int height) method.
- Save the image stream to a JPG or PNG file using Stream->Save() method.
- C++
#include "Spire.Presentation.o.h"
using namespace Spire::Presentation;
using namespace std;
int main()
{
//Initialize an instance of the Presentation class
Presentation* ppt = new Presentation();
//Load a PowerPoint presentation
ppt->LoadFromFile(L"Sample.pptx");
//Get the slide collection of the presentation
SlideCollection* slides = ppt->GetSlides();
//Iterate through the slides in the collection
for (int i = 0; i < slides->GetCount(); i++)
{
ISlide* slide = slides->GetItem(i);
//Save each slide to a PNG image with a size of 600 x 400 pixels
Stream* image = slide->SaveAsImage(600, 400);
image->Save(( L"ImagesWithSpecificSize\\ToImage_img_" + to_wstring(i) + L".png").c_str());
}
ppt->Dispose();
delete ppt;
}

Convert PowerPoint Presentation to SVG Images in C++
To convert the slides in a PowerPoint presentation to SVG images, you can use the ISlide->SaveToSVG() method. The detailed steps are as follows:
- Initialize an instance of the Presentation class.
- Load a PowerPoint presentation using Presentation->LoadFromFile() method.
- Access the slide collection of the presentation using Presentation->GetSlides() method.
- Iterate through the slides in the collection.
- Save each slide to an SVG stream using ISlide->SaveToSVG() method.
- Save the SVG stream to an SVG file using Stream->Save() method.
- C++
#include "Spire.Presentation.o.h"
using namespace Spire::Presentation;
using namespace std;
int main()
{
//Initialize an instance of the Presentation class
Presentation* ppt = new Presentation();
//Load a PowerPoint presentation
ppt->LoadFromFile(L"Sample.pptx");
//Get the slide collection of the presentation
SlideCollection* slides = ppt->GetSlides();
//Set whether to retain notes while converting PowerPoint to SVG
ppt->SetIsNoteRetained(true);
//Iterate through the slides in the collection
for (int i = 0; i < slides->GetCount(); i++)
{
ISlide* slide = slides->GetItem(i);
//Save each slide to an SVG image
Stream* svg = slide->SaveToSVG();
svg->Save((L"SvgImages\\ToSVG-" + to_wstring(i) + L".svg").c_str());
}
ppt->Dispose();
delete ppt;
}

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.
Spire.OCR for .NET is a professional OCR library that supports recognizing text from Images (such as JPG, PNG, GIF, BMP, and TIFF) in both .NET Framework and .NET Core applications. In this article, we will explain how to use Spire.OCR for .NET to read text from images in .NET Framework applications.
Step 1: Create a console application (.NET Framework) in Visual Studio.


Step 2: Change the platform target of the application to X64.
In the application's solution explorer, right-click on the solution name and then click "Properties".

Change the platform target of the application to X64. This step must be performed since Spire.OCR only supports 64-bit platforms.

Step 3: Add a reference to Spire.OCR for .NET DLL in the application.
We recommend installing Spire.OCR for .NET through NuGet (Note: only Spire.OCR for .NET Version 1.8.0 or above supports working with .NET Framework). The detailed steps are as follows:
- In the application's solution explorer, right-click on the solution name or "References" and select "Manage NuGet Packages".
- Click the "Browse" tab and search for Spire.OCR.
- Click "Install" to install Spire.OCR.

Step 4: Copy DLLs from the "packages" directory to the "Debug" directory in the application.
When you install Spire.OCR through NuGet, NuGet downloads the packages and puts them in your application under a directory called "packages". You need to find the "Spire.OCR" directory under the "packages" directory, then copy the DLLs under the "Spire.OCR" directory (packages\Spire.OCR.1.8.0\runtimes\win-x64\native) to the "Debug" directory of your application.

Now you have successfully included Spire.OCR in your .NET Framework application. You can refer to the following code example to read text from images using Spire.OCR.
- C#
using Spire.OCR;
using System.IO;
namespace OcrImage
{
internal class Program
{
static void Main(string[] args)
{
//Create an instance of the OcrScanner class
OcrScanner scanner = new OcrScanner();
//Call the OcrScanner.Scan() method to scan text from an image
scanner.Scan("image.png");
//Save the scanned text to a .txt file
string text = scanner.Text.ToString();
File.WriteAllText("output.txt", text);
}
}
}
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.