Word处理控件Aspose.Words功能演示:使用 C++ 创建 MS Word 文档 (DOC/DOCX)
Aspose.Words 是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,
Aspose API支持流行文件格式处理,并允许将各类文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。
Aspose.words 最新下载(761297826)https://www.evget.com/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 text builder->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 section auto section = doc->get_Sections()->idx_get(0); // Get body auto body = section->get_Body(); // Get first paragraph auto para = body->get_FirstParagraph(); // Update text auto 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 row System::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) 步骤 ,要是您还有其他关于产品方面的问题,欢迎咨询我们,或者加入我们官方技术交流群。
Word处理控件Aspose.Words功能演示:使用 C++ 创建 MS Word 文档 (DOC/DOCX)相关推荐
- Word处理控件Aspose.Words功能演示:使用 C++ 在 Word (DOC/DOCX) 中添加或删除水印
Aspose.Words 是一种高级Word文档处理API,用于执行各种文档管理和操作任务.API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word.此 ...
- Word处理控件Aspose.Words功能演示:用Java从Word文档中提取文本
Aspose.Words For .NET是一种高级Word文档处理API,用于执行各种文档管理和操作任务.API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsof ...
- Word处理控件Aspose.Words功能演示:使用 C# 在 Word 文档中创建和修改 VBA 宏
Aspose.Words 是一种高级Word文档处理API,用于执行各种文档管理和操作任务.API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word.此 ...
- Word处理控件Aspose.Words功能演示:在 Python 中将 Word DOCX 或 DOC 转换为 PDF
Word 到PDF是最流行和执行最广泛的文档转换之一.DOCX或DOC文件在打印或共享之前会转换为 PDF 格式.在本文中,我们将在 Python 中自动将 Word 转换为 PDF.步骤和代码示例将 ...
- Word处理控件Aspose.Words功能演示:在Java中将Word文档以邮件形式发送
在大多数情况下,电子邮件按照特定模板以格式正确的布局发送.但是,各种电子邮件编辑器不提供增强的格式选项.在这种情况下,可以在Word文档中创建一条消息,并将其用作电子邮件正文.在本文中,将学习如何使用 ...
- Word处理控件Aspose.Words功能演示:在 Java 中将 Word DOC/DOCX 转换为 PDF
Aspose.Words是一种高级Word文档处理API,用于执行各种文档管理和操作任务.API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word. A ...
- Word处理控件Aspose.Words功能演示:使用 C# 将 Word 文档转换为 HTML
在各种情况下,您需要在 Web 或桌面应用程序中显示 Word 文档的内容.在这种情况下,合适的选项之一是将 Word 文档转换为HTML.为了在 .NET 应用程序中实现这一点,本文介绍了如何使用 ...
- Word处理控件Aspose.Words功能演示:使用 Java 将 Word 文档转换为 Markdown
大多数智能设备,如智能手机.平板电脑.笔记本电脑等,都支持EPUB格式来查看或阅读文档.它是电子书或电子出版物的常用格式.另一方面,MS Word 格式,如DOCX.DOC等,是数字世界中广泛使用的文 ...
- Word处理控件Aspose.Words功能演示:使用 C# 将 Word 文档转换为 Markdown
如今,大量的文章.博客和文档都是以Markdown ( MD ) 格式编写的.但是,对于大型文档,Markdown 语法通常变得难以记忆和编写.为方便起见,您可以在 MS Word 中编写内容,然后将 ...
- Word处理控件Aspose.Words功能演示:使用 Java 为 Word 文档添加水印
为防止非法使用.定义所有权.显示文档状态等多种目的,在 Word 文档中添加水印.在本文中,您将学习如何使用 Java 为 Word 文档添加水印.文章将分别演示如何将文字和图片水印插入到Word文档 ...
最新文章
- jupyter % 符号用法
- NUC1312 Sum【水题+数学题】
- GitHub 近 100,000 程序员“起义”:向“996”开炮!
- 深度学习——02、深度学习入门——卷积神经网络
- spring-boot (三) spring data jpa
- “约见”面试官系列之常见面试题之第五十一篇之CSS Sprites(建议收藏)
- python网络编程知识点_python 网络编程要点
- 如何实现input输入框自带清除按钮
- BootStrap笔记-文本颜色链接颜色背景颜色
- .net remoting与web service的区别
- Linux目录下有剩余空间,但无法写入数据
- element-UI 表单校验失效处理
- python读取XML中bndbox和object name的方法
- 2012Google校园招聘笔试题
- android关机铃声代码,android系统添加关机铃声
- 中国省市区列表MySQLl数据库脚本
- 向iPhone模拟器中添加视频
- (官网)虚幻3--基础游戏快速入门
- 【JZOJ100209】【20190705】狂妄之人
- iOS - MVC框架
热门文章
- 信息学奥赛一本通2031:[例4.17]四位完全平方数
- linux找不到网络打印机驱动程序,添加网络打印机时提示找不到驱动程序
- python牛顿法解非线性方程组_利用python求非线性方程
- c语言编程undeclared,c语言中undeclared identifier是什么意思?
- 圣彼得堡大帝理工学院有没有计算机专业,【圣彼得堡彼得大帝理工大学-硕士阶段专业一览】...
- 【安信可LoRa模组专题②】快速搭建远程控制灯LoRa智能灯控【51单片机篇】
- mysql存带表情的微信昵称_MySQL保存 emoji 表情(微信昵称表情)
- 某高校的R语言数据分析期末作业
- 拼多多打造爆款的技巧|耀之阳电商
- Win10+VS2019安装vcpkg