我如何知道单词列表是项目符号列表还是数字列表?
How can I know if a Word list is a bullet list or a numeric list?
我正在阅读一个 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}");
}
例如,这会输出 Header 1、Normal 和 List Paragraph。所以我的问题。我看不出 List Paragraph 是项目符号列表还是数字列表。问题,如何知道列表的类型?
使用 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,
}
区分两个单词列表可能还不够。
我不知道 "Outlined list" 的确切含义,但数字列表和项目符号列表似乎都属于此类。
那么,你能做什么?
选项 1. 您可以使用 Range.ListFormat.ListString
来确定标记列表的文本。它可以是项目符号、数字、三角形或任何在 word 文件中定义的内容。但这不是一个好主意,因为你永远不知道那里存储了什么值,所以你无法比较它。
选项2.可以使用WdListNumberStyle枚举,虽然有点复杂。我会尽力解释。
有一个名为 Range.ListFormat.ListTemplate.ListLevels
的 属性,它存储所有可能级别的列表格式。普通列表具有 1 级格式,嵌套列表分别具有 2 到 9 级格式(似乎您可以在 MS Word 中为嵌套列表定义 9 种不同格式)。因此,您需要获取 Range.ListFormat.ListTemplate.ListLevels
属性 的第一项并检查其 NumberStyle
属性(请参阅上面的 link)。但是,由于 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--;
}
}
我不知道这对问题的作者是否有帮助,但由于我遇到了问题,在寻找解决方案时我来到这里但没有找到我决定 post 我找到的.我不明白为什么它必须如此复杂以及为什么你不能直接从 ListTemplate
.
中获取描述列表样式的值
我正在阅读一个 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}");
}
例如,这会输出 Header 1、Normal 和 List Paragraph。所以我的问题。我看不出 List Paragraph 是项目符号列表还是数字列表。问题,如何知道列表的类型?
使用 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,
}
区分两个单词列表可能还不够。 我不知道 "Outlined list" 的确切含义,但数字列表和项目符号列表似乎都属于此类。
那么,你能做什么?
选项 1. 您可以使用 Range.ListFormat.ListString
来确定标记列表的文本。它可以是项目符号、数字、三角形或任何在 word 文件中定义的内容。但这不是一个好主意,因为你永远不知道那里存储了什么值,所以你无法比较它。
选项2.可以使用WdListNumberStyle枚举,虽然有点复杂。我会尽力解释。
有一个名为 Range.ListFormat.ListTemplate.ListLevels
的 属性,它存储所有可能级别的列表格式。普通列表具有 1 级格式,嵌套列表分别具有 2 到 9 级格式(似乎您可以在 MS Word 中为嵌套列表定义 9 种不同格式)。因此,您需要获取 Range.ListFormat.ListTemplate.ListLevels
属性 的第一项并检查其 NumberStyle
属性(请参阅上面的 link)。但是,由于 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--;
}
}
我不知道这对问题的作者是否有帮助,但由于我遇到了问题,在寻找解决方案时我来到这里但没有找到我决定 post 我找到的.我不明白为什么它必须如此复杂以及为什么你不能直接从 ListTemplate
.