Dynamic Word DOCX File Creation via Free C++ API
Open Source C++ DOCX Library to Generate Word Documents (.docx Format) Directly from C++ Code. It allows Adding Tables & Images, Applying Formats & Styles to Text & more.
What is MiniDocx?
Working with Microsoft Word documents programmatically has traditionally been a challenging task for C++ developers, often requiring bulky dependencies or proprietary software installations. MiniDocx changes this landscape by offering a lightweight, modern, and user-friendly solution for creating Word documents directly from C++ applications. This open-source library eliminates the need for Microsoft Office or WPS Office installations while providing robust functionality for document manipulation.’ MiniDocx comes packed with essential features that cover the fundamental aspects of Word document creation. It supports sections, paragraphs, rich text formatting, tables, pictures, styles, and lists. This comprehensive feature set allows developers to create complex, well-formatted documents that meet professional standards.
MiniDocx is a modern, open-source C++ library that enables developers to create and manipulate Microsoft Word .docx documents programmatically — without requiring Microsoft Word or WPS Office to be installed.The library's architecture is built around modern C++20 standards, leveraging the latest language features for better performance, type safety, and code clarity. One of the standout characteristics of MiniDocx is its cross-platform compatibility. The library works seamlessly across Windows, Linux, and macOS operating systems, making it an excellent choice for developers building applications that need to run on multiple platforms.
Getting Started with MiniDocx
The recommend way to install MiniDocx is via GitHub. Please use the following command for a smooth installation.
Install MiniDocx via GitHub
git clone git@github.com:totravel/minidocx.git
cd minidocx You can also download it directly from Aspose product page.Creating Word Docx Document via C++
The open source MiniDocx library makes it easy for software developers to create and manipulate Word Docx documents inside C++ applications. The library has included support for apply formatting, adding text, tables and images inside Word documents. The simplest way to understand MiniDocx is through a practical example. Creating a basic Word document with formatted text demonstrates the library's intuitive API design and straightforward workflow.
How to Create Word Documents using C++ Library?
#include "minidocx/minidocx.hpp"
#include
int main()
{
using namespace md;
try {
Document doc;
SectionPointer sect = doc.addSection();
ParagraphPointer para = sect->addParagraph();
para->prop_.align_ = Alignment::Centered;
RichTextPointer rich = para->addRichText("Happy Chinese New Year!");
rich->prop_.fontSize_ = 32;
rich->prop_.color_ = "FF0000";
doc.saveAs("a.docx");
}
catch (const Exception& ex) {
std::cerr << ex.what() << std::endl;
}
return 0;
}
Add Tables to Documents via C++ Library
Tables are essential for presenting structured data in Word documents, and MiniDocx provides comprehensive table creation and formatting capabilities. Tables consist of rows and cells, with each cell able to contain text, formatting, and even nested content. Creating a table involves several steps: defining the table structure, adding rows, populating cells with content, and applying formatting. Here's a detailed example showing how software developers can create a formatted table inside C++ apps.
How to Create Formatted Table in Word Document via C++ Library?
#include "minidocx/minidocx.hpp"
int main()
{
using namespace md;
try {
Document doc;
SectionPointer sect = doc.addSection();
// Create a table with 3 rows and 2 columns
TablePointer table = sect->addTable(3, 2);
// Access and populate cells
// First row - header
auto cell00 = table->cell(0, 0);
auto para00 = cell00->addParagraph();
auto text00 = para00->addRichText("Name");
text00->prop_.bold_ = true;
auto cell01 = table->cell(0, 1);
auto para01 = cell01->addParagraph();
auto text01 = para01->addRichText("Age");
text01->prop_.bold_ = true;
// Second row
auto cell10 = table->cell(1, 0);
auto para10 = cell10->addParagraph();
para10->addRichText("Alice");
auto cell11 = table->cell(1, 1);
auto para11 = cell11->addParagraph();
para11->addRichText("25");
// Third row
auto cell20 = table->cell(2, 0);
auto para20 = cell20->addParagraph();
para20->addRichText("Bob");
auto cell21 = table->cell(2, 1);
auto para21 = cell21->addParagraph();
para21->addRichText("30");
doc.saveAs("table_document.docx");
}
catch (const Exception& ex) {
std::cerr << ex.what() << std::endl;
}
return 0;
}
Insert Images and Pictures to Word DOCX File
Visual content enhances document readability and engagement, making image support a critical feature for any document generation library. MiniDocx enables software developers to insert images into documents with control over sizing and positioning. Adding images to a document requires specifying the image file path and optionally setting dimensions. Here's an example showing how to insert an image into a Word document via C++ library.
How to Insert an Image into a Word Document via C++ library?
#include "minidocx/minidocx.hpp"
int main()
{
using namespace md;
try {
Document doc;
SectionPointer sect = doc.addSection();
// Add a paragraph before the image
ParagraphPointer para1 = sect->addParagraph();
para1->addRichText("Below is an important diagram:");
// Add a paragraph containing the image
ParagraphPointer para2 = sect->addParagraph();
para2->prop_.align_ = Alignment::Centered;
// Insert the picture
PicturePointer pic = para2->addPicture("path/to/image.png");
pic->prop_.width_ = 400;
pic->prop_.height_ = 300;
// Add a paragraph after the image
ParagraphPointer para3 = sect->addParagraph();
para3->addRichText("Figure 1: Important visualization");
doc.saveAs("document_with_image.docx");
}
catch (const Exception& ex) {
std::cerr << ex.what() << std::endl;
}
return 0;
}
Document Sections and Page Layout
Sections provide the framework for controlling page layout properties within Word documents. Each section can have different page settings, including page size, orientation, margins, headers, and footers. This section-based architecture allows for complex document layouts with varying page configurations. A typical use case for multiple sections is creating documents with both portrait and landscape pages, such as reports that include wide tables or charts requiring landscape orientation. Here's an example showing how to work with multiple sections of Word documents inside C++ applications.
How to Work with Multiple Sections of Word Documents via C++ Library?
#include "minidocx/minidocx.hpp"
int main()
{
using namespace md;
try {
Document doc;
// First section with portrait orientation
SectionPointer sect1 = doc.addSection();
sect1->prop_.orientation_ = Orientation::Portrait;
sect1->prop_.pageWidth_ = 8.5 * 1440; // Letter size in twips
sect1->prop_.pageHeight_ = 11 * 1440;
ParagraphPointer para1 = sect1->addParagraph();
para1->addRichText("This is content in portrait orientation.");
// Second section with landscape orientation
SectionPointer sect2 = doc.addSection();
sect2->prop_.orientation_ = Orientation::Landscape;
sect2->prop_.pageWidth_ = 11 * 1440;
sect2->prop_.pageHeight_ = 8.5 * 1440;
ParagraphPointer para2 = sect2->addParagraph();
para2->addRichText("This content appears in landscape orientation.");
doc.saveAs("multi_section_document.docx");
}
catch (const Exception& ex) {
std::cerr << ex.what() << std::endl;
}
return 0;
}