在 C++ 中制作 PDF 或 Word 文档

使用我们的高保真 C++ 库以编程方式创建几乎任何格式的新文档

使用我们的编程 API,C++ 开发人员只需几行代码即可轻松制作 PDF、DOC、DOCX、HTML、EPUB 和许多其他格式的文档。

查看代码片段

使用 C++ 制作文档

借助给定的强大 API,C++ 开发人员可以创建几乎任何格式的文档。为此,您需要使用我们的 C++ 库执行几个步骤:

  1. 添加文件名
  2. 开始使用 C++ 创建文档
  3. 以选定的格式保存创建的文档

值得注意的是,从技术上讲,一个空白文档应该包含一个段落,因此当您以编程方式创建文档时,您将获得准确的基本文档结构。

请注意,您可以立即将内容添加到新创建的文档中。因此,您将获得的不仅仅是一个空文档,而是一个包含必要内容的文档。有关如何编辑文档的更多信息,请参阅编辑页面。

以编程方式在 C++ 中创建文档

给定的 C++ 库允许您以任何受支持的格式(PDF、DOCX、DOC、RTF、ODT、EPUB、HTML 等)以编程方式创建文档。

试试我们强大的功能,看看如何使用以下示例创建某些格式的文档:

使用 C++ 创建一个新文档
从列表中选择目标格式
运行代码
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);
运行代码

如何在C++中制作文档

  1. Aspose.Words for C++
  2. 将库引用(导入库)添加到您的 C++ 项目
  3. 创建一个新文档
  4. 调用"Save()"方法,传递文件名
  5. 将结果作为单独的文件获取

C++ 创建文档的库

有三个选项可以将 Aspose.Words for C++ 安装到您的开发人员环境中。请选择符合您需求的一种,并按照分步说明进行操作:

系统要求

您可以使用这个 C++ 库在 Microsoft Windows 、Linux 和 macOS 操作系统上开发软件:

  • Linux 需要 GCC >= 6.3.0 和 Clang >= 3.9.1
  • Xcode >= 12.5.1 ,macOS 需要 Clang 和 libc++

如果您为 Linux 或 macOS 开发软件,请查看产品文档中 fontconfig 和 mesa-glu 开源包)的信息。

最流行的文件格式

5%

订阅 Aspose 产品更新

获取直接发送到您的邮箱的每月通讯和优惠。

© Aspose Pty Ltd 2001-2024. 版权所有。