如何计算Word文档中段落第一行的左缩进?

发布于 2024-10-07 05:24:23 字数 54 浏览 0 评论 0原文

如果我直接读取DOCX文件,如何确定段落第一行的左缩进?它往往很奇怪,尤其是在项目符号列表中。

If I am reading a DOCX file directly, how do I determine the left indent of the first line of a paragraph? It tends to be very weird, especially if in a bulleted list.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乜一 2024-10-14 05:24:23

这也发布在 DOCX 中的缩进定位 (它的格式比我在这里可以做的要好得多)。

首先,要获得任何属性(包括缩进)的最终状态,我们需要遵循规范第 §2.7.2 节中的样式层次结构规则。

其中,它正确地指出直接格式(p 元素下的 pPr)取代了列表定义中的版本

由于制表位是附加的,我们只需将它们全部收集起来,我们就有了完整的制表位集合

。位于同一位置的选项卡(样式、列表)。

现在,我们按如下方式显示列表:

首先,检查 numFmt 元素§2.9.18 以查看列表定义中任何数字的格式。

接下来,将文本放置在 lvlText 元素 §2.9.12 中最后一个左缩进的位置处。

如果 numFmt 不是“bullet”,我们需要适当地替换 %[1-9] 语法。

现在,查看 suff 元素 §2.9.30,看看是什么将项目符号与文本分开。

如果是空格或什么都没有,则添加一个空格或什么也不添加。

如果是选项卡,则添加一个选项卡。 (注意:如果未设置 suff,这是默认值!)

然后我们只需要知道下一个制表位是什么。

为此,显示级别文本末尾之后的任何制表符停止位均有效:

使用 tab 元素设置的制表符

任何悬挂缩进(只要未设置 doNotUseIndentAsNumberingTabStop 元素 §2.15.3.2)

默认制表符停止位置由defaultTabStop 元素 §2.15.1.24

最后,我们调整结果 - 从关卡文本的开头到行尾。您现在就可以调整它,使其始终相对于相同的制表位。

还有一件事 - 使用悬挂选项卡仅出现在 Word 2007+(即 2007、2010 及更高版本)中 - 并且它是 2007+ 中的默认设置,这使得它有点奇怪:

对于 Word xml 文件,Word 2003 和的默认行为2007+是省略虚拟悬挂选项卡(如果Word 2007+,我们有一个选项让Word启用悬挂选项卡)

对于在Word 2003中打开的普通Word 2003文档文件,虚拟悬挂选项卡总是被省略。

对于在 Word 2007+ 中打开的普通 Word 2003 文档文件,Word 2007+ 将自动选择“不使用悬挂缩进作为项目符号和编号的制表位”选项,并忽略悬挂缩进。

对于在 Word 2007+ 中打开的普通 Word 2007+ docx 文件,Word 2007+ 默认情况下将使用悬挂缩进作为项目符号和编号的选项卡顶部。

对于在 Word 2003 中打开的普通 Word 2007+ docx 文件,Word 2003 将省略虚拟悬挂选项卡,因此可能会导致文档未对齐。 (请参阅知识库http://support.microsoft.com/kb/937936

但是还有另一个关于虚拟悬挂缩进的警告 - 在 RTF/DOC/WordML 中(注意此规则不适用于 DOCX),使用虚拟悬挂缩进制表位 - 当且仅当 - 列表、段落或列表中没有设置制表位虚拟位置之后的样式(列表/段落样式)。

This is also posted at Indent Positioning in DOCX (where it has much better formatting than what I could do here).

First, to get the final state of any property (including indents), we need to follow the style hierarchy rules in §2.7.2 of the specification

There, it correctly states that the direct formatting (the pPr under the p element) supersedes the version in the list definition

Since tab stops are additive, we just gather them all up and we have the full set of tab stops

If there is a then that tab is cleared and not only does not count as a tab, but clears out any parent tabs (style, list) that are at the same position.

Now, we display the list as follows:

First, check the numFmt element §2.9.18 to see the format of any numbers in the list definition.

Next, place the text in the lvlText element §2.9.12 at the location of the final left indent.

If the numFmt isn't "bullet", we need to replace the %[1-9] syntax appropriately.

Now, look at the suff element §2.9.30 to see what separates the bullet from the text.

If it's space or nothing, add a space or nothing.

If it's tab, add a tab. (Note: this is the default if suff is not set!)

Then we just need to know what the next tab stop is.

For that, any tab stop after the end of the displayed level text is valid:

Tabs set with the tab element

Any hanging indent (as long as the doNotUseIndentAsNumberingTabStop element §2.15.3.2 is not set)

Default tab stops at the distances set by the defaultTabStop element §2.15.1.24

Finally, we justify the result - from start of the level text to end of the line. You justify now so that it's always relative to the same tab stops.

One more thing - the use hanging tab only occurs in Word 2007+ (ie 2007, 2010 & later) - and it's the default in 2007+ which makes it sort of weird:

For Word xml file, the default behavior of Word 2003 and 2007+ is to omit the virtual hanging tab (If Word 2007+, we have an option to make Word enable the hanging tab)

For normal Word 2003 doc files opened in Word 2003, the virtual hanging tab is always omitted.

For normal Word 2003 doc files opened in Word 2007+, Word 2007+ will automatically set the option "Don't use hanging indent as tab stop for bullets and numbering" selected, and omit the hanging indention.

For normal Word 2007+ docx files opened in Word 2007+, Word 2007+ will use hanging indent as tab top for bullets and numbering by default.

For normal Word 2007+ docx files opened in Word 2003, Word 2003 will omit the virtual hanging tab, and therefore, may misaligned the document. (see KB http://support.microsoft.com/kb/937936)

But there is another caveat on the virtual hanging indent - in RTF/DOC/WordML (note this rule does not apply to DOCX) the virtual hanging indent tab stop is used - if and only if - there are no tab stops set in the list, paragraph, or styles (list/paragraph style) after the virtual position.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文