iText入门示例

前几天偶然在网上看到iText输出PDF格式这一技术,今天下午心血澎湃就尝试了一下。
中间碰到了一个创建字体的问题:

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自带字库:

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



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


/**
* 创建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();
}
}
}



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.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 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 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();
}
}
}



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

public static void main(String[] args) {
PDFWriter pdf = new MyWriter("gtxin.pdf");
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值