poi-scratchpad-3.8 (HWPF) を使用して Microsoft Word 2003 ドキュメント (.doc) を読み込もうとしています。ファイルを単語ごと、または文字ごとに読み取る必要があります。どちらの方法でも、私が必要とするものには問題ありません。文字または単語を読み取ったら、単語/文字に適用されるスタイル名を取得する必要があります。問題は、.doc ファイルを読み取るときに単語または文字に使用されるスタイル名を取得するにはどうすればよいかということです。
編集
これを試みるために使用したコードを追加しています。誰かがこれを試みたいなら、頑張ってください。
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());
}
}