java itextpdf 5.5.6读取pdf中文文档乱码怎么解决

如题,jdk1.8下边,使用itextpdf5.5.6读取本地的pdf文档,读取的内容都是乱码,pdf文档是本地存在的,不是程序生成的。有没有大神能帮忙指点一下。

第1个回答  2017-07-09
String s = new String(bytes, 0, n,"gbk");
第2个回答  2017-07-09
Itext中文处理:
在Itext中的中文处理随着Itext的版本变化,采取的方法也在变化。
首先我来演示一下利用Itext-2.1.3.jar与iTextAsian.jar两个包来解决中文问题。
附件中有所需要的包:
示例代码:
Java代码
package com.lwf.pdf.test;
import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;

public class HelloWord {

public static void main(String[] args) {

try {
Document doc = new Document();
PdfWriter.getInstance(doc, new FileOutputStream("c:\\good.pdf"));
doc.open();
BaseFont bfChinese = BaseFont.createFont( "STSongStd-Light" , "UniGB-UCS2-H" , fals...Itext中文处理:
在Itext中的中文处理随着Itext的版本变化,采取的方法也在变化。
首先我来演示一下利用Itext-2.1.3.jar与iTextAsian.jar两个包来解决中文问题。
附件中有所需要的包:
示例代码:
Java代码
package com.lwf.pdf.test;
import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;

public class HelloWord {

public static void main(String[] args) {

try {
Document doc = new Document();
PdfWriter.getInstance(doc, new FileOutputStream("c:\\good.pdf"));
doc.open();
BaseFont bfChinese = BaseFont.createFont( "STSongStd-Light" , "UniGB-UCS2-H" , false );
Font fontChinese = new Font(bfChinese , 12 , Font.NORMAL, Color.GREEN);
Paragraph pf = new Paragraph("eee");
pf.add(new Paragraph("我们的家好大的家",fontChinese));
pf.add(new Paragraph("sfsfsf"));
doc.add(pf);

doc.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

}本回答被提问者采纳