針對 Word、Excel、PDF 等文件文字抽取需求,本文總結四種主流技術方案,涵蓋 Jacob、Apache POI、PDFBox、Xpdf,附完整代碼範例與資源連結。
Jacob (Java-COM Bridge) 連接 Java 與 COM/Win32 API,需自行編寫 DLL 或使用官方提供的範例 DLL。適用於 Windows 平台調用 Office 應用程式進行格式轉換。
import com.jacob.com.*; import com.jacob.activeX.*;
public class FileExtracter {
public static void main(String[] args) {
ActiveXComponent app = new ActiveXComponent("Word.Application");
String inFile = "c:\\test.doc", tpFile = "c:\\temp.htm";
try {
app.setProperty("Visible", new Variant(false));
Object docs = app.getProperty("Documents").toDispatch();
Object doc = Dispatch.invoke(docs, "Open", Dispatch.Method,
new Object[]{inFile, new Variant(false), new Variant(true)}, new int[1]).toDispatch();
Dispatch.invoke(doc, "SaveAs", Dispatch.Method,
new Object[]{tpFile, new Variant(8)}, new int[1]); // 8 = wdFormatHTML
Dispatch.call(doc, "Close", new Variant(false));
} catch (Exception e) { e.printStackTrace(); }
finally { app.invoke("Quit", new Variant[] {}); }
}
}
📥 Jacob 下載 (含 DLL 與範例)
POI 是 Apache 專案,直接解析 Office 二進制格式。為簡化使用,提供封裝後的介面 WordExtractor。
import java.io.*; import org.textmining.text.extraction.WordExtractor;
public class PdfExtractor {
public static void main(String[] args) throws Exception {
FileInputStream in = new FileInputStream("c:\\a.doc");
WordExtractor extractor = new WordExtractor();
String text = extractor.extractText(in);
System.out.println("Length: " + text.length());
System.out.println(text);
}
}
PDFBox 為純 Java PDF 處理庫,但對中文支援較弱。
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.pdfparser.PDFParser;
import org.pdfbox.util.PDFTextStripper;
import java.io.*;
public class PdfExtracter {
public String getTextFromPdf(String filename) throws Exception {
FileInputStream is = new FileInputStream(filename);
PDFParser parser = new PDFParser(is);
parser.parse();
PDDocument doc = parser.getPDDocument();
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(out);
PDFTextStripper stripper = new PDFTextStripper();
stripper.writeText(doc, writer);
writer.close();
return new String(out.toByteArray());
}
public static void main(String[] args) {
try { System.out.println(new PdfExtracter().getTextFromPdf("c:\\a.pdf")); }
catch (Exception e) { e.printStackTrace(); }
}
}
Xpdf 為開源 C++ 專案,透過 JNI 調用原生 pdftotext.exe 實現高質量中文 PDF 抽取。需下載主程式與中文支援補丁。
import java.io.*;
public class PdfWin {
public static void main(String[] args) throws Exception {
String pdftotext = "C:\\Program Files\\xpdf\\pdftotext.exe";
String[] cmd = {pdftotext, "-enc", "UTF-8", "-q", "c:\\a.pdf", "-"};
Process p = Runtime.getRuntime().exec(cmd);
BufferedInputStream bis = new BufferedInputStream(p.getInputStream());
InputStreamReader reader = new InputStreamReader(bis, "UTF-8");
char[] buf = new char[10000]; int len;
while ((len = reader.read(buf)) >= 0) {
System.out.println(new String(buf, 0, len));
}
reader.close();
}
}
| 工具 | 優點 | 缺點 | 適用場景 |
|---|---|---|---|
| Jacob | 完美支援 Office 所有格式、版面保真 | 僅限 Windows、需安裝 Office、效能較低 | Windows 伺服器、需高保真轉換 |
| POI | 純 Java、跨平台、無需安裝 Office | 複雜版面解析不完美、大文件記憶體壓力大 | 跨平台環境、批次處理 .doc/.xls |
| PDFBox | 純 Java、跨平台、API 友善 | 中文支援弱、複雜 PDF 文字順序可能錯亂 | 英文為主的 PDF 文字抽取 |
| Xpdf | 中文支援最佳、速度快、穩定 | 需平台原生二進制、部署稍繁瑣 | 中文 PDF 為主的生產環境 |