How to Integrate Spire.Doc for C++ in a C++ Application

Spire.Doc for C++ is a professional Word library built for developers to read, create, edit, compare and convert Word documents in any type of C++ applications. This article demonstrates how to integrate Spire.Doc for C++ into your C++ application in two different ways.

Install Spire.Doc for C++ via NuGet

Step 1

Create a C++ project in Visual Studio 2022.

How to Integrate Spire.Doc for C++ in a C++ Application

Step 2

Right-click 'References' in the Solution Explorer and choose 'Manage NuGet Package' in the popup menu.

How to Integrate Spire.Doc for C++ in a C++ Application

Click 'Browse', search for 'spire.doc.cpp', and install it in your project.

How to Integrate Spire.Doc for C++ in a C++ Application

Step 3

Write test code in the Console Application.cpp file,the code example below shows you how to create a simple Word file using Spire.Doc for C++.

  • C++
#include <iostream>
#include <string>
#include "Spire/Doc/pch.h"
#ifdef DocumentProperties
#undef DocumentProperties
#endif
#define DocumentProperties DocumentPropertiesA
#include "Spire/Doc/Document.h"
#include "Spire/Doc/Documents/Paragraph.h"
#include "Spire/Doc/ExportOptions.h"
#include "Spire/Doc/Fields/TextRange.h"
#include "Spire/Doc/FileFormat.h"
#include "Spire/Doc/Section.h"
#include "Spire/Doc/ToPdfParameterList.h"
#include "Spire/Doc/common/h/Stream.h"
using namespace std;
using namespace Spire::Doc;
using namespace Spire::Doc::Common;
using namespace Spire::Doc::Documents;
using namespace Spire::Doc::Fields;

int main()
{

    std::string outputFile = "HelloWorld.docx";

    // Create a Word document
    intrusive_ptr<Document> document = new Document();

    // Add a new section
    intrusive_ptr<Section> section = document->AddSection();

    // Add a new paragraph
    intrusive_ptr<Paragraph> paragraph = section->AddParagraph();

    // Append text
    paragraph->AppendText("Hello World!");

    // Save the document file.
    document->SaveToFile(outputFile.c_str(), FileFormat::Docx);

    // Release resources
    document->Close();
    return 0;
}

Install Spire.Doc for C++ by Manually Importing Libraries

Step 1

Download Spire.Doc for C++ package and unzip it somewhere on your disc to get the following files.

How to Integrate Spire.Doc for C++ in a C++ Application

Step 2

Create a C++ project in Visual Studio 2022.

How to Integrate Spire.Doc for C++ in a C++ Application

Step 3

Copy the 'include' folder and the 'lib' folder from the product package to your project, and save them under the same folder where the .sln file exists.

How to Integrate Spire.Doc for C++ in a C++ Application

Step 4

Modify .vcxproj project configuration:

How to Integrate Spire.Doc for C++ in a C++ Application

<ItemDefinitionGroup>
  <ClCompile>
    <AdditionalIncludeDirectories>$(ProjectDir)include;$(ProjectDir)include\Spire\Doc;$(ProjectDir)include\Spire\Doc\common\h;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
    <DisableSpecificWarnings>4244;4250;4267;%(DisableSpecificWarnings)</DisableSpecificWarnings>
    <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
    <UndefinePreprocessorDefinitions>_DEBUG;%(UndefinePreprocessorDefinitions)</UndefinePreprocessorDefinitions>
  </ClCompile>
  <Link>
    <AdditionalDependencies>$(ProjectDir)lib\win-x64\SpireDocCpp.lib;%(AdditionalDependencies)</AdditionalDependencies>
  </Link>
  <PostBuildEvent>
    <Command>if not exist "$(ProjectDir)Output" mkdir "$(ProjectDir)Output"
xcopy /Y /D "$(ProjectDir)lib\win-x64\*.dll" "$(OutDir)"</Command>
  </PostBuildEvent>
</ItemDefinitionGroup>

Step 5

Write test code in the Console Application.cpp file.

How to Integrate Spire.Doc for C++ in a C++ Application

The code example above shows you how to create a simple Word file using Spire.Doc for C++.

How to Integrate Spire.Doc for C++ in a C++ Application

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.