用itext导出pdf文件,包含中文
2009-07-14 21:47
316 查看
今天在用itext做导出pdf文件时出现了中文不显示,然后就看itext的api又写了一个PDFExcel让其继承Cell.如下
/**
*
*/
package com.pdf.demo;
import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Chunk;
import com.lowagie.text.Font;
/**
* @author 崔冉 E-mail:cuiran2001@163.com
* @version 创建时间:2009-7-14 下午08:47:53
*/
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);
}
public PDFCell(String content)
throws BadElementException {
super(new Chunk(content, PDFChineseFont
.createChineseFont(10, Font.NORMAL)));
setHeader(false);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
然后写个例子如下。
/**
*
*/
package com.pdf.demo;
import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
/**
* @author Administrator
*
*/
public class pdfTa {
/**
* @param args
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String [] title={"序号","项目名称","计划名称","项目类型","项目密级","开始时间","结束时间"};
Document document=new Document();
BaseFont bfChinese=null;
try {
bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
} catch (DocumentException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
com.lowagie.text.Font FontChinese = new com.lowagie.text.Font(bfChinese, 12, com.lowagie.text.Font.NORMAL );
Paragraph pragraph=new Paragraph("计划信息", FontChinese);
try {
try {
PdfWriter.getInstance(document, new FileOutputStream("Chap0103.pdf"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
document.addTitle("这是一个demo!");
document.addSubject("计划信息");
document.addCreator("天外来客");
document.addCreationDate();
document.open();
try {
document.add(pragraph);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Table table=new Table(7);
table.setBorderWidth(1);
table.setBorderColor(new Color(0, 0, 255));
// table.setPadding(5);
// table.setSpacing(5);
for(int i=0;i<title.length;i++){
table.addCell(new PDFCell(title[i]));
}
try {
document.add(table);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (BadElementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
document.close();
}
}
/**
*
*/
package com.pdf.demo;
import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Chunk;
import com.lowagie.text.Font;
/**
* @author 崔冉 E-mail:cuiran2001@163.com
* @version 创建时间:2009-7-14 下午08:47:53
*/
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);
}
public PDFCell(String content)
throws BadElementException {
super(new Chunk(content, PDFChineseFont
.createChineseFont(10, Font.NORMAL)));
setHeader(false);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
然后写个例子如下。
/**
*
*/
package com.pdf.demo;
import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
/**
* @author Administrator
*
*/
public class pdfTa {
/**
* @param args
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String [] title={"序号","项目名称","计划名称","项目类型","项目密级","开始时间","结束时间"};
Document document=new Document();
BaseFont bfChinese=null;
try {
bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
} catch (DocumentException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
com.lowagie.text.Font FontChinese = new com.lowagie.text.Font(bfChinese, 12, com.lowagie.text.Font.NORMAL );
Paragraph pragraph=new Paragraph("计划信息", FontChinese);
try {
try {
PdfWriter.getInstance(document, new FileOutputStream("Chap0103.pdf"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
document.addTitle("这是一个demo!");
document.addSubject("计划信息");
document.addCreator("天外来客");
document.addCreationDate();
document.open();
try {
document.add(pragraph);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Table table=new Table(7);
table.setBorderWidth(1);
table.setBorderColor(new Color(0, 0, 255));
// table.setPadding(5);
// table.setSpacing(5);
for(int i=0;i<title.length;i++){
table.addCell(new PDFCell(title[i]));
}
try {
document.add(table);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (BadElementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
document.close();
}
}
相关文章推荐
- 用itext导出pdf文件,包含中文
- jasperreports和Ireport导出PDF文件中文不显示问题(最新解决方法)
- iText导出pdf 表格文件
- Itext 导出PDF(中文解决,HTML转PDF)
- HTML导出PDF中文文件
- 利用iText导出pdf文件
- flying-saucer + iText + Freemarker实现pdf的导出, 支持中文、css以及图片
- 对itext导出PDF的一些小总结,以及ireport中生成中文PDF
- confluence导出PDF格式文件不显示中文解决
- 滴水穿石--Java 生成PDF文件--iText使用之插入图片和中文乱码
- iText生成PDF文件(解决了中文乱码)
- 关于RDLC使用导出PDF文件时,中文乱码解决方案
- 利用itext导出HTML到PDF,解决中文不显示和中文不换行的问题
- dokuwiki + dw2pdf 插件,解决导出pdf文件中文乱码(字体问题,显示小方块)
- Java解析PDF文件(PDFBOX、itext解析PDF)导出PDF中的子图片,去除PDF中的水印
- Itext 导出pdf文件
- Itext根据模板生成pdf文件并导出
- 使用Itext导出PDF文件
- ecside 导出中文繁体pdf出现乱码是缺少文件
- iText7 生成 PDF 文件,解决各种操作系统中文问题,控制指定位置显示图片、文字、图文混排