列表分类是指在Word文档中使用不同格式排序的列表,来帮助我们一目了然地表达出一段文字的主要内容。比如,当我们描述了某个主题的若干点,就可以用列表把它们一一表达出来,而不是写成完整的段落形式。同时,列表也可以帮助我们做出精确的计算和比较,简洁有效地表示出不同部分之间的关系。在Word文档中创建列表可以便于人们去检索资料方便定位,其中总共有四种不同类型的列表:编号列表、项目符号列表、多级编号列表和多级混合类型列表。本文就将详细为您介绍如何使用C++在Word文档中创建编号列表、项目符号列表和多级列表

  • 在Word中创建编号列表
  • 在Word中创建项目符号列表
  • 在Word中创建多级编号列表
  • 在Word中创建多级混合类型列表

安装 Spire.Doc for C++

有两种方法可以将 Spire.Doc for C++ 集成到您的应用程序中。一种方法是通过NuGet安装它,另一种方法是从我们的网站下载包并将库复制到您的程序中。通过 NuGet 安装更简单,更推荐使用。您可以通过访问以下链接找到更多详细信息。

如何将 Spire.Doc for C++ 集成到 C++ 程序中

在Word中创建编号列表

您可以使用ListStyle类创建编号列表样式或项目符号样式。然后,可以使用Paragraph->GetListFormat()->ApplyStyle() 方法将列表样式应用于段落。创建编号列表的步骤如下。

  • 创建一个Document对象。
  • 使用Document->AddSection() 方法添加一个节。
  • 创建ListStyle类的实例,将列表类型指定为Numbered
  • 使用ListStyle->GetLevels()->GetItem(index) 方法获取列表的特定级别,并使用ListLevel->SetPatternType() 方法设置编号类型。
  • 使用Document->GetListStyles()->Add() 方法将列表样式添加到文档中。
  • 使用Section->AddParagraph() 方法将多个段落添加到文档中。
  • 使用Paragraph->GetListFormat()->ApplyStyle() 方法将列表样式应用于特定段落。
  • 使用Paragraph->GetListFormat()->GetListLevelNumber() 方法指定列表级别。
  • 使用Document->SaveToFile() 方法将文档保存到Word文件中。

完整代码

C++

#include "Spire.Doc.o.h";

using namespace Spire::Doc;
using namespace std; int main() { //创建一个Document对象
intrusive_ptr<Document> document = new Document(); //添加一个节
intrusive_ptr<Section> section = document->AddSection(); //创建编号列表样式
intrusive_ptr<ListStyle> listStyle = new ListStyle(document, ListType::Numbered);
listStyle->SetName(L"numberedList");
listStyle->GetLevels()->GetItem(0)->SetPatternType(ListPatternType::DecimalEnclosedParen);
listStyle->GetLevels()->GetItem(0)->SetTextPosition(20);
document->GetListStyles()->Add(listStyle); //添加一个段落
intrusive_ptr<Paragraph> paragraph = section->AddParagraph();
paragraph->AppendText(L"完整的论证要素:");
paragraph->GetFormat()->SetAfterSpacing(5); //添加段落并对其应用编号列表样式
paragraph = section->AddParagraph();
paragraph->AppendText(L"论题");
paragraph->GetListFormat()->ApplyStyle(L"numberedList");
paragraph->GetListFormat()->SetListLevelNumber(0); //再添加四个段落,并将编号列表样式应用于特定段落
paragraph = section->AddParagraph();
paragraph->AppendText(L"论点");
paragraph->GetListFormat()->ApplyStyle(L"numberedList");
paragraph->GetListFormat()->SetListLevelNumber(0); paragraph = section->AddParagraph();
paragraph->AppendText(L"论据");
paragraph->GetListFormat()->ApplyStyle(L"numberedList");
paragraph->GetListFormat()->SetListLevelNumber(0); paragraph = section->AddParagraph();
paragraph->AppendText(L"论证方式");
paragraph->GetListFormat()->ApplyStyle(L"numberedList");
paragraph->GetListFormat()->SetListLevelNumber(0); //将文档保存为Word文件
document->SaveToFile(L"FE编号列表.docx", FileFormat::Docx2019);
document->Dispose();
}

效果图

在Word中创建项目符号列表

创建项目符号列表的过程与创建编号列表的过程类似。不同之处在于,创建列表样式时,必须将列表类型指定为“项目符号”,并为其设置项目符号。以下是详细步骤。

  • 创建一个Document对象。
  • 使用Document->AddSection() 方法添加一个节。
  • 创建ListStyle类的实例,将列表类型指定为“Bulleted”。
  • 使用ListStyle->GetLevels()->Get(index) 方法获取列表的特定级别,并使用ListLevel->SetBulletCharacter() 方法设置项目符号。
  • 使用Document->GetListStyles()->Add() 方法将列表样式添加到文档中。
  • 使用Section->AddParagraph() 方法将多个段落添加到文档中。
  • 使用Paragraph->GetListFormat()->ApplyStyle() 方法将列表样式应用于特定段落。
  • 使用Paragraph->GetListFormat()->SetListLevelNumber() 方法指定列表级别。
  • 使用Document->SaveToFile() 方法将文档保存到Word文件中。

完整代码

C++

#include "Spire.Doc.o.h";

using namespace Spire::Doc;
using namespace std; int main() { //创建一个Document对象
intrusive_ptr<Document> document = new Document(); //添加一个节
intrusive_ptr<Section> section = document->AddSection(); //创建项目符号列表样式
intrusive_ptr<ListStyle> listStyle = new ListStyle(document, ListType::Bulleted);
listStyle->SetName(L"bulletedList");
listStyle->GetLevels()->GetItem(0)->SetBulletCharacter(L"\u00B7");
listStyle->GetLevels()->GetItem(0)->GetCharacterFormat()->SetFontName(L"Symbol");
listStyle->GetLevels()->GetItem(0)->SetTextPosition(20);
document->GetListStyles()->Add(listStyle); //添加一个段落
intrusive_ptr<Paragraph> paragraph = section->AddParagraph();
paragraph->AppendText(L"常用的六种论证方法:");
paragraph->GetFormat()->SetAfterSpacing(5); //添加段落并对其应用项目符号列表样式
paragraph = section->AddParagraph();
paragraph->AppendText(L"举例论证");
paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
paragraph->GetListFormat()->SetListLevelNumber(0); //再添加五个段落,并将项目符号列表样式应用于特定段落
paragraph = section->AddParagraph();
paragraph->AppendText(L"道理论证");
paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
paragraph->GetListFormat()->SetListLevelNumber(0); paragraph = section->AddParagraph();
paragraph->AppendText(L"对比论证");
paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
paragraph->GetListFormat()->SetListLevelNumber(0); paragraph = section->AddParagraph();
paragraph->AppendText(L"比喻论证");
paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
paragraph->GetListFormat()->SetListLevelNumber(0); paragraph = section->AddParagraph();
paragraph->AppendText(L"引用论证");
paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
paragraph->GetListFormat()->SetListLevelNumber(0); paragraph = section->AddParagraph();
paragraph->AppendText(L"因果论证");
paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
paragraph->GetListFormat()->SetListLevelNumber(0); //保存结果文档
document->SaveToFile(L"FE项目符号列表.docx", FileFormat::Docx2019);
document->Dispose();
}

效果图

在Word中创建多级编号列表

多级列表至少由两个不同的级别组成。嵌套列表的每个级别都可以使用ListStyle->GetLevels()->GetItem(index) 方法进行访问。通过ListLevel对象,您可以设置某个级别的编号类型和前缀。以下是在Word中创建多级编号列表的步骤。

  • 创建一个Document对象。
  • 使用Document->AddSection() 方法添加一个节。
  • 创建ListStyle类的实例,将列表类型指定为Numbered
  • 使用ListStyle->GetLevels()->GetItem(index) 方法获取列表的特定级别,并设置编号类型和前缀。
  • 使用Document->GetListStyles()->Add() 方法将列表样式添加到文档中。
  • 使用Section->AddParagraph() 方法将多个段落添加到文档中。
  • 使用Paragraph->GetListFormat()->ApplyStyle() 方法将列表样式应用于特定段落。
  • 使用Paragraph->GetListFormat()->SetListLevelNumber() 方法指定列表级别。
  • 使用Document->SaveToFile() 方法将文档保存到Word文件中。

完整代码

C++

#include "Spire.Doc.o.h";

using namespace Spire::Doc;
using namespace std; int main() { //创建一个Document对象
intrusive_ptr<Document> document = new Document(); //添加一个节
intrusive_ptr<Section> section = document->AddSection(); //创建编号列表样式,指定每个级别的编号前缀和图案类型
intrusive_ptr<ListStyle> listStyle = new ListStyle(document, ListType::Numbered);
listStyle->SetName(L"nestedStyle");
listStyle->GetLevels()->GetItem(0)->SetPatternType(ListPatternType::Arabic);
listStyle->GetLevels()->GetItem(0)->SetTextPosition(20);
listStyle->GetLevels()->GetItem(1)->SetNumberPrefix(L"%1.");
listStyle->GetLevels()->GetItem(1)->SetPatternType(ListPatternType::Arabic);
listStyle->GetLevels()->GetItem(2)->SetNumberPrefix(L"%1.%2.");
listStyle->GetLevels()->GetItem(2)->SetPatternType(ListPatternType::Arabic);
document->GetListStyles()->Add(listStyle); //添加一个段落
intrusive_ptr<Paragraph> paragraph = section->AddParagraph();
paragraph->AppendText(L"这是一个多级编号列表:");
paragraph->GetFormat()->SetAfterSpacing(5); //添加段落并对其应用编号列表样式
paragraph = section->AddParagraph();
paragraph->AppendText(L"水果");
paragraph->GetListFormat()->ApplyStyle(L"nestedStyle");
paragraph->GetListFormat()->SetListLevelNumber(0); //再添加五个段落,并将编号列表样式应用于特定段落
paragraph = section->AddParagraph();
paragraph->AppendText(L"蔬菜");
paragraph->GetListFormat()->ApplyStyle(L"nestedStyle");
paragraph->GetListFormat()->SetListLevelNumber(0); paragraph = section->AddParagraph();
paragraph->AppendText(L"根菜类");
paragraph->GetListFormat()->ApplyStyle(L"nestedStyle");
paragraph->GetListFormat()->SetListLevelNumber(1); paragraph = section->AddParagraph();
paragraph->AppendText(L"叶菜类");
paragraph->GetListFormat()->ContinueListNumbering();
paragraph->GetListFormat()->ApplyStyle(L"nestedStyle"); paragraph = section->AddParagraph();
paragraph->AppendText(L"小白菜");
paragraph->GetListFormat()->ApplyStyle(L"nestedStyle");
paragraph->GetListFormat()->SetListLevelNumber(2); paragraph = section->AddParagraph();
paragraph->AppendText(L"谷物");
paragraph->GetListFormat()->ApplyStyle(L"nestedStyle");
paragraph->GetListFormat()->SetListLevelNumber(0); //保存结果文档
document->SaveToFile(L"FE多级编号列表.docx", FileFormat::Docx2019);
document->Dispose();
}

效果图

在Word中创建多级混合类型列表

多级列表可以是编号列表和项目符号列表的组合。要创建混合类型列表,只需要创建编号列表样式和项目符号列表样式,并将它们应用于不同的段落。具体步骤如下。

  • 创建一个Document对象。
  • 使用Document->AddSection() 方法添加一个节。
  • 创建编号列表样式和项目符号列表样式。
  • 使用Section->AddParagraph() 方法将多个段落添加到文档中。
  • 使用Paragraph->GgetListFormat()->ApplyStyle() 方法将不同的列表样式应用于不同的段落。
  • 使用Document->SaveToFile() 方法将文档保存到Word文件中。

完整代码

C++

#include "Spire.Doc.o.h";

using namespace Spire::Doc;
using namespace std; int main() { //创建一个Document对象
intrusive_ptr<Document> document = new Document(); //添加一个节
intrusive_ptr<Section> section = document->AddSection(); //创建编号列表样式
intrusive_ptr<ListStyle> numberedListStyle = new ListStyle(document, ListType::Numbered);
numberedListStyle->SetName(L"numberedStyle");
numberedListStyle->GetLevels()->GetItem(0)->SetPatternType(ListPatternType::Arabic);
numberedListStyle->GetLevels()->GetItem(0)->SetTextPosition(20);
numberedListStyle->GetLevels()->GetItem(1)->SetPatternType(ListPatternType::LowLetter);
document->GetListStyles()->Add(numberedListStyle); //创建项目符号列表样式
intrusive_ptr<ListStyle> bulletedListStyle = new ListStyle(document, ListType::Bulleted);
bulletedListStyle->SetName(L"bulletedStyle");
bulletedListStyle->GetLevels()->GetItem(2)->SetBulletCharacter(L"\u002A");
bulletedListStyle->GetLevels()->GetItem(2)->GetCharacterFormat()->SetFontName(L"Symbol");
document->GetListStyles()->Add(bulletedListStyle); //添加段落
intrusive_ptr<Paragraph> paragraph = section->AddParagraph();
paragraph->AppendText(L"这是一个多级混合列表:");
paragraph->GetFormat()->SetAfterSpacing(5); //添加段落并对其应用编号列表样式
paragraph = section->AddParagraph();
paragraph->AppendText(L"水果");
paragraph->GetListFormat()->ApplyStyle(L"numberedStyle");
paragraph->GetListFormat()->SetListLevelNumber(0); //再添加五个段落,并对其应用不同的列表样式
paragraph = section->AddParagraph();
paragraph->AppendText(L"瓜果类");
paragraph->GetListFormat()->ApplyStyle(L"numberedStyle");
paragraph->GetListFormat()->SetListLevelNumber(1); paragraph = section->AddParagraph();
paragraph->AppendText(L"浆果类");
paragraph->GetListFormat()->SetListLevelNumber(1);
paragraph->GetListFormat()->ApplyStyle(L"numberedStyle"); paragraph = section->AddParagraph();
paragraph->AppendText(L"蔓越莓");
paragraph->GetListFormat()->ApplyStyle(L"bulletedStyle");
paragraph->GetListFormat()->SetListLevelNumber(2); paragraph = section->AddParagraph();
paragraph->AppendText(L"覆盆子");
paragraph->GetListFormat()->ApplyStyle(L"bulletedStyle");
paragraph->GetListFormat()->SetListLevelNumber(2); paragraph = section->AddParagraph();
paragraph->AppendText(L"蔬菜");
paragraph->GetListFormat()->ApplyStyle(L"numberedStyle");
paragraph->GetListFormat()->SetListLevelNumber(0); //保存结果文档
document->SaveToFile(L"FE多级混合类型列表.docx", FileFormat::Docx);
document->Dispose();
}

效果图

—本文完—

如何使用C++ 在Word文档中创建列表的更多相关文章

  1. C# Word文档中插入、提取图片,文字替换图片

    Download Files:ImageOperationsInWord.zip 简介 在这篇文章中我们可以学到在C#程序中使用一个Word文档对图像的各种操作.图像会比阅读文字更有吸引力,而且图像是 ...

  2. C# 在Word文档中生成条形码

    C# 在Word文档中生成条形码 简介 条形码是由多个不同的空白和黑条按照一定的顺序组成,用于表示各种信息如产品名称.制造商.类别.价格等.目前,条形码在我们的日常生活中有着很广泛的应用,不管是在图书 ...

  3. C# 提取Word文档中的图片

    C# 提取Word文档中的图片 图片和文字是word文档中两种最常见的对象,在微软word中,如果我们想要提取出一个文档内的图片,只需要右击图片选择另存为然后命名保存就可以了,今天这篇文章主要是实现使 ...

  4. 把word文档中的所有图片导出

    把word文档中的所有图片导出 end

  5. aspose.words复制插入同一word文档中的某个页面

    选择word模板 Document doc = new Document(Server.MapPath("~\\templet") + "\\" + name. ...

  6. Aspose.Words:如何添加另一个WORD文档中的Node对象

    原文:Aspose.Words:如何添加另一个WORD文档中的Node对象 首先看一段代码,这段代码意图从docSource中获取第一个表格,并插入docTarget的末尾: , true); doc ...

  7. C# 在word文档中复制表格并粘帖到下一页中

    C# 在word文档中复制表格并粘帖到下一页中 object oMissing = System.Reflection.Missing.Value;            Microsoft.Offi ...

  8. 使用Java POI来选择提取Word文档中的表格信息

    通过使用Java POI来提取Word(1992)文档中的表格信息,其中POI支持不同的ms文档类型,在具体操作中需要注意.本文主要是通过POI来提取微软2003文档中的表格信息,具体code如下(事 ...

  9. 处理Word文档中所有修订

    打开现有文档进行编辑 若要打开现有文档,您可以将 Word类实例化,如以下 using 语句所示. 为此,您可以使用Open(String, Boolean) 方法打开具有指定 fileName 的字 ...

  10. 如何批量删除word文档中的超级链接?

    有时候从网页上copy来的文章中,会带有非常多的链接,这些链接很烦人是吧?如何批量删除(一次性全部删除)word文章中的超链接呢? 有些朋友说,Ctrl+A全选文章,然后点击格式工具栏上的“清除格式” ...

随机推荐

  1. 声网Agora 实时音视频服务正式上线 HTC VIVE Sync App,支持非 VR 用户

    全球实时互动云服务开创者和引领者声网Agora(纳斯达克股票代码:API)宣布其视频 SDK 现已集成到领先的 VR/XR 远程协作及会议应用 HTC VIVE Sync App 中. 通过集成声网A ...

  2. Why WebRTC丨前世今生

    前言 近几年实时音视频通信应用呈现出了大爆发的趋势.在这些实时通信技术的背后,有一项不得不提的技术--WebRTC. 今年 1 月,WebRTC 被 W3C 和 IETF 发布为正式标准.据调研机构 ...

  3. 拒绝“爆雷”!GaussDB(for MySQL)新上线了这个功能

    摘要:智能把控大数据量查询,防患系统奔溃于未然. 本文分享自华为云社区<拒绝"爆雷"!GaussDB(for MySQL)新上线了这个功能>,作者:GaussDB 数据 ...

  4. NEFU-NSILAB2021选拔赛WriteUp

    Web signin 打开看到源码: <?php highlight_file(__FILE__); $file = $_GET['file']; if ($file) { include $f ...

  5. CTF-RE-学习记录-汇编

    八进制运算 加法表 1+1=2 1+2=3 2+2=4 1+3=4 2+3=5 3+3=6 1+4=5 2+4=6 3+4=7 4+4=10 1+5=6 2+5=7 3+5=8 4+5=11 5+5= ...

  6. C#自定义事件(简单版本)

    C#中的事件分为两种:一种是厂商微软在VS中已经内置,以供用户使用:另一种是有用户自己定义的事件: 先简单回顾下第一种: [场景1]一个Form上一个Textbox控件和Button控件,当用户按下B ...

  7. Jmix 如何将外部数据直接显示在界面?

    企业级应用中,通常一个业务系统并不是孤立存在的,而是需要与企业.部门或者是外部的已有系统进行集成.一般而言,系统集成的数据和接口交互方式通常有以下几种: 文件传输:通过文件传输的方式将数据传递给其他系 ...

  8. [数据库/MySQL]数据库备份与升级:MySQL Percona(RPM) 5.7.24-27 升级到 5.7.31-34

    1 数据库升级方式:RPM包方式升级 [亲测有效] 环境 OS: CENTOS 7 DB: MYSQL 5.7.24-27 1.1 数据库备份 备份以防止升级失败 备份数据库的2个主要方法: 1)用M ...

  9. [Linux]CentOS7 安装指定版本软件包

    以安装openssl-libs为例. 查看当前服务器中YUM源可安装的软件包版本 [root@iz2vc84t88x94kno0u49zwz ~]# yum list | grep openssl-l ...

  10. 进程,Process模块,join方法,ipc机制,守护进程

    多道技术: """ 在学习并发编程的过程中 不做刻意提醒的情况下 默认一台计算机就一个CPU(只有一个干活的人) """ 单道技术 所有的程 ...