`

iText生成Pdf 和 中文编码问题

阅读更多
前几天偶然在网上看到iText输出PDF格式这一技术,今天下午心血澎湃就尝试了一下。
中间碰到了一个创建字体的问题:
Java代码
BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);   
console错误提示"Font 'STSong-Light' with 'UniGB-UCS2-H' is not recognized" 

BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
console错误提示"Font 'STSong-Light' with 'UniGB-UCS2-H' is not recognized"

   google一下,处理方式可以采用windos自带字库:
Java代码
BaseFont bf = BaseFont.CreateFont("c:\\windows\\fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); 

BaseFont bf = BaseFont.CreateFont("c:\\windows\\fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);



   下面是在对网上一例的代码整理:

Java代码
/** 
* 创建pdfWriter模板 
*  
* @Field cell表示表格中每个单元格的内容,paragraph为段落内容 
* @date 2009年01月13日 
* @author 林维煜 
*/ 
abstract public class PDFWriter {  
 
    protected Document document = null;  
    protected FileOutputStream out = null;  
    protected Rectangle pageSize = null;  
    protected String filePath = null;  
 
    protected Cell cell = null;  
    protected Paragraph header = null;  
    protected Paragraph prg = null;  
    protected Table table = null;  
      
    public PDFWriter(String filePath) {  
        try {  
            this.filePath = filePath;  
            document = new Document();  
            out = new FileOutputStream(filePath);//文档输出路径信息  
            PdfWriter.getInstance(document, out);  
            document.open();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (DocumentException e) {  
            e.printStackTrace();  
        }  
    }  
 
    public void close() {  
        try {  
            document.close();  
            out.close();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  


/**
* 创建pdfWriter模板
*
* @Field cell表示表格中每个单元格的内容,paragraph为段落内容
* @date 2009年01月13日
* @author 林维煜
*/
abstract public class PDFWriter {

protected Document document = null;
protected FileOutputStream out = null;
protected Rectangle pageSize = null;
protected String filePath = null;

protected Cell cell = null;
protected Paragraph header = null;
protected Paragraph prg = null;
protected Table table = null;

public PDFWriter(String filePath) {
try {
this.filePath = filePath;
document = new Document();
out = new FileOutputStream(filePath);//文档输出路径信息
PdfWriter.getInstance(document, out);
document.open();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
}

public void close() {
try {
document.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}


Java代码
import com.lowagie.text.BadElementException;  
import com.lowagie.text.Cell;  
import com.lowagie.text.Chunk;  
import com.lowagie.text.Font;  
 
/** 
* 定制单元格 
*  
* @date 2009年01月13日 
* @author 林维煜 
*/ 
public class PDFCell extends Cell {  
 
    public PDFCell(String content, int rowspan, int colspan)  
            throws BadElementException {  
        super(new Chunk(content, PDFChineseFont.createChineseFont(10,  
                Font.NORMAL)));  
        setRowspan(rowspan);  
        setColspan(colspan);  
        setHeader(false);  
    }  


import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Chunk;
import com.lowagie.text.Font;

/**
* 定制单元格
*
* @date 2009年01月13日
* @author 林维煜
*/
public class PDFCell extends Cell {

public PDFCell(String content, int rowspan, int colspan)
throws BadElementException {
super(new Chunk(content, PDFChineseFont.createChineseFont(10,
Font.NORMAL)));
setRowspan(rowspan);
setColspan(colspan);
setHeader(false);
}
}


Java代码
import com.lowagie.text.Element;  
import com.lowagie.text.Font;  
import com.lowagie.text.Paragraph;  
 
/** 
* 定制段落 
*  
* @date 2009年01月13日 
* @author 林维煜 
*/ 
@SuppressWarnings("serial")  
public class PDFParagragh extends Paragraph {  
 
    public PDFParagragh(String content, int alignment, int fontSize) {  
        super(content, PDFChineseFont.createChineseFont(fontSize, Font.NORMAL));  
        setAlignment(alignment);  
    }  
 
    public static final int CENTER = Element.ALIGN_CENTER;  
    public static final int LEFT = Element.ALIGN_LEFT;  
    public static final int RIGHT = Element.ALIGN_RIGHT;  
    public static final int TOP = Element.ALIGN_TOP;  
    public static final int MIDDLE = Element.ALIGN_MIDDLE;  
    public static final int BOTTOM = Element.ALIGN_BOTTOM;  


import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;

/**
* 定制段落
*
* @date 2009年01月13日
* @author 林维煜
*/
@SuppressWarnings("serial")
public class PDFParagragh extends Paragraph {

public PDFParagragh(String content, int alignment, int fontSize) {
super(content, PDFChineseFont.createChineseFont(fontSize, Font.NORMAL));
setAlignment(alignment);
}

public static final int CENTER = Element.ALIGN_CENTER;
public static final int LEFT = Element.ALIGN_LEFT;
public static final int RIGHT = Element.ALIGN_RIGHT;
public static final int TOP = Element.ALIGN_TOP;
public static final int MIDDLE = Element.ALIGN_MIDDLE;
public static final int BOTTOM = Element.ALIGN_BOTTOM;
}


Java代码
import java.io.IOException;  
import com.lowagie.text.DocumentException;  
import com.lowagie.text.Font;  
import com.lowagie.text.pdf.BaseFont;  
 
/** 
* 如果无此函数定义,生成的pdf文件中的中文字符将不会显示 
*  
* @date 2009年01月13日 
* @author 林维煜 
*/ 
public class PDFChineseFont {  
 
    private static Font chineseFont;  
 
    public final static Font createChineseFont(int size, int style) {  
        try {  
//          chineseFont = new Font(BaseFont.createFont("STSong-Light",  
//                  "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), size, style);  
              
            //使用windos自带字库:  
            chineseFont = new Font(BaseFont.createFont("c:\\windows\\fonts\\simsun.ttc,1",  
                    BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED), size, style);  
        } catch (DocumentException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        return chineseFont;  
    }  


import java.io.IOException;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.pdf.BaseFont;

/**
* 如果无此函数定义,生成的pdf文件中的中文字符将不会显示
*
* @date 2009年01月13日
* @author 林维煜
*/
public class PDFChineseFont {

private static Font chineseFont;

public final static Font createChineseFont(int size, int style) {
try {
// chineseFont = new Font(BaseFont.createFont("STSong-Light",
// "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), size, style);

//使用windos自带字库:
chineseFont = new Font(BaseFont.createFont("c:\\windows\\fonts\\simsun.ttc,1",
BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED), size, style);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return chineseFont;
}
}


Java代码
import com.lowagie.text.BadElementException;  
import com.lowagie.text.DocumentException;  
import com.lowagie.text.Paragraph;  
import com.lowagie.text.Table;  
 
/** 
* 定制PDF文档格式 
*  
* @date 2009年01月13日 
* @author 林维煜 
*/ 
public class MyWriter extends PDFWriter {  
 
    public MyWriter(String path) {  
        super(path);  
        try {  
            header = new Paragraph("练习文档");  
        //  header = new PDFParagraph("练习文档");   
 
            document.add(header);  
            table = new Table(14);  
            table.setBorderWidth(0);  
            table.addCell(new PDFCell("(单价:1毛)", 1, 5));  
            table.addCell(new PDFCell("2009年01月13号", 1, 9));  
            document.add(table);  
            table = new Table(14);  
            table.setBorderWidth(0);  
            table.addCell(new PDFCell("公司名称", 1, 2));  
            table.addCell(new PDFCell("网址", 1, 3));  
            table.addCell(new PDFCell("型号规格", 1, 2));  
            table.addCell(new PDFCell("数量", 1, 1));  
            table.addCell(new PDFCell("单价", 1, 1));  
            table.addCell(new PDFCell("总价", 1, 1));  
            table.addCell(new PDFCell("附件", 1, 2));  
            table.addCell(new PDFCell("备注", 1, 2));  
            table.endHeaders();// 换行  
            table.addCell(new PDFCell("广拓芯", 1, 2));  
            table.addCell(new PDFCell("www.thepartnumber.com", 1, 3));  
            table.addCell(new PDFCell("1860", 1, 2));  
            table.addCell(new PDFCell("3", 1, 1));  
            table.addCell(new PDFCell("0.3", 1, 1));  
            table.addCell(new PDFCell("2000", 1, 1));  
            table.addCell(new PDFCell("无", 1, 2));  
            table.addCell(new PDFCell("无", 1, 2));  
            table.endHeaders();       
            document.add(table);  
            close();// 别忘记关闭  
        } catch (BadElementException e) {  
            e.printStackTrace();  
        } catch (DocumentException e) {  
            e.printStackTrace();  
        }  
    }  


import com.lowagie.text.BadElementException;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;

/**
* 定制PDF文档格式
*
* @date 2009年01月13日
* @author 林维煜
*/
public class MyWriter extends PDFWriter {

public MyWriter(String path) {
super(path);
try {
header = new Paragraph("练习文档");
// header = new PDFParagraph("练习文档");

document.add(header);
table = new Table(14);
table.setBorderWidth(0);
table.addCell(new PDFCell("(单价:1毛)", 1, 5));
table.addCell(new PDFCell("2009年01月13号", 1, 9));
document.add(table);
table = new Table(14);
table.setBorderWidth(0);
table.addCell(new PDFCell("公司名称", 1, 2));
table.addCell(new PDFCell("网址", 1, 3));
table.addCell(new PDFCell("型号规格", 1, 2));
table.addCell(new PDFCell("数量", 1, 1));
table.addCell(new PDFCell("单价", 1, 1));
table.addCell(new PDFCell("总价", 1, 1));
table.addCell(new PDFCell("附件", 1, 2));
table.addCell(new PDFCell("备注", 1, 2));
table.endHeaders();// 换行
table.addCell(new PDFCell("广拓芯", 1, 2));
table.addCell(new PDFCell("www.thepartnumber.com", 1, 3));
table.addCell(new PDFCell("1860", 1, 2));
table.addCell(new PDFCell("3", 1, 1));
table.addCell(new PDFCell("0.3", 1, 1));
table.addCell(new PDFCell("2000", 1, 1));
table.addCell(new PDFCell("无", 1, 2));
table.addCell(new PDFCell("无", 1, 2));
table.endHeaders();
document.add(table);
close();// 别忘记关闭
} catch (BadElementException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
}
}


Java代码
/** 
* 测试类 
*  
* @date 2009年01月13日 
* @author 林维煜 
*/ 
public class Test {  
 
    public static void main(String[] args) {  
        PDFWriter pdf = new MyWriter("gtxin.pdf");  
    }  


/**
* 测试类
*
* @date 2009年01月13日
* @author 林维煜
*/
public class Test {

public static void main(String[] args) {
PDFWriter pdf = new MyWriter("gtxin.pdf");
}
}
分享到:
评论

相关推荐

    itextpdf 解决中文不显示问题

    itextpdf 再创将pdf格式文件的时候中文不显示或者乱码问题的解决 其实目前最新版本的itexpdf即使加了asian的辅助包也不能解决中文不显示问题 因为自己试过 可能方式不对 这个绝对显示 显示不了的 分不要

    Java生成PDF完整示例,解决中文乱码

    Java生成PDF完整示例,并解决中文乱码问题, 内有生成PDF相关的三个包, iText-2.0.8.jar, iTextAsian.jar, iTextAsianCmaps.jar

    html转pdfHTML导出PDF中文、ITEXT转HTML为PDF包括中文显示解决方案

    1、解决中文问题 2、附字体 3、动态html拼接转pdf public static void htmlCodeComeString(String linkcss,String htmlCode, String outputFile,String title) throws Exception { OutputStream os = new ...

    flying-saucer-pdf 生成pdf解决图片问题 解决中文问题

    flying-saucer-pdf 生成pdf解决图片问题 解决中文问题

    根据PDF模板生成PDF文件 下载 在线预览

    原生java代码,导入到eclipse就能运行,这是研究了两天研究出来的,比网上的用什么doc转pdf,还有pdfbox之类的有效多了 而且没有乱码。绝对有效。所需的模板在另一地址下载:...

    ItextpdfboxPDFword生成和读取各种例子最详细的打包

    itext pdfbox 详细的例子,其中包含 读取 解析pdf wrod 生成画格子 以及解决中文乱码问题 很多例子打包成一起 详细的注释

    itext2.0.8支持中文换行例子源码

    网上有很多人没有搞定,有人搞定了不共享,强烈鄙视弄出来了不共享的这些人,这里是我自己的例子,能够正确的将html生成pdf,详细说明请看我的博客,有什么问题欢迎交流。。。

    html2pdf:html转换为pdf

    itext7的一些简单操作(二维码、水印、表格、HTML代码生成PDF) itext7解决中文显示问题有两种解决方式: 1 引入对应的语言包,火狐浏览器预览生成的pdf可能存在部分中文乱码问题,同时因为加载了语言包,生成的pdf更大 ...

    java8源码-java-vue-pdf:java通过vuessr动态生成pdf解决方案Itonlyneedsvue

    java接收到html通过itext生成pdf 注:由于ssr只支持get请求,所以如果一次将所有数据都通过url传送的方式不太好,因此通过传递id反向查询数据的方式更靠谱 准备 java8(本人使用) node10(本人使用) 安装字体。生成pdf...

    Java和itext7实现的pdf文件签章

    解决pdf文件的电子签章功能,通过输入或实时数据库信息计算;...可以通过编码进行自定义设置,例如印章所在的页码和位置等。还支持电子签约、电子签章 账单的生成等等一系列的模板类的pdf的生成或者合成

    flying saucer的中文不显示问题(修改源码后重新打的jar包)

    使用flyingsaucer将网页转换为pdf之中文问题彻底解决

    itext5.5.0 api

    比较稳定的版本,pdf的生成展示以及weblogic上乱码问题本人亲测可解决。

    亚洲字符集,iTextAsian.jar

    解决ireport3.0报表生成pdf文件乱码问题。。。

    web开发常用jar

    itext中关于亚洲编码的类库,在这里用于中文字体的输入。 junit.jar Junit包,当你运行Hibernate自带的测试代码的时候需要,否则就不用。 commons-digester.jar Apache Commons包中的一个,通过它可以很方便的...

    java开发常用jar包

    itext中关于亚洲编码的类库,在这里用于中文字体的输入。 junit.jar Junit包,当你运行Hibernate自带的测试代码的时候需要,否则就不用。 commons-digester.jar Apache Commons包中的一个,通过它可以很方便的解析...

    JSP实用技巧集合,jsp编程的一些小技巧总结

    52. 使用iText生成PDF? 53. 制作图片水印? 54. 在页面中屏蔽键盘功能键? 55. 禁止用户复制网页内容? 56. 实现不在地址拦中显示当前URL? 57. 获取用户的真实IP地址? 58. 获取用户浏览器信息? 59. 获取当前绝对...

    Java开发实战1200例(第1卷).(清华出版.李钟尉.陈丹丹).part3

    本书是第II卷,以开发人员在项目开发中经常遇到的问题和必须掌握的技术为中心,介绍了应用Java进行桌面程序开发各个方面的知识和技巧,主要包括Java语法与面向对象技术、Java高级应用、窗体与控件应用、文件操作...

    瓦迪姆

    所有者和管理员通过管理员添加和编辑用户添加和编辑客户端按员工搜索员工所在城市的客户通过管理员搜索数据库中的所有客户端导入和导出CSV文件以及客户数据与客户签订合同根据合同价值和用户角色接受合同打印PDF文件...

    jsp编程技巧集锦

    使用iText生成PDF? 53. 制作图片水印? 54. 在页面中屏蔽键盘功能键? 55. 禁止用户复制网页内容? 56. 实现不在地址拦中显示当前URL? 57. 获取用户的真实IP地址? 58. 获取用户浏览器信息?...

Global site tag (gtag.js) - Google Analytics