首页 文章

Java - PDFBox 1.8.9 unicode textfile到pdf

提问于
浏览
1

我在SO上忽略了与此问题相关的所有问题,但无法找到并回答 .

我有一个textFile,其中包含unicode字符,如“ā”,“š”,“ī”等 . 问题是,当我将textFile写入PDF时,pdf文件无法正确显示 .

如何设置我的代码,所以我可以在我的PDF上写这些字符?也许更好的问题是:这甚至可能吗?由于我一直在寻找这个几个小时,无法找到解决方案 .

由于这个应用程序将是商业的,我不能使用iText!

我的代码:

TextToPDF pdf = new TextToPDF();
String fileName = "test.txt";
File pdfFile = new File("test.pdf");

BufferedReader reader = new BufferedReader(new FileReader(fileName));

PDSimpleFont courier = PDType1Font.COURIER;
PDSimpleFont testFont = PDTrueTypeFont.loadTTF( document, new File("times.ttf" ));

pdf.setFont(testFont);
pdf.setFontSize(8);

pdf.createPDFFromText(document, reader);

document.save(pdfFile);
document.close();

如果有人这样做了,请分享你是如何做到的 . 我相信它应该与 font.setFontEncoding(); 有关但是由于PDFBox文档缺乏相当多的信息,我还没弄明白,我应该做什么或如何做到这一点 .

顺便说一下,这里是我读过的SO问题列表,所以请不要将我重新定向给他们......

1)Java PDFBOX text encoding

2)Using Java PDFBox library to write Russian PDF

3)Using PDFBox to write UTF-8 encoded strings to a PDF

我读了更多的主题,但这些仍然在我的标签中打开 .

编辑:刚发现这个 - > Using PDFBox to write unicode strings to a PDF

似乎它不可能,需要更新到2.0.0版并尝试一下 .

编辑#2:在新版本的PDFBox 2.0.0(至少现在)已经删除了类 TextToPDF() ,它让我传入textFile . 所以现在意味着,我手动阅读文本然后将其写入PDF,或者需要找到一些其他解决方案 .

3 回答

  • -1
    you can create a pdf by simply creating a file with .pdf extension 
    You are going to create pdf file like this way  "**File pdfFile = new File("test.pdf")**"  but itsn't correct way . please go through below code how to crate pdf file .
    
        public static void main(String arg[]){
           this.create("test.pdf");`enter code here`enter code here`
        }
        public void create(String file) throws IOException {*enter code here*
          PDDocument document=null;
          try {
            document=new PDDocument();
            PDPage blankPage=new PDPage();
            document.addPage(blankPage);
            document.save(file);
          }
          finally {
            if (document != null) {
              document.close();
            }
          }
        }
    
    and also go through below link **http://www.javased.com/api=org.apache.pdfbox.pdmodel.PDDocument**
    
  • 0

    你的问题在这里:

    BufferedReader reader = new BufferedReader(new FileReader(fileName));
    

    如下所述:http://docs.oracle.com/javase/7/docs/api/java/io/FileReader.html FileReader将以系统默认编码读取文件 . 把它改成这个:

    BufferedReader in = new BufferedReader(
               new InputStreamReader(
                          new FileInputStream(fileDir), "UTF8"));
    

    如果它是UTF-8,这将以UTF-8读取您的文件 . 你所描述的特殊字符存在于像iso latin 1等字符编码中 .

    当您知道输入的编码时,请确保以此编码方式读取它 . 然后PDFBox也可以用他想要的编码来编写它们 .

  • 0

    刚发现这个 - > Using PDFBox to write unicode strings to a PDF

    似乎它不可能,需要更新到2.0.0版并尝试一下 .

    编辑#2:在新版本的PDFBox 2.0.0(至少现在)已被删除类TextToPDF()(在评论中,已经说过它现在可用),这让我通过textFile . 所以现在意味着,我手动阅读文本然后将其写入PDF,或者需要找到一些其他解决方案

相关问题