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

问题描述 投票:1回答:2

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

java apache-poi apache-tika aspose aspose.words
2个回答
2
投票

样式分隔符其实就是普通的段落分隔符,但是设置了特殊的属性。所以你可以把被样式分隔符分隔的内容看作是两个独立的段落。

<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属性来检测段落中断是否是样式分隔符。

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

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

cs: java.Words.Aspose.Words

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团队工作。


2
投票

至少在 Office Open XML (*.docx) 使用样式分隔符(Ctrl祭祀进入)只能导致该段的虚线断行。然而,样式分隔符前后的段落是两个独立的段落,可以这样理解。

给出以下内容 Word 文件 WordDocument.docx:

enter image description here

下面的代码读取所有段落并获得它们的样式。还有一个方法 getIsLineBreakVanished 它检查给定段落的换行符是否消失。这对于样式分隔符(Ctrl祭祀进入)被使用。

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);
   }
  }
 }
}
© www.soinside.com 2019 - 2024. All rights reserved.