PdfBox从pdf中提取具有相同font-family的文本

时间:2013-09-18 16:39:51

标签: java pdf pdfbox extraction

我需要从pdf中提取一段文本。此文本具有与特征相同的font-family。有任何想法吗? 欢呼声

编辑:让我以其他方式提问:如何从pdf页面中提取“粗体”文字?

1 个答案:

答案 0 :(得分:0)

public String pdftoText(String fileName){
    try {
        File f = new File(fileName);
        if (!f.isFile()) {
            System.out.println("File not exist.");
            return null;
        }
        parser = new PDFParser(new FileInputStream(f));
        parser.parse();
        cosDoc = parser.getDocument();
        pdfStripper = new PDFTextStripper();
        pdDoc = new PDDocument(cosDoc);
        parsedText = pdfStripper.getText(pdDoc);
        cosDoc.close();
        pdDoc.close();
        return parsedText;
    } catch (IOException ex) {
        Logger.getLogger(PDFTextParser.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}

在运行之前:将pdfbox.jar添加到您的项目中

相关问题