I am trying to create java applicationone that will search for a specific word in the selected file doc, docxand create a report on it. This report will contain the page number and line number of the searched word. Now that I have reached, I can read the files docand docxparagraph by paragraph. But I did not find a way to search for a specific word and get the line and page number where this word is present. I searched a lot, but still no luck. Hope someone knows how to do this.
Here is my code
if(fc.getSelectedFile().getAbsolutePath().contains("docx")) {
File file = fc.getSelectedFile();
FileInputStream fis = new FileInputStream(file.getAbsolutePath());
XWPFDocument document = new XWPFDocument(fis);
List<XWPFParagraph> paragraphs = document.getParagraphs();
System.out.println("Total no of paragraph "+paragraphs.size());
for (XWPFParagraph para : paragraphs) {
System.out.println(para.getText());
}
fis.close();
} else {
WordExtractor extractor = null;
FileInputStream fis = new FileInputStream(fc.getSelectedFile());
HWPFDocument document = new HWPFDocument(fis);
extractor = new WordExtractor(document);
String[] fileData = extractor.getParagraphText();
for (int i = 0; i < fileData.length; i++) {
if (fileData[i] != null)
System.out.println(fileData[i]);
}
extractor.close();
}
I'm using swing,apache poi 3.10.1.
source
share