C++: Create or Edit Word Documents

Word documents are files created with Microsoft Word or other word-processing programs. They are used by almost all types of businesses around the world. A wide variety of professional documents, such as business contracts, essays, brochures, letters, resumes, and reports, are created and saved in the form of Word documents. In this article, you will learn how to create or edit Word documents programmatically in C++ using Spire.Doc for C++ library.

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

Create a Word Document in C++

Using Spire.Doc for C++, you can create a Word document with one or more sections and add various elements to it, such as paragraphs, tables, images, lists, hyperlinks, watermarks, headers, footers, content controls, and comments.

The following steps show you how to create a simple Word document with a section and three paragraphs:

  • Initialize an instance of the Document class.
  • Add a section to the document using Document->AddSection() method.
  • Set page margins for the section.
  • Add three paragraphs to the section using Section->AddParagraph() method.
  • Add text to the paragraphs using Paragraph->AppendText() method.
  • Initialize two instances of the ParagraphStyle class to create two paragraph styles, and then apply the styles to the paragraphs respectively using Paragraph->ApplyStyle() method.
  • Save the result document to a Word file using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h"

using namespace Spire::Doc;

int main()
{
    //Initialize an instance of the Document class
    intrusive_ptr<Document> doc = new Document();
    //Add a section to the document
    intrusive_ptr<Section> section = doc->AddSection();

    //Set page margins
    section->GetPageSetup()->GetMargins()->SetAll(72);

    //Add a title paragraph to the section
    intrusive_ptr<Paragraph> titlePara = section->AddParagraph();
    //Add text to the paragraph
    titlePara->AppendText(L"Spire.Doc for C++ Introduction");

    //Add a body paragraph to the section
    intrusive_ptr<Paragraph> bodyPara1 = section->AddParagraph();
    //Add text to the paragraph
    bodyPara1->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.");

    //Add a body paragraph to the section
    intrusive_ptr<Paragraph> bodyPara2 = section->AddParagraph();
    //Add text to the paragraph
    bodyPara2->AppendText(L"By using Spire.Doc for C++, users can convert Word Doc/Docx to XML, RTF, EMF, TXT, XPS, EPUB, HTML, SVG, ODT and vice versa. Spire.Doc for C++ also supports converting Word Doc/Docx to PDF and HTML to image.");

    //Create a style and apply it to the title paragraph
    intrusive_ptr<ParagraphStyle> style1 = new ParagraphStyle(doc);
    style1->SetName(L"titleStyle");
    style1->GetCharacterFormat()->SetBold(true);
    style1->GetCharacterFormat()->SetTextColor(Color::GetBlue());
    style1->GetCharacterFormat()->SetFontName(L"Calibri");
    style1->GetCharacterFormat()->SetFontSize(16);
    doc->GetStyles()->Add(style1);
    titlePara->ApplyStyle(L"titleStyle");

    //Create a style and apply it to the body paragraphs
    intrusive_ptr<ParagraphStyle> style2 = new ParagraphStyle(doc);
    style2->SetName(L"paraStyle");
    style2->GetCharacterFormat()->SetFontName(L"Calibri");
    style2->GetCharacterFormat()->SetFontSize(12);
    doc->GetStyles()->Add(style2);
    bodyPara1->ApplyStyle(L"paraStyle");
    bodyPara2->ApplyStyle(L"paraStyle");

    //Set horizontal alignment for the title and body paragraphs
    titlePara->GetFormat()->SetHorizontalAlignment(HorizontalAlignment::Center);
    bodyPara1->GetFormat()->SetHorizontalAlignment(HorizontalAlignment::Justify);
    bodyPara2->GetFormat()->SetHorizontalAlignment(HorizontalAlignment::Justify);

    //Set spacing after the title and body paragraphs
    titlePara->GetFormat()->SetAfterSpacing(10);
    bodyPara1->GetFormat()->SetAfterSpacing(10);

    //Save the result document
    doc->SaveToFile(L"CreateWord.docx", FileFormat::Docx2013);
    doc->Dispose();
}

C++: Create or Edit Word Documents

Edit an Existing Word Document in C++

Apart from creating Word documents from scratch, Spire.Doc for C++ also enables you to edit existing Word documents. For example, you can modify existing elements in the document or add new elements to the document.

The following steps show you how to modify the text of a specific paragraph in a Word document:

  • Initialize an instance of the Document class.
  • Load the Word document using Document->LoadFromFile() method.
  • Access a specific section in the document by its index using Document->GetSections()->GetItem(int index) method.
  • Access a specific paragraph in the section by its index using Section->GetParagraphs()->GetItem(int index) method.
  • Modify the paragraph text using Paragraph->SetText() method.
  • Save the result document using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h"

using namespace Spire::Doc;

int main()
{
    //Initialize an instance of the Document class
    intrusive_ptr<Document> doc = new Document();
    //Load a Word document
    doc->LoadFromFile(L"CreateWord.docx");

    //Access the first section in the document
    intrusive_ptr<Section> section = doc->GetSections()->GetItemInSectionCollection(0);

    //Access the second paragraph in the first section
    intrusive_ptr<Paragraph> para = section->GetParagraphs()->GetItemInParagraphCollection(1);
    //Modify the paragraph text
    para->SetText(L"This Paragraph is Updated");

    //Save the result document
    doc->SaveToFile(L"EditWord.docx", FileFormat::Docx2013);
    doc->Dispose();
}

C++: Create or Edit Word Documents

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.