通过免费 C++ API 创建动态 Word DOCX 文件
开源 C++ DOCX 库可直接从 C++ 代码生成 Word 文档(.docx 格式)。它允许添加表格和图片,对文本应用格式和样式等。
MiniDocx 是什么?
在编程方式下处理 Microsoft Word 文档一直是 C++ 开发者的一大挑战,通常需要庞大的依赖或专有软件的安装。MiniDocx 通过提供轻量、现代且用户友好的解决方案,简化了直接从 C++ 应用创建 Word 文档的过程。此开源库消除了对 Microsoft Office 或 WPS Office 的安装需求,同时提供了强大的文档操作功能。MiniDocx 包含覆盖 Word 文档创建基本方面的必备功能。它支持章节、段落、富文本格式、表格、图片、样式和列表。这套完整的功能集使开发者能够创建符合专业标准的复杂、格式良好的文档。
MiniDocx 是一个现代的开源 C++ 库,使开发者能够以编程方式创建和操作 Microsoft Word .docx 文档——无需安装 Microsoft Word 或 WPS Office。该库的架构基于现代 C++20 标准,利用最新的语言特性提升性能、类型安全和代码可读性。MiniDocx 的突出特点之一是其跨平台兼容性。该库在 Windows、Linux 和 macOS 操作系统上无缝运行,是需要在多个平台上运行的应用开发者的理想选择。
MiniDocx 入门指南
推荐的 MiniDocx 安装方式是通过 GitHub。请使用以下命令进行顺畅的安装。
通过 GitHub 安装 MiniDocx
git clone git@github.com:totravel/minidocx.git
cd minidocx You can also download it directly from Aspose product page.通过 C++ 创建 Word Docx 文档
开源的 MiniDocx 库让软件开发者可以轻松在 C++ 应用中创建和操作 Word Docx 文档。该库已包含对格式化、添加文本、表格和图片的支持。最简单的了解 MiniDocx 方法是通过实际示例。创建一个带格式化文本的基本 Word 文档,展示了该库直观的 API 设计和简明的工作流。
如何使用 C++ 库创建 Word 文档?
#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;
}
向文档添加表格(使用 C++ 库)
表格是呈现 Word 文档中结构化数据的关键,MiniDocx 提供了全面的表格创建和格式化功能。表格由行和单元格组成,每个单元格可包含文本、格式甚至嵌套内容。创建表格涉及多个步骤:定义表格结构、添加行、填充单元格内容以及应用格式。以下是一个详细示例,展示软件开发者如何在 C++ 应用中创建格式化表格。
如何使用 C++ 库在 Word 文档中创建格式化表格?
#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;
}
向 Word DOCX 文件插入图片和图像
视觉内容提升文档的可读性和参与度,使图像支持成为任何文档生成库的关键特性。MiniDocx 让软件开发者能够在文档中插入图像,并控制其尺寸和位置。向文档添加图像需要指定图像文件路径,并可选地设置尺寸。以下示例展示了如何通过 C++ 库在 Word 文档中插入图像。
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;
}
文档章节与页面布局
章节为控制 Word 文档中的页面布局属性提供框架。每个章节可以拥有不同的页面设置,包括页面大小、方向、边距、页眉和页脚。这种基于章节的架构允许创建具有多种页面配置的复杂文档布局。多个章节的典型用例是创建同时包含纵向和横向页面的文档,例如包含宽表格或需要横向布局的图表的报告。以下示例展示了如何在 C++ 应用中处理 Word 文档的多个章节。
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;
}