無料 C++ API で動的な Word DOCX ファイルを作成
オープンソース C++ DOCX ライブラリは、C++ コードから直接 Word ドキュメント(.docx 形式)を生成します。テーブルや画像の追加、テキストへの書式やスタイルの適用などが可能です。
MiniDocx とは?
Microsoft Word ドキュメントをプログラムで操作することは、従来 C++ 開発者にとって大きな依存関係や専有ソフトウェアのインストールが必要になるなど、困難な課題でした。MiniDocx は、C++ アプリケーションから直接 Word ドキュメントを作成するための軽量でモダン、かつユーザーフレンドリーなソリューションを提供し、この状況を変えます。このオープンソースライブラリは、Microsoft Office や WPS Office のインストール不要で、ドキュメント操作の強力な機能を提供します。MiniDocx は、Word ドキュメント作成の基本要素を網羅する必須機能が満載です。セクション、段落、リッチテキストの書式設定、テーブル、画像、スタイル、リストをサポートします。この包括的な機能セットにより、開発者はプロフェッショナルな基準を満たす複雑で整ったドキュメントを作成できます。
MiniDocx は、Microsoft Word .docx ドキュメントをプログラムから作成・操作できる、モダンなオープンソース C++ ライブラリです。Microsoft Word や WPS Office のインストールは不要です。ライブラリのアーキテクチャは最新の C++20 標準を基盤としており、パフォーマンス、型安全性、コードの明瞭性を向上させる最新言語機能を活用しています。MiniDocx の特筆すべき特徴の一つは、クロスプラットフォーム互換性です。Windows、Linux、macOS の各 OS でシームレスに動作し、複数プラットフォームで実行するアプリケーションを開発する開発者にとって最適な選択肢となります。
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 ドキュメントに画像を挿入する例です。
C++ ライブラリで Word ドキュメントに画像を挿入する方法は?
#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 ドキュメントの複数セクションを扱う方法を示す例です。
C++ ライブラリで Word ドキュメントの複数セクションを扱う方法は?
#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;
}