PDFから文字列取得

PDFBox を使うと、PDF から簡単に文字列を取得することができる。

PDDocument document = PDDocument.load(in);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Writer output = new OutputStreamWriter(baos, encoding);
PDFTextStripper stripper = new PDFTextStripper();
stripper.writeText(document, output);
return baos.toString(encoding);

Maven2 でこれしたいなら、以下を pom.xml に書く。

<dependency>
<groupId>pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>0.7.3</version>
</dependency>

一応、文字化けもしないみたい。

Word, Excel, PowerPointから文字列取得

POI を使うと、Wordからとか、簡単に文字列を取得することができる。

InputStream in = ...;
String content;
content = new org.apache.poi.hwpf.extractor.WordExtractor(in).getText();
content = new org.apache.poi.hssf.extractor.ExcelExtractor(new HSSFWorkbook(in)).getText();
content = new org.apache.poi.hslf.extractor.PowerPointExtractor(in).getText();

Maven2 でこれしたいなら、以下を pom.xml に書く。

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.5-beta6</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.5-beta6</version>
</dependency>

一応、文字化けもしないみたい。

Office Open XMLのMIMEタイプ

うーん、いくら何でも長すぎなんじゃないのか・・・(始め見て長すぎて信じられんかったよ)。

docx application/vnd.openxmlformats-officedocument.wordprocessingml.document
docm application/vnd.ms-word.document.macroEnabled.12
dotx application/vnd.openxmlformats-officedocument.wordprocessingml.template
dotm application/vnd.ms-word.template.macroEnabled.12
xlsx application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
xlsm application/vnd.ms-excel.sheet.macroEnabled.12
xltx application/vnd.openxmlformats-officedocument.spreadsheetml.template
xltm application/vnd.ms-excel.template.macroEnabled.12
xlsb application/vnd.ms-excel.sheet.binary.macroEnabled.12
xlam application/vnd.ms-excel.addin.macroEnabled.12
pptx application/vnd.openxmlformats-officedocument.presentationml.presentation
pptm application/vnd.ms-powerpoint.presentation.macroEnabled.12
ppsx application/vnd.openxmlformats-officedocument.presentationml.slideshow
ppsm application/vnd.ms-powerpoint.slideshow.macroEnabled.12
potx application/vnd.openxmlformats-officedocument.presentationml.template
potm application/vnd.ms-powerpoint.template.macroEnabled.12
ppam application/vnd.ms-powerpoint.addin.macroEnabled.12
sldx application/vnd.openxmlformats-officedocument.presentationml.slide
sldm application/vnd.ms-powerpoint.slide.macroEnabled.12
onetoc application/onenote
onetoc2 application/onenote
onetmp application/onenote
onepkg application/onenote