首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Java 抽取 Office/PDF 文件文字的四大核心工具

     編輯:Java 相關知識

針對 Word、Excel、PDF 等文件文字抽取需求,本文總結四種主流技術方案,涵蓋 Jacob、Apache POI、PDFBox、Xpdf,附完整代碼範例與資源連結。

1. Jacob:Java-COM 橋接方案

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 與範例)

2. Apache POI:純 Java 解析 Office 文件

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);
    }
}

📥 封裝版 POI 下載

3. PDFBox:純 Java PDF 文字抽取

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(); }
    }
}

📥 PDFBox 下載

4. Xpdf:支援中文的 PDF 文字抽取

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();
    }
}

📥 Xpdf 下載 | 中文補丁

選型建議

工具優點缺點適用場景
Jacob完美支援 Office 所有格式、版面保真僅限 Windows、需安裝 Office、效能較低Windows 伺服器、需高保真轉換
POI純 Java、跨平台、無需安裝 Office複雜版面解析不完美、大文件記憶體壓力大跨平台環境、批次處理 .doc/.xls
PDFBox純 Java、跨平台、API 友善中文支援弱、複雜 PDF 文字順序可能錯亂英文為主的 PDF 文字抽取
Xpdf中文支援最佳、速度快、穩定需平台原生二進制、部署稍繁瑣中文 PDF 為主的生產環境
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved