如何在 Aspose 或 Apache Poi 中解析 MS Word 的样式分隔段落?

How to parse style separated paragraphs of MS Word in Aspose or Apache Poi?

ms word 文档有多种样式的段落,通常每个段落只有一种样式,但您可以使用样式分隔工具在一个段落上组合两种或多种样式的文本。那么如何使用 Aspose Words、Apache Poi 或其他工具从根 parahraph 中获取样式分隔段落的子样式和文本内容?

至少在 Office Open XML (*.docx) 中使用样式分隔符 (CtrlAlt输入 ) 只会导致该段落的消失换行符。尽管如此,样式分隔符前后的段落是两个单独的段落,可以这样阅读。

给出以下 Word 文档 WordDocument.docx

以下代码读取所有段落并获取它们的样式。还有一种方法 getIsLineBreakVanished 检查给定段落的换行符是否消失。对于使用样式分隔符 (CtrlAltEnter) 的段落也是如此。

import java.io.FileInputStream;

import org.apache.poi.xwpf.usermodel.*;

public class WordGetParagraphStyles {

 static boolean getIsLineBreakVanished(XWPFParagraph paragraph) {
  boolean result = false;
  if (paragraph.getCTP().getPPr() != null) {
   if (paragraph.getCTP().getPPr().getRPr() != null) {
    if (paragraph.getCTP().getPPr().getRPr().getVanish() != null && paragraph.getCTP().getPPr().getRPr().getSpecVanish() != null) {
     result = true;
    }
   }
  }
  return result;
 }

 public static void main(String[] args) throws Exception {
  XWPFDocument document = new XWPFDocument(new FileInputStream("./WordDocument.docx"));

  for (IBodyElement bodyElement : document.getBodyElements()) {
   if (bodyElement instanceof XWPFParagraph) {
    XWPFParagraph paragraph = (XWPFParagraph)bodyElement;
    String style = paragraph.getStyle();
    String styleID = paragraph.getStyleID();
    String text = paragraph.getText();
    boolean hasCRLF = !getIsLineBreakVanished(paragraph);
    System.out.println("Found paragraph:" + " Style=" + styleID + ":" + style + ", Text=" + text + ", has CRLF=" + hasCRLF);
   }
  }
 }
}

样式分隔符实际上是普通的段落分隔符,但设置了特殊属性。因此,您可以将由样式分隔符分隔的内容视为两个单独的段落。

<w:p w14:paraId="561A87F3" w14:textId="0D47DD82" w:rsidR="00AB32A0" w:rsidRPr="00AB32A0" w:rsidRDefault="00AB32A0" w:rsidP="00AB32A0">
  <w:pPr>
    <w:pStyle w:val="Heading1" />
    <w:rPr>
      <w:vanish />
      <w:specVanish />
    </w:rPr>
  </w:pPr>
  <w:r w:rsidRPr="00AB32A0">
    <w:rPr>
      <w:rStyle w:val="Heading1Char" />
    </w:rPr>
    <w:t>Test heading1</w:t>
  </w:r>
</w:p>
<w:p w14:paraId="0982566B" w14:textId="76E92742" w:rsidR="00391656" w:rsidRDefault="00AB32A0" w:rsidP="00AB32A0">
  <w:r>
    <w:t xml:space="preserve"> test paragraph.</w:t>
  </w:r>
</w:p>

下面两个属性表示段落分隔符是样式分隔符

<w:rPr>
  <w:vanish />
  <w:specVanish />
</w:rPr>

在Aspose.Words中可以通过Paragraph.BreakIsStyleSeparator属性检测段落是否为样式分隔符。

cs:

Document doc = new Document(@"C:\Temp\test.docx");
foreach (Paragraph para in doc.FirstSection.Body.Paragraphs)
{
    Console.WriteLine("Style Name: {0}; Is Style Separator: {1}; Content: {2}", para.ParagraphFormat.StyleName, para.BreakIsStyleSeparator, para.ToString(SaveFormat.Text));
}

java:

Document doc = new Document("C:/Temp/test.docx");
for(Paragraph para : doc.getFirstSection().getBody().getParagraphs()){
   String styleName = para.getParagraphFormat().getStyleName();
   boolean isStyleSeparator = para.getBreakIsStyleSeparator();
   String content = para.toString(SaveFormat.TEXT);
}

披露:我在 Aspose.Words 团队工作。