1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Word处理控件Aspose.Words功能演示:使用 C++ 创建 MS Word 文档 (DOC/DOCX)

Word处理控件Aspose.Words功能演示:使用 C++ 创建 MS Word 文档 (DOC/DOCX)

时间:2024-07-06 08:20:06

相关推荐

Word处理控件Aspose.Words功能演示:使用 C++ 创建 MS Word 文档 (DOC/DOCX)

Aspose.Words是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,

Aspose API支持流行文件格式处理,并允许将各类文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

Aspose.words 最新下载(761297826)/product/4116/download

Aspose.Words是一个功能丰富的 API 集合,可让您以编程方式创建、编辑和转换 MS Word 文档。它为操作文字处理文档提供了广泛的基本和高级功能。在本文中,您将学习如何使用Aspose.Words for C++并使用 C++ 从头开始创建 MS Word 文档。分步指南和代码示例将让您了解如何在 Word 文档中插入文本、图像、表格、列表和其他元素。

一、用于创建 MS Word 文档的 C++ API

Aspose.Words for C++允许您在没有 MS Word 的情况下在 C++ 应用程序中生成和操作文字处理文档。您可以通过以下命令使用NuGet下载API 或将其安装在您的 C++ 应用程序中。

PM> Install-Package Aspose.Words.Cpp

二、使用 C++ 创建 MS Word 文档

让我们首先创建一个简单的 Word 文档并将其另存为.doc或.docx文件。为此,您需要执行以下步骤:

使用Document类创建一个空白的 Word 文档。创建DocumentBuilder类的对象以将内容添加到文档中。使用DocumentBuilder->Writeln()方法添加文本。使用Document->Save()方法将文档保存为.doc或.docx文件。

以下代码示例显示了如何使用 C++ 创建 Word DOCX 文档。

// Initialize a Document.System::SharedPtr<Document> doc = System::MakeObject<Document>();// Use a document builder to add content to the document.System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);// Add textbuilder->Writeln(u"Hello World!");// Save the document to disk.doc->Save(u"document.docx");

三、使用 C++ 编辑或更新现有的 Word DOC/DOCX

您还可以使用 Aspose.Words for C++ 编辑现有的 Word 文档。为此,API 使用文档对象模型 (DOM) 作为文档的内存表示。DOM 允许您访问 Word 文档的元素,例如页眉/页脚、段落、表格等。在此处阅读有关 DOM 的更多信息。

要更新 Word 文档,只需使用Document类加载它并根据需要进行处理。以下是编辑和更新现有 Word 文档的步骤。

使用Document类加载 Word 文档。创建DocumentBuilder类的对象以访问内容。访问所需的段落(或任何其他元素)并更新内容。使用Document->Save()方法保存更新的文档。

下面的代码示例显示了如何使用 C++ 更新 Word 文档中段落的文本。

// Initialize a Document.System::SharedPtr<Document> doc = System::MakeObject<Document>(u"document.docx");// Use a document builder to add content to the document.System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);// Get sectionauto section = doc->get_Sections()->idx_get(0);// Get bodyauto body = section->get_Body();// Get first paragraphauto para = body->get_FirstParagraph();// Update textauto run = para->get_Runs()->idx_get(0);run->set_Text(u"This is the updated text.");// Save the document to disk.doc->Save(u"updated_document.docx");

四、使用 C++ 在 Word 文档中插入图像

以下是使用 C++ 在 MS Word 文档中插入图像的步骤。

使用Document类创建一个新的 Word 文档或加载现有文档。创建一个DocumentBuilder对象并使用 Document 对象对其进行初始化。使用DocumentBuilder->InsertImage(String fileName, RelativeHorizontalPosition horzPos, double left, RelativeVerticalPosition vertPos, double top, double width, double height, WrapType wrapType)方法插入图像。将文档另存为 Word 文件。

下面的代码示例显示了如何使用 C++ 将图像插入到 Word 文档中。

System::SharedPtr<Document> doc = System::MakeObject<Document>();System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);// Add a logo to the top left of the page. The image is placed in front of all other text.System::SharedPtr<Shape> shape = builder->InsertImage( u"Aspose Logo.png", RelativeHorizontalPosition::Page, 60.0, RelativeVerticalPosition::Page, 60.0, -1.0, -1.0, WrapType::None);doc->Save(u"document_with_image.docx");

五、使用 C++ 在 Word 文档中插入表格

表格是 Word 文档的重要元素,用于以行和列的形式保存数据。为了在 Word 文档中生成表格,请按照以下步骤操作。

使用Document类创建一个新的 Word 文档。创建表类的对象。使用Document->get_FirstSection()->get_Body()->AppendChild()方法将表格插入文档。使用Row类创建一个新行。使用Table->AppendChild(row)方法将行插入表中。使用Cell->get_FirstParagraph()->AppendChild()方法创建和新建Cell并向其中插入文本。使用Row->AppendChild()方法将单元格插入行。重复添加多行的过程。使用Document->Save()方法保存文档。

以下代码示例显示如何使用 C++ 在 Word 文档中插入表格 。

System::SharedPtr<Document> doc = System::MakeObject<Document>();System::SharedPtr<Table> table = System::MakeObject<Table>(doc);// Add the table to the document.doc->get_FirstSection()->get_Body()->AppendChild(table);System::SharedPtr<Row> row = System::MakeObject<Row>(doc);row->get_RowFormat()->set_AllowBreakAcrossPages(true);table->AppendChild(row);// We can now apply any auto fit settings.table->AutoFit(AutoFitBehavior::FixedColumnWidths);// Create a cell and add it to the rowSystem::SharedPtr<Cell> cell = System::MakeObject<Cell>(doc);cell->get_CellFormat()->get_Shading()->set_BackgroundPatternColor(System::Drawing::Color::get_LightBlue());cell->get_CellFormat()->set_Width(80);// Add a paragraph to the cell as well as a new run with some text.cell->AppendChild(System::MakeObject<Paragraph>(doc));cell->get_FirstParagraph()->AppendChild(System::MakeObject<Run>(doc, u"Row 1, Cell 1 Text"));// Add the cell to the row.row->AppendChild(cell);// We would then repeat the process for the other cells and rows in the table.// We can also speed things up by cloning existing cells and rows.row->AppendChild((System::StaticCast<Node>(cell))->Clone(false));row->get_LastCell()->AppendChild(System::MakeObject<Paragraph>(doc));row->get_LastCell()->get_FirstParagraph()->AppendChild(System::MakeObject<Run>(doc, u"Row 1, Cell 2 Text"));// Save the document to disk.doc->Save(u"document_with_table.docx");

六、使用 C++ 在 Word 文档中添加列表

最后但同样重要的是,在 Word 文档中创建一个列表。以下是创建项目符号列表的步骤。

使用Document类创建一个新的 Word 文档或加载现有文档。定义一个新的DocumentBuilder对象并使用Document对象对其进行初始化。使用DocumentBuilder->get_ListFormat()->set_List(Document->get_Lists()->Add(ListTemplate::NumberArabicDot))方法创建列表。填充列表并设置列表级别。将文档另存为文件。

以下代码示例显示如何使用 C++ 在 Word 文档中创建列表。

System::SharedPtr<Document> doc = System::MakeObject<Document>();System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);// Create a numbered list based on one of the Microsoft Word list templates and// apply it to the current paragraph in the document builder.builder->get_ListFormat()->set_List(doc->get_Lists()->Add(ListTemplate::NumberArabicDot));// There are 9 levels in this list, lets try them all.for (int32_t i = 0; i < 9; i++){builder->get_ListFormat()->set_ListLevelNumber(i);builder->Writeln(System::String(u"Level ") + i);}// Create a bulleted list based on one of the Microsoft Word list templates// and apply it to the current paragraph in the document builder.builder->get_ListFormat()->set_List(doc->get_Lists()->Add(ListTemplate::BulletDiamonds));// There are 9 levels in this list, lets try them all.for (int32_t i = 0; i < 9; i++){builder->get_ListFormat()->set_ListLevelNumber(i);builder->Writeln(System::String(u"Level ") + i);}// This is a way to stop list formatting.builder->get_ListFormat()->set_List(nullptr);// Save the document to disk.builder->get_Document()->Save(u"document_with_list.docx");

以上便是如何使用 C++ 创建 MS Word 文档 (DOC/DOCX) 步骤 ,要是您还有其他关于产品方面的问题,欢迎咨询我们,或者加入我们官方技术交流群。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。