簡體   English   中英

無法使用Java閱讀pdf中的unicode字符

[英]Unable to read unicode character in pdf using java

我正在嘗試將包含泰米爾語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");
    }

您可以使用Apache PDFBoxhttps://pdfbox.apache.org/download.cgi 使用組件PDFTextStripper ,調用方法getText(PDDocument doc)您將獲得一個簡單的String,它表示.pdf文件的內容

這是一個例子:

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

然后,您可以在文件上寫

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM