论坛首页 入门技术论坛

iText入门示例

浏览 8191 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-01-13  
   前几天偶然在网上看到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");
	}
}

   发表时间:2009-08-21  
好好,很有价值。
0 请登录后投票
   发表时间:2009-08-26  
如果页面有flash怎么导出?
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics