Java 如何使用ApacheHWPF从文档文件中提取文本和图像
我下载了。我想用它来读取doc文件并将其文本写入纯文本文件。我不太了解HWPF 我非常简单的程序如下: 我现在有3个问题:Java 如何使用ApacheHWPF从文档文件中提取文本和图像,java,apache-poi,hwpf,Java,Apache Poi,Hwpf,我下载了。我想用它来读取doc文件并将其文本写入纯文本文件。我不太了解HWPF 我非常简单的程序如下: 我现在有3个问题: 有些包有错误(它们找不到ApacheHDF)。我怎样才能修好它们 如何使用HWDF的方法来查找和提取图像 我的一些程序是不完整和不正确的。所以请帮我完成它 我必须在两天内完成这个项目 我再次重复一遍,请帮我完成这个 非常感谢你们的帮助 这是我的基本代码: public class test { public void m1 (){ String filesnam
public class test {
public void m1 (){
String filesname = "Hello.doc";
POIFSFileSystem fs = null;
fs = new POIFSFileSystem(new FileInputStream(filesname );
HWPFDocument doc = new HWPFDocument(fs);
WordExtractor we = new WordExtractor(doc);
String str = we.getText() ;
String[] paragraphs = we.getParagraphText();
Picture pic = new Picture(. . .) ;
pic.writeImageContent( . . . ) ;
PicturesTable picTable = new PicturesTable( . . . ) ;
if ( picTable.hasPicture( . . . ) ){
picTable.extractPicture(..., ...);
picTable.getAllPictures() ;
}
}
如果您只想这样做,而不关心编码,您可以使用
$antiword file.doc>out.txt我很久以前就知道了这一点,但我发现谷歌代码中的文本挖掘更准确、更易于使用。然而,它几乎是被抛弃的代码。将为您完成这项工作。它处理与POI的对话以完成HWPF的工作,并为您提供文件内容的XHTML或纯文本。如果您注册了一个递归解析器,那么您也将获得所有嵌入的图像。
//您可以使用org.apache.poi.hwpf.extractor.WordExtractor获取文本
//you can use the org.apache.poi.hwpf.extractor.WordExtractor to get the text
String fileName = "example.doc";
HWPFDocument wordDoc = new HWPFDocument(new FileInputStream(fileName));
WordExtractor extractor = new WordExtractor(wordDoc);
String[] text = extractor.getParagraphText();
int lineCounter = text.length;
String articleStr = ""; // This string object use to store text from the word document.
for(int index = 0;index < lineCounter;++ index){
String paragraphStr = text[index].replaceAll("\r\n","").replaceAll("\n","").trim();
int paragraphLength = paragraphStr.length();
if(paragraphLength != 0){
articleStr.concat(paragraphStr);
}
}
//you can use the org.apache.poi.hwpf.usermodel.Picture to get the image
List<Picture> picturesList = wordDoc.getPicturesTable().getAllPictures();
for(int i = 0;i < picturesList.size();++i){
BufferedImage image = null;
Picture pic = picturesList.get(i);
image = ImageIO.read(new ByteArrayInputStream(pic.getContent()));
if(image != null){
System.out.println("Image["+i+"]"+" ImageWidth:"+image.getWidth()+" ImageHeight:"+image.getHeight()+" Suggest Image Format:"+pic.suggestFileExtension());
}
}
字符串fileName=“example.doc”;
HWPFDocument wordDoc=新的HWPFDocument(新文件输入流(文件名));
WordExtractor提取器=新的WordExtractor(wordDoc);
String[]text=extractor.getParagraphText();
int lineCounter=text.length;
字符串articleStr=“”;//此字符串对象用于存储word文档中的文本。
对于(int index=0;index
相关: