2015-07-13 40 views
1

我忽略了所有与此问题有关的问题,但无法找到并回答。Java - PDFBox 1.8.9 unicode textfile to pdf

我有一个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(); 

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

这里的方式是如此的问题,我已阅读列表,所以请不要重定向我还给他们......

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

好像它不possbile,需要更新到2.0.0版本,并给它一个尝试。

EDITED#2:在新版本的PDFBox 2.0.0(至少现在)已被删除类TextToPDF(),让我通过textFile。所以现在这意味着,我要么手动读取文本,然后将其写入PDF,要么需要找到其他解决方案。

+1

前段时间我做了同样的,不得不使用不同的库(itext) – user1516873

回答

0

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

好像它不possbile,需要更新到2.0.0版本,并给它一个尝试。

编辑#2:在PDFBox的2.0.0(ATLEAST现)的新版本 已经被移除类TextToPDF() (在评论,有人说,它的缴费现在) 这让我过去在文本文件。所以现在这意味着,要么我手动读取文本,然后将其写入PDF,或需要找到一些其他解决方案

+0

*“删除了类TextToPDF”* - 不完全,它只是已经转移到单独的JAR,pdfbox-tools.jar,它也可以通过maven或从pdfbox.apache.org下载:https://pdfbox.apache.org/download.cgi#20x – mkl

+0

当我做了这个,或者是缺乏关于这个的信息,或者没有那个JAR。 – arccuks

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

刚试过这个。没有效果:( – arccuks

+0

你知道你的文件是UTF-8吗?试着将文件内容打印到标准输出中,看看java是否已经正确读取了内容。如果是,那么你的pdfbox需要更多的设置。否则,你仍然使用错误的编码读取文件注意一个txt文件的编码不能100%检查你必须知道它或你必须尝试 –

+0

我使用NotePad ++,并且我已经设置textFile编码为'UTF-8'。 Ty试图提供帮助,但似乎需要尝试预发行版本2.0.0 – arccuks

-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

1st:我不明白这是怎么解决我的unicode问题的 2nd:Link指向PAGE_NOT_FOUND。第三:对于最后一个,我认为它更好地做尝试与资源的方法,如'try(PDDocument doc = new PDDocument())' – arccuks