O Java Apache POI lê o arquivo Word (.doc) e obtém os estilos nomeados usados
Estou tentando ler um documento do Microsoft Word 2003 (.doc) usando o poi-scratchpad-3.8 (HWPF). Eu preciso ler o arquivo palavra por palavra ou caractere por caractere. De qualquer maneira é bom para o que eu preciso. Depois de ler um caractere ou uma palavra, preciso obter o nome do estilo aplicado à palavra / caractere. Então, a pergunta é: como obtenho o nome do estilo usado para uma palavra ou caractere ao ler o arquivo .doc?
EDITAR
Eu estou adicionando o código que eu usei para tentar isso. Se alguém quiser tentar isso, boa sorte.
private void processDoc(String path) throws Exception {
System.out.println(path);
POIFSFileSystem fis = new POIFSFileSystem(new FileInputStream(path));
HWPFDocument wdDoc = new HWPFDocument(fis);
// list all style names and indexes in stylesheet
for (int j = 0; j < wdDoc.getStyleSheet().numStyles(); j++) {
if (wdDoc.getStyleSheet().getStyleDescription(j) != null) {
System.out.println(j + ": " + wdDoc.getStyleSheet().getStyleDescription(j).getName());
} else {
// getStyleDescription returned null
System.out.println(j + ": " + null);
}
}
// set range for entire document
Range range = wdDoc.getRange();
// loop through all paragraphs in range
for (int i = 0; i < range.numParagraphs(); i++) {
Paragraph p = range.getParagraph(i);
// check if style index is greater than total number of styles
if (wdDoc.getStyleSheet().numStyles() > p.getStyleIndex()) {
System.out.println(wdDoc.getStyleSheet().numStyles() + " -> " + p.getStyleIndex());
StyleDescription style = wdDoc.getStyleSheet().getStyleDescription(p.getStyleIndex());
String styleName = style.getName();
// write style name and associated text
System.out.println(styleName + " -> " + p.text());
} else {
System.out.println("\n" + wdDoc.getStyleSheet().numStyles() + " ----> " + p.getStyleIndex());
}
}