【问题标题】:Unable to read unicode character in pdf using java无法使用java读取pdf中的unicode字符
【发布时间】:2015-04-05 04:37:39
【问题描述】:

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

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

【问题讨论】:

  • String text, junk, too, or is it as expected?的内容

标签: java pdf unicode ms-word itext


【解决方案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();

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

【讨论】:

猜你喜欢
  • 2012-12-02
  • 1970-01-01
  • 1970-01-01
  • 2018-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多