简体   繁体   English

我怎么知道单词列表是项目符号列表还是数字列表?

[英]How can I know if a Word list is a bullet list or a numeric list?

I am reading a word document (converting it to HTML) and want to know what type each paragraph is (at least that's the way I think I want to do it like). 我正在阅读一个Word文档(将其转换为HTML),并想知道每个段落的类型(至少我认为这是我想要的方式)。

My code looks like this 我的代码看起来像这样

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}");
}

This outputs Header 1 , Normal and List Paragraph for instance. 例如,这将输出Header 1NormalList Paragraph So my question. 所以我的问题。 I can't see if List Paragraph is a bullet list or a numeric list. 我看不到“ 列表段落”是项目符号列表还是数字列表。 Question, How can I know what type the list is of? 问题,如何知道列表的类型?

Use Range.ListFormat.ListType which can have the following values: 使用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,
}

It may not be enough to distinguis two Word lists. 区分两个Word列表可能还不够。 I don't know what exactly means "Outlined list", but it appears that both numeric lists and bulleted lists are in this category. 我不知道“概述列表”的确切含义,但似乎数字列表和项目符号列表都在此类别中。

So, what can you do? 所以,你可以做什么?

Option 1. You can use Range.ListFormat.ListString to determine what text is marking the list. 选项1.您可以使用Range.ListFormat.ListString确定哪些文本标记了列表。 It can be bullet, number, triangle or anything that is defined in word file. 它可以是项目符号,数字,三角形或Word文件中定义的任何内容。 But this is not very good idea because you never know what value is stored there so you can't compare it. 但这不是一个好主意,因为您永远不知道在那里存储了什么值,因此无法进行比较。

Option 2. You can use WdListNumberStyle Enumeration, though it's a little bit complicated. 选项2。您可以使用WdListNumberStyle枚举,尽管有点复杂。 I'll try to explain. 我会尽力解释。 There is property called Range.ListFormat.ListTemplate.ListLevels which stores list formats for all possible levels of list. 有一个名为Range.ListFormat.ListTemplate.ListLevels属性,该属性存储所有可能级别的列表的列表格式。 Usual lists have level 1 format and nested lists have formats from 2 to 9 respectively (seems like you can define 9 different formats for nested lists in MS Word). 常用列表的格式为1级,嵌套列表的格式分别为2到9(似乎您可以在MS Word中为嵌套列表定义9种不同的格式)。 So, what you need is to get first item of Range.ListFormat.ListTemplate.ListLevels property and check its NumberStyle property (see link above). 因此,您需要获取Range.ListFormat.ListTemplate.ListLevels属性的第一项并检查其NumberStyle属性(请参见上面的链接)。 However since ListLevels supports only IEnumerable interface you can't get certain element. 但是,由于ListLevels仅支持IEnumerable接口,因此无法获得某些元素。 You can use something like this: 您可以使用如下形式:

private static Word.WdListNumberStyle GetListType(Word.Range sentence)
{
    foreach (Word.ListLevel lvl in sentence.ListFormat.ListTemplate.ListLevels)
    {
        return lvl.NumberStyle;
    }
}

or, more specificly 或者,更具体地说

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--;
    }
}

I don't know whether it was helpful for the author of question but since I had a problem and while searching for solution I came here and didn't found one I decided to post what I found. 我不知道这对问题的作者是否有帮助,但是由于我遇到问题,在寻找解决方案时,我来到这里,但没有找到解决方案,所以我决定发表我的发现。 I don't why it must be so complicated and why you can't get the value describing list style directly from ListTemplate . 我不明白为什么它必须这么复杂,为什么不能直接从ListTemplate获得描述列表样式的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM