我如何知道单词列表是项目符号列表还是数字列表
本文关键字:列表 符号 数字 项目 单词 何知道 | 更新日期: 2023-09-27 18:35:13
我正在阅读一个word文档(将其转换为HTML),并想知道每个段落的类型(至少我认为我想
这样做的方式)。我的代码看起来像这样
Application application = new Application();
var doc = application.Documents.Open("D:''myDoc.docx");
for (int i = 0; i < doc.Paragraphs.Count; i++)
{
Console.WriteLine($"{doc.Paragraphs[i + 1].Range.ParagraphStyle.NameLocal}");
}
例如,这将输出标题 1、普通和列表段落。所以我的问题。我看不出列表段落是项目符号列表还是数字列表。问题,我怎么知道列表是什么类型?
使用可以具有以下值的Range.ListFormat.ListType
:
// Summary:
// Specifies a type of list.
public enum WdListType
{
// Summary:
// List with no bullets, numbering, or outlining.
wdListNoNumbering = 0,
//
// Summary:
// ListNum fields that can be used in the body of a paragraph.
wdListListNumOnly = 1,
//
// Summary:
// Bulleted list.
wdListBullet = 2,
//
// Summary:
// Simple numeric list.
wdListSimpleNumbering = 3,
//
// Summary:
// Outlined list.
wdListOutlineNumbering = 4,
//
// Summary:
// Mixed numeric list.
wdListMixedNumbering = 5,
//
// Summary:
// Picture bulleted list.
wdListPictureBullet = 6,
}
两个单词列表分开可能还不够。我不知道"大纲列表"的确切含义,但似乎数字列表和项目符号列表都属于此类别。
那么,你能做什么呢?
选项 1.您可以使用Range.ListFormat.ListString
来确定标记列表的文本。它可以是项目符号,数字,三角形或word文件中定义的任何内容。但这不是一个很好的主意,因为你永远不知道那里存储了什么值,所以你无法比较它。
选项 2.您可以使用 WdListNumberStyle 枚举,尽管它有点复杂。我会尝试解释。有一个名为 Range.ListFormat.ListTemplate.ListLevels
的属性,它存储所有可能的列表级别的列表格式。通常的列表具有1级格式,嵌套列表的格式分别为2到9(似乎您可以在MS Word中为嵌套列表定义9种不同的格式)。因此,您需要的是获取Range.ListFormat.ListTemplate.ListLevels
属性的第一项并检查其NumberStyle
属性(请参阅上面的链接)。但是,由于ListLevels
仅支持IEnumerable
接口,因此您无法获取某些元素。你可以使用这样的东西:
private static Word.WdListNumberStyle GetListType(Word.Range sentence)
{
foreach (Word.ListLevel lvl in sentence.ListFormat.ListTemplate.ListLevels)
{
return lvl.NumberStyle;
}
}
或者,更具体地说
private static Word.WdListNumberStyle GetListType(Word.Range sentence, byte level)
{
foreach (Word.ListLevel lvl in sentence.ListFormat.ListTemplate.ListLevels)
{
if (level == 1)
return lvl.NumberStyle;
level--;
}
}
我不知道它是否对问题的作者有帮助,但由于我遇到了问题,在寻找解决方案时,我来到这里但没有找到,我决定发布我发现的内容。我不知道为什么它一定如此复杂,以及为什么你不能直接从ListTemplate
获得描述列表样式的值。