无法使用java读取pdf中的unicode字符

时间:2015-02-05 10:50:22

标签: java pdf unicode ms-word itext

我正在尝试将包含泰米尔语unicode字符的Pdf文档转换为保留所有格式的word文档。我无法读取Pdf中的unicode字符,它们在字中显示为垃圾字符。我使用下面的代码可以有人请帮忙吗?

public static void main(String[] args) throws IOException {
        System.out.println("Document converted started");
        XWPFDocument doc = new XWPFDocument();
        String pdf = "D:\\sample1.pdf";
        PdfReader reader = new PdfReader(pdf);
     //   InputStreamReader isr = new InputStreamReader(reader,"UTF8");
        PdfReaderContentParser parser = new PdfReaderContentParser(reader);
        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            TextExtractionStrategy strategy = parser.processContent(i,
                    new SimpleTextExtractionStrategy());
            System.out.println(strategy.getResultantText());
            String text = strategy.getResultantText();
            XWPFParagraph p = doc.createParagraph();
            XWPFRun run = p.createRun();
   //         run.setFontFamily(new Font("Arial"));
            run.setFontSize(14);
            run.setText(text);
     //       run.addBreak(BreakType.PAGE);
        }
        FileOutputStream out = new FileOutputStream("D:\\tamildoc.docx");
        doc.write(out);
        out.close();
        reader.close();
        System.out.println("Document converted successfully");
    }

1 个答案:

答案 0 :(得分:1)

您可以使用库 Apache PDFBox https://pdfbox.apache.org/download.cgi。使用组件 PDFTextStripper ,调用方法getText(PDDocument doc),您将获得一个表示.pdf文件内容的简单字符串

这是一个例子:

    UploadedFile file = new UploadedFile(fileName);
    InputStream is = file.getInputStream(); 
    PDDocument doc = PDDocument.load(is);
    String content = new PDFTextStripper().getText(doc);
    doc.close();

然后你就可以写你的文件了