从pdf文件中读取非拉丁字符时出现编码错误

时间:2014-11-11 21:06:52

标签: pdf character-encoding pdfbox

我正在尝试使用PDFbox 1.8.7从pdf文件中阅读希腊语。问题是,无论我使用什么编码(Windows-1253,ISO-8859-7,UTF-8),我都无法读取希腊字符。您是否可以让我了解我需要在PDFbox的选项中设置不同的内容以便阅读希腊字符。 (样本pdf文件https://www.wetransfer.com/downloads/861b3c85bb2b1eca9f10af210a8eff4820141111223938/83ff55

我的源代码如下:

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import org.apache.pdfbox.cos.COSDocument;
    import org.apache.pdfbox.pdfparser.PDFParser;
    import org.apache.pdfbox.pdmodel.PDDocument;
    import org.apache.pdfbox.pdmodel.font.PDFont;
    import org.apache.pdfbox.pdmodel.font.PDTrueTypeFont;
    import org.apache.pdfbox.util.PDFText2HTML;
    import org.apache.pdfbox.util.PDFTextStripper;

    /**
     *
     * @author Administrator
     */
    public class Pdf {

        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            try {
                PDFTextStripper pdfStripper = null;
                PDDocument pdDoc = null;
                COSDocument cosDoc = null;
                File file = new File("C:\\themis\\Pdfs\\first.pdf");

                PDFParser parser = new PDFParser(new FileInputStream(file));
                parser.parse();
                cosDoc = parser.getDocument();
                pdDoc = new PDDocument(cosDoc);
                String encoding="ISO-8859-7";

                pdfStripper = new PDFTextStripper(encoding);
                pdfStripper.setStartPage(1);
                pdfStripper.setEndPage(2);
                String parsedText = pdfStripper.getText(pdDoc);
                System.out.println(parsedText);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(PdfBMW.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(PdfBMW.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

0 个答案:

没有答案
相关问题