我花了无数个小时试图理解 Open XML 中的有序列表。这是众多参考资料之一。
我在这里找到了这个非常有用的简单文档创建器示例。
另外,如果我可能会抱怨一点,我必须说这是一个痛苦的学习曲线。创建编号属性并引用正确abstractNumberId
的和列表继续。
有没有人有在代码中创建多级列表的完整示例?我可以在没有自定义设置的情况下这样做,就像设置NumberFormatValues.Decimal
. 一旦你想开始控制listType
你就会有很多即时开销。
以我开始的上述示例为例,我添加了一个level
参数:
public void AddBulletList(List<Run> runList, int level)
我将它合并到这一行:
var abstractLevel = new Level(new NumberingFormat() {
Val = NumberFormatValues.Decimal}, new LevelText() {Val = "·"}) {LevelIndex = level};
请注意,我将格式类型更改为十进制...方法名称显示为子弹,但我只是在这里进行测试。
我还利用它来处理缩进:
var indentation = new Indentation() { Left = (720 * (level + 1)).ToString(), Hanging = "360" };
0
所以在我的测试中,我给传入for level的方法发送了几句话。然后我再送几个传球来1
换水平。
我的结果有两个问题:
1)我不知道如何重置计数器,所以我明白了:
1. Sentence 1
2. Sentence 2
3. Sentence 3
4. Sentence 1
5. Sentence 2
6. Sentence 3
我试图使用,levelRestart
但没有奏效:
abstractLevel.LevelRestart = new LevelRestart(){ Val = 0 } // tried 1 also
我可以让它重新启动的唯一方法是在插入第二个句子列表之前插入一个空白段落,但这有样式问题(间距)。
我遇到的第二个问题是编号出现在 WordDoc 中,但它在 Microsoft Word 中显示为项目符号。此外,我在 Microsoft Word 中收到兼容模式警告。
有人会过来说:
使用 Open XML Productivity Tools 并创建一个文档并查看生成的代码
那么我对此的回应是:
看了 5,000 行代码和一个很小的文档来测试创建后,我的眼睛在流血。
所以我觉得我超级接近。我的代码中有很多其他的自定义,这就是为什么我继续参考我开始的地方。如果有人可以举这个例子或提供一个现有的例子,我只想创建多级列表并控制使用的编号类型(lowerRoman、decimal 等)。
更新 1
我真的只能硬着头皮做一些认真的学习。在 Thomas 的帮助下,我能够继续前进,但似乎我还有一个令人讨厌的问题。我的新有序列表不是从“1”开始的。
请注意在图像中,如果我单击第一个列表的级别,它只会突出显示该列表。我希望它们是分开的,但显然不是。下一个列表应该从 1 开始。
对于每个新的有序列表,我都给它分配了一个新的numberId
,所以我不知道发生了什么。这是生成的标记:
<w:numbering xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se w16cid wp14">
<w:abstractNum w:abstractNumId="1" w15:restartNumberingAfterBreak="1">
<w:nsid w:val="191025D9" />
<w:multiLevelType w:val="hybridMultilevel" />
<w:tmpl w:val="48A2E570" />
<w:lvl w:ilvl="0" w:tplc="0409000F">
<w:start w:val="1" />
<w:numFmt w:val="decimal" />
<w:lvlText w:val="%1." />
<w:lvlJc w:val="left" />
<w:pPr>
<w:ind w:start="720" w:hanging="360" />
</w:pPr>
</w:lvl>
<w:lvl w:ilvl="1" w:tplc="04090019">
<w:start w:val="1" />
<w:numFmt w:val="lowerLetter" />
<w:lvlText w:val="%2." />
<w:lvlJc w:val="left" />
<w:pPr>
<w:ind w:start="1440" w:hanging="360" />
</w:pPr>
</w:lvl>
</w:abstractNum>
<w:abstractNum w:abstractNumId="2" w15:restartNumberingAfterBreak="1">
<w:nsid w:val="191025D9" />
<w:multiLevelType w:val="hybridMultilevel" />
<w:tmpl w:val="48A2E570" />
<w:lvl w:ilvl="0" w:tplc="0409000F">
<w:start w:val="1" />
<w:numFmt w:val="decimal" />
<w:lvlText w:val="%1." />
<w:lvlJc w:val="left" />
<w:pPr>
<w:ind w:start="720" w:hanging="360" />
</w:pPr>
</w:lvl>
<w:lvl w:ilvl="1" w:tplc="04090019">
<w:start w:val="1" />
<w:numFmt w:val="lowerLetter" />
<w:lvlText w:val="%2." />
<w:lvlJc w:val="left" />
<w:pPr>
<w:ind w:start="1440" w:hanging="360" />
</w:pPr>
</w:lvl>
</w:abstractNum>
<w:num w:numId="1">
<w:abstractNumId w:val="1" />
</w:num>
<w:num w:numId="2">
<w:abstractNumId w:val="2" />
</w:num>
</w:numbering>
这是身体:
<w:body xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:p>
<w:pPr>
<w:pStyle w:val="ListParagraph" />
<w:numPr>
<w:ilvl w:val="0" />
<w:numId w:val="1" />
</w:numPr>
</w:pPr>
<w:r>
<w:t>List one item 1</w:t>
</w:r>
</w:p>
<w:p>
<w:pPr>
<w:pStyle w:val="ListParagraph" />
<w:numPr>
<w:ilvl w:val="0" />
<w:numId w:val="1" />
</w:numPr>
</w:pPr>
<w:r>
<w:t>List one item 2</w:t>
</w:r>
</w:p>
<w:p>
<w:pPr>
<w:pStyle w:val="ListParagraph" />
<w:numPr>
<w:ilvl w:val="0" />
<w:numId w:val="2" />
</w:numPr>
</w:pPr>
<w:r>
<w:t>List two item 1</w:t>
</w:r>
</w:p>
<w:p>
<w:pPr>
<w:pStyle w:val="ListParagraph" />
<w:numPr>
<w:ilvl w:val="0" />
<w:numId w:val="2" />
</w:numPr>
</w:pPr>
<w:r>
<w:t>List two item 2</w:t>
</w:r>
</w:p>
</w:body>