使用我們的 C++ 庫,開發人員可以輕鬆地從頭開始製作 DOCX。為此,C++ 開發人員只需執行幾個步驟:
值得注意的是,從技術上講,空白文檔應該包含一個段落,因此當您以編程方式創建 DOCX 文檔時,您將獲得完全一致的基本文檔結構。
請注意,您可以立即將內容添加到新創建的 DOCX 文件中。因此,您將獲得的不僅僅是一個空文檔,而是一個包含必要內容的文檔。有關如何編輯文檔的更多信息,請參閱編輯頁面。
此 C++ 庫允許您以編程方式創建 DOCX 文檔。試試我們強大的功能,看看如何使用以下示例以某些格式創建 DOCX:
dotnet add package Aspose.Words.Cpp
複製
using namespace Aspose::Words;
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
auto font = builder->get_Font();
font->set_Name(u"Courier New");
font->set_Color(System::Drawing::Color::get_Blue());
font->set_Size(36);
font->set_HighlightColor(System::Drawing::Color::get_Yellow());
builder->Write(u"Morbi enim nunc faucibus a.");
doc->Save(u"Output.docx");
using namespace Aspose::Words;
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
auto firstRun = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed. ");
auto secondRun = MakeObject<Run>(doc, u"Morbi enim nunc faucibus a.");
doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(firstRun);
doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(secondRun);
builder->MoveTo(secondRun);
builder->StartBookmark(u"Aspose bookmark");
// 如果 NextSibling 為 null,很可能這是該段落中的最後一次 Run 。
if (secondRun->get_NextSibling() != NULL)
builder->MoveTo(secondRun->get_NextSibling());
else
builder->MoveTo(secondRun->get_ParentParagraph());
builder->EndBookmark(u"Aspose bookmark");
doc->Save(u"Output.docx");
using namespace Aspose::Words;
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
auto firstRun = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed. ");
auto secondRun = MakeObject<Run>(doc, u"Morbi enim nunc faucibus a.");
doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(firstRun);
doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(secondRun);
builder->MoveTo(secondRun);
builder->StartBookmark(u"Aspose bookmark");
// 如果 NextSibling 為 null,很可能這是該段落中的最後一次 Run 。
if (secondRun->get_NextSibling() != NULL)
builder->MoveTo(secondRun->get_NextSibling());
else
builder->MoveTo(secondRun->get_ParentParagraph());
builder->EndBookmark(u"Aspose bookmark");
auto saveOptions = MakeObject<PdfSaveOptions>();
saveOptions->get_OutlineOptions()->get_BookmarksOutlineLevels()->Add(u"Aspose bookmark", 1);
doc->Save(u"Output.docx", saveOptions);
using namespace Aspose::Words;
auto doc = MakeObject<Document>();
auto run = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed.");
auto para = doc->get_FirstSection()->get_Body()->get_FirstParagraph();
para->AppendChild(run);
auto comment = MakeObject<Comment>(doc, u"John Doe", u"JD", System::DateTime::get_Today());
comment->SetText(u"Quisque fringilla leo.");
auto commentRangeStart = MakeObject<CommentRangeStart>(doc, comment->get_Id());
auto commentRangeEnd = MakeObject<CommentRangeEnd>(doc, comment->get_Id());
run->get_ParentNode()->InsertBefore(commentRangeStart, run);
run->get_ParentNode()->InsertAfter(commentRangeEnd, run);
commentRangeEnd->get_ParentNode()->InsertAfter(comment, commentRangeEnd);
comment->AddReply(u"Jane Doe", u"JD", System::DateTime::get_Now(),
u"Pellentesque vel sapien justo.");
doc->Save(u"Output.docx");
using namespace Aspose::Words;
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
auto firstRun = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed. ");
auto secondRun = MakeObject<Run>(doc, u"Morbi enim nunc faucibus a.");
doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(firstRun);
doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(secondRun);
builder->MoveTo(secondRun);
builder->StartBookmark(u"Aspose bookmark");
// 如果 NextSibling 為 null,很可能這是該段落中的最後一次 Run 。
if (secondRun->get_NextSibling() != NULL)
builder->MoveTo(secondRun->get_NextSibling());
else
builder->MoveTo(secondRun->get_ParentParagraph());
builder->EndBookmark(u"Aspose bookmark");
doc->Save(u"Output.docx");
using namespace Aspose::Words;
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
auto firstRun = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed. ");
auto secondRun = MakeObject<Run>(doc, u"Morbi enim nunc faucibus a.");
doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(firstRun);
doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(secondRun);
builder->MoveTo(secondRun);
builder->StartBookmark(u"Aspose bookmark");
// 如果 NextSibling 為 null,很可能這是該段落中的最後一次 Run 。
if (secondRun->get_NextSibling() != NULL)
builder->MoveTo(secondRun->get_NextSibling());
else
builder->MoveTo(secondRun->get_ParentParagraph());
builder->EndBookmark(u"Aspose bookmark");
auto saveOptions = MakeObject<PdfSaveOptions>();
saveOptions->get_OutlineOptions()->get_BookmarksOutlineLevels()->Add(u"Aspose bookmark", 1);
doc->Save(u"Output.docx", saveOptions);
using namespace Aspose::Words;
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
auto firstRun = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed. ");
auto secondRun = MakeObject<Run>(doc, u"Morbi enim nunc faucibus a.");
doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(firstRun);
doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(secondRun);
builder->MoveTo(secondRun);
builder->StartBookmark(u"Aspose bookmark");
// 如果 NextSibling 為 null,很可能這是該段落中的最後一次 Run 。
if (secondRun->get_NextSibling() != NULL)
builder->MoveTo(secondRun->get_NextSibling());
else
builder->MoveTo(secondRun->get_ParentParagraph());
builder->EndBookmark(u"Aspose bookmark");
doc->Save(u"Output.docx");
using namespace Aspose::Words;
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
auto firstRun = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed. ");
auto secondRun = MakeObject<Run>(doc, u"Morbi enim nunc faucibus a.");
doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(firstRun);
doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(secondRun);
builder->MoveTo(secondRun);
builder->StartBookmark(u"Aspose bookmark");
// 如果 NextSibling 為 null,很可能這是該段落中的最後一次 Run 。
if (secondRun->get_NextSibling() != NULL)
builder->MoveTo(secondRun->get_NextSibling());
else
builder->MoveTo(secondRun->get_ParentParagraph());
builder->EndBookmark(u"Aspose bookmark");
doc->Save(u"Output.docx");
using namespace Aspose::Words;
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
auto firstRun = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed. ");
auto secondRun = MakeObject<Run>(doc, u"Morbi enim nunc faucibus a.");
doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(firstRun);
doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(secondRun);
builder->MoveTo(secondRun);
builder->StartBookmark(u"Aspose bookmark");
// 如果 NextSibling 為 null,很可能這是該段落中的最後一次 Run 。
if (secondRun->get_NextSibling() != NULL)
builder->MoveTo(secondRun->get_NextSibling());
else
builder->MoveTo(secondRun->get_ParentParagraph());
builder->EndBookmark(u"Aspose bookmark");
auto saveOptions = MakeObject<PdfSaveOptions>();
saveOptions->get_OutlineOptions()->get_BookmarksOutlineLevels()->Add(u"Aspose bookmark", 1);
doc->Save(u"Output.docx", saveOptions);
using namespace Aspose::Words;
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
auto shape = builder->InsertChart(ChartType::Pie, 432, 252);
auto chart = shape->get_Chart();
chart->get_Title()->set_Text(u"Demo Chart");
chart->get_Series()->Clear();
chart->get_Series()->Add(u"Series 1",
MakeArray<String>({ u"Category1", u"Category2", u"Category3" }),
MakeArray<double>({ 2.7, 3.2, 0.8 }));
doc->Save(u"Output.docx");
using namespace Aspose::Words;
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
auto firstRun = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed. ");
auto secondRun = MakeObject<Run>(doc, u"Morbi enim nunc faucibus a.");
doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(firstRun);
doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(secondRun);
builder->MoveTo(secondRun);
builder->StartBookmark(u"Aspose bookmark");
// 如果 NextSibling 為 null,很可能這是該段落中的最後一次 Run 。
if (secondRun->get_NextSibling() != NULL)
builder->MoveTo(secondRun->get_NextSibling());
else
builder->MoveTo(secondRun->get_ParentParagraph());
builder->EndBookmark(u"Aspose bookmark");
doc->Save(u"Output.docx");
using namespace Aspose::Words;
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
auto firstRun = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed. ");
auto secondRun = MakeObject<Run>(doc, u"Morbi enim nunc faucibus a.");
doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(firstRun);
doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(secondRun);
builder->MoveTo(secondRun);
builder->StartBookmark(u"Aspose bookmark");
// 如果 NextSibling 為 null,很可能這是該段落中的最後一次 Run 。
if (secondRun->get_NextSibling() != NULL)
builder->MoveTo(secondRun->get_NextSibling());
else
builder->MoveTo(secondRun->get_ParentParagraph());
builder->EndBookmark(u"Aspose bookmark");
auto saveOptions = MakeObject<PdfSaveOptions>();
saveOptions->get_OutlineOptions()->get_BookmarksOutlineLevels()->Add(u"Aspose bookmark", 1);
doc->Save(u"Output.docx", saveOptions);
有三個選項可以將 Aspose.Words for C++ 安裝到您的開發人員環境中。請選擇符合您需求的一種,並按照分步說明進行操作:
您可以使用這個 C++ 庫在 Microsoft Windows 、Linux 和 macOS 操作系統上開發軟件:
如果您為 Linux 或 macOS 開發軟件,請查看產品文檔中 fontconfig 和 mesa-glu 開源包)的信息。