Ms word 如何使用ApachePOI从MS word文档的文本框中获取文本?
我想在MS word文档的文本框中获取信息。我正在使用ApachePOI解析word文档 目前我正在遍历所有段落对象,但此段落列表不包含TextBox中的信息,因此我在输出中缺少此信息 e、 gMs word 如何使用ApachePOI从MS word文档的文本框中获取文本?,ms-word,document,apache-poi,Ms Word,Document,Apache Poi,我想在MS word文档的文本框中获取信息。我正在使用ApachePOI解析word文档 目前我正在遍历所有段落对象,但此段落列表不包含TextBox中的信息,因此我在输出中缺少此信息 e、 g 纯文本段落 **** 再加一段纯文本 我要提取的内容: <para>paragraph in plain text</para> <text_box>some information in text box</text_box> <para&g
纯文本段落
****
再加一段纯文本
我要提取的内容:
<para>paragraph in plain text</para>
<text_box>some information in text box</text_box>
<para>one more paragraph in plain text</para>
纯文本段落
文本框中的某些信息
再加一段纯文本
我目前得到的信息:
<para>paragraph in plain text</para>
<text_box>some information in text box</text_box>
<para>one more paragraph in plain text</para>
纯文本段落
再加一段纯文本
有人知道如何使用Apache POI从文本框中提取信息吗?如果您想从docx文件中的文本框中获取文本(使用POI 3.10-FINAL),下面是示例代码:
FileInputStream fileInputStream = new FileInputStream(inputFile);
XWPFDocument document = new XWPFDocument(OPCPackage.open(fileInputStream));
for (XWPFParagraph xwpfParagraph : document.getParagraphs()) {
String text = xwpfParagraph.getParagraphText(); //here is where you receive text from textbox
}
或者您可以迭代每个
XWPFParagraph中的XWPFRun并调用toString()方法。相同的结果。如果您想从docx文件中的文本框中获取文本(使用POI 3.10-FINAL),以下是示例代码:
FileInputStream fileInputStream = new FileInputStream(inputFile);
XWPFDocument document = new XWPFDocument(OPCPackage.open(fileInputStream));
for (XWPFParagraph xwpfParagraph : document.getParagraphs()) {
String text = xwpfParagraph.getParagraphText(); //here is where you receive text from textbox
}
或者您可以迭代每个
XWPFParagraph中的XWPFRun并调用toString()方法。同样的结果。这对我很有效
private void printContentsOfTextBox(XWPFParagraph paragraph) {
XmlObject[] textBoxObjects = paragraph.getCTP().selectPath("
declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'
declare namespace wps='http://schemas.microsoft.com/office/word/2010/wordprocessingShape'
declare namespace v='urn:schemas-microsoft-com:vml'
.//*/wps:txbx/w:txbxContent | .//*/v:textbox/w:txbxContent");
for (int i =0; i < textBoxObjects.length; i++) {
XWPFParagraph embeddedPara = null;
try {
XmlObject[] paraObjects = textBoxObjects[i].
selectChildren(
new QName("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "p"));
for (int j=0; j<paraObjects.length; j++) {
embeddedPara = new XWPFParagraph(
CTP.Factory.parse(paraObjects[j].xmlText()), paragraph.getBody());
//Here you have your paragraph;
System.out.println(embeddedPara.getText());
}
} catch (XmlException e) {
//handle
}
}
}
private void printContentsOfTextBox(XWPFParagraph段落){
XmlObject[]textBoxObjects=段落。getCTP()。选择路径(“
声明命名空间w='1!'http://schemas.openxmlformats.org/wordprocessingml/2006/main'
声明名称空间wps=http://schemas.microsoft.com/office/word/2010/wordprocessingShape'
声明命名空间v='urn:schemas microsoft com:vml'
./*/wps:txbx/w:txbxContent |./*/v:textbox/w:txbxContent“);
对于(int i=0;i
private void printContentsOfTextBox(XWPFParagraph paragraph) {
XmlObject[] textBoxObjects = paragraph.getCTP().selectPath("
declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'
declare namespace wps='http://schemas.microsoft.com/office/word/2010/wordprocessingShape'
declare namespace v='urn:schemas-microsoft-com:vml'
.//*/wps:txbx/w:txbxContent | .//*/v:textbox/w:txbxContent");
for (int i =0; i < textBoxObjects.length; i++) {
XWPFParagraph embeddedPara = null;
try {
XmlObject[] paraObjects = textBoxObjects[i].
selectChildren(
new QName("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "p"));
for (int j=0; j<paraObjects.length; j++) {
embeddedPara = new XWPFParagraph(
CTP.Factory.parse(paraObjects[j].xmlText()), paragraph.getBody());
//Here you have your paragraph;
System.out.println(embeddedPara.getText());
}
} catch (XmlException e) {
//handle
}
}
}
private void printContentsOfTextBox(XWPFParagraph段落){
XmlObject[]textBoxObjects=段落。getCTP()。选择路径(“
声明命名空间w='1!'http://schemas.openxmlformats.org/wordprocessingml/2006/main'
声明名称空间wps=http://schemas.microsoft.com/office/word/2010/wordprocessingShape'
声明命名空间v='urn:schemas microsoft com:vml'
./*/wps:txbx/w:txbxContent |./*/v:textbox/w:txbxContent“);
对于(int i=0;i 对于(int j=0;j要从Word.doc和.docx文件中提取所有出现的文本,我使用源代码作为如何正确使用Apache POI API的参考。如果您希望直接使用POI而不依赖Tika,这非常有用
对于Word.docx文件,请查看此Tika类:
org.apache.tika.parser.microsoft.ooxml.XWPFWordExtractorDecorator
如果忽略XHTMLContentHandler
和格式化代码,您可以看到如何使用POI正确导航XWPFDocument
。
对于.doc文件,此类非常有用:
org.apache.tika.parser.microsoft.WordExtractor
都来自tika-parsers-1.x.jar
。通过maven依赖项访问tika代码的简单方法是将tika临时添加到pom.xml中,如
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-parsers</artifactId>
<version>1.7</version>
</dependency>
org.apache.tika
提卡解析器
1.7
让您的IDE解析附加的源代码并进入上面的类。要从Word.doc和.docx文件中提取所有出现的文本,我使用源代码作为如何正确使用Apache POI API的参考。如果您希望直接使用POI而不依赖Tika,这非常有用
对于Word.docx文件,请查看此Tika类:
org.apache.tika.parser.microsoft.ooxml.XWPFWordExtractorDecorator
如果忽略XHTMLContentHandler
和格式化代码,您可以看到如何使用POI正确导航XWPFDocument
。
对于.doc文件,此类非常有用:
org.apache.tika.parser.microsoft.WordExtractor
都来自tika-parsers-1.x.jar
。通过maven依赖项访问tika代码的简单方法是将tika临时添加到pom.xml中,如
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-parsers</artifactId>
<version>1.7</version>
</dependency>
org.apache.tika
提卡解析器
1.7
让您的IDE解析附加的源并进入上面的类。
@ PLutEXT,从DOC格式开始,但以后需要为DOX和RTF做同样的操作。您可以考虑使用JodCurrase+LyBrice将所有三种格式转换为DOX,然后使用POI(DOCX4J)从Dox中提取文本框内容。。这样,您就不必担心二进制格式或解析rtf。@plutext,非常感谢。。我将研究JODConverter。我希望它是免费的。@Shekhar您找到如何从.docx文档的文本框中提取文本了吗?如果找到了,欢迎您随时共享该信息。。)@ PututEXT,从DOC格式开始,但以后需要为DOX和RTF做同样的操作。您可以考虑使用JodCurrase+LyBeice将所有三种格式转换为DOX,然后使用POI(DOCX4J)从Dox中提取文本框内容。。这样,您就不必担心二进制格式或解析rtf。@plutext,非常感谢。。我将研究JODConverter。我希望它是免费的。@Shekhar您找到如何从.docx文档的文本框中提取文本了吗?如果找到了,欢迎您随时共享该信息。。)更新:在给定的示例中,并不是所有的文本框都在架构内。下面是另一个文本框objects.addAll(Arrays.asList(paration.getCTP().selectPath(“declare namespace w=''declare namespace v='urn:schemas microsoft com:vml./*/v:textbox/w:txbcontent”));我没有找到OOXML模式定义提供的文本框模式的完整列表,如果有人有,请共享。更新:结果不是所有的文本框都是