java生成PDF(支持中文)

package com.util;
/**
* AsianTest.java
*/

import java.io.FileOutputStream;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;

import java.awt.Color;

public class NewPdfCreate {

public static void UtilCreatePdf(HttpServletRequest request) {

// 创建一个Document对象
Document document = new Document();

try {

// 生成名为 AsianTest.pdf 的文档
PdfWriter.getInstance(document, new FileOutputStream(
"d:\\ima\\AsianTest.pdf"));

/**
* 新建一个字体,iText的方法 STSongStd-Light 是字体,在iTextAsian.jar 中以property为后缀
* UniGB-UCS2-H 是编码,在iTextAsian.jar 中以cmap为后缀 H 代表文字版式是 横版, 相应的 V
* 代表竖版
*/
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",
"UniGB-UCS2-H", false);
/**
BOLD 粗
BOLDITALIC 粗斜
*/
Font bold_fontChinese = new Font(bfChinese, 14, Font.BOLD,
Color.BLACK); //标题 加粗
Font nameandsex = new Font(bfChinese, 12, Font.BOLD,
Color.BLACK); //身份内容 加粗
Font content_fontChinese = new Font(bfChinese, 6, Font.COURIER,
Color.BLACK); //标题 加粗
Font italic_fontChinese = new Font(bfChinese, 10, Font.ITALIC,
Color.BLACK); //斜,细
Font impressFont = new Font(bfChinese, 14, Font.BOLDITALIC,
Color.BLACK);//加粗,阴影
Font normal_fontChinese = new Font(bfChinese, 10, Font.NORMAL,
Color.BLACK); //正常内容
Font keyfont=new Font(bfChinese,6,Font.BOLD);
// 打开文档,将要写入内容
document.open();
document.add(new Paragraph(" ", bold_fontChinese));
Image jpeg1 = Image.getInstance("d:\\ima\\1.png");

// 图片居中
//jpeg.setAlignment(Image.ALIGN_CENTER);
// jpeg.setAbsolutePosition(60, 100);
document.add(jpeg1);
/* Cell cell = new Cell("example cell with colspan 1 and rowspan 2");
Table table = new Table(3);
table.setPadding(5);
table.addCell(cell, 3, 2);*/


//header information
document.add(new Paragraph(" 债权转让及受让协议(首期)",bold_fontChinese));


document.add(new Paragraph(" ", bold_fontChinese));
document.add(new Paragraph("路博玉"+" 女士", nameandsex));
// 吉林省吉林市船营区洮南胡同11-1-16
document.add(new Paragraph("吉林省\t吉林市\t船营区\t洮南胡同11-1-16", nameandsex));
document.add(new Paragraph("564100", nameandsex));
//债权转让及受让
document.add(new Paragraph(" 债权转让及受让", nameandsex));
document.add(new Paragraph(" ", bold_fontChinese));
document.add(new Paragraph("尊敬的路博玉\t\t女士,您好!", normal_fontChinese));
//尊敬的路博玉/女士,您好! addSubject
document.add(new Paragraph(" 通过玖和惠诚公司信用评估筛选,推荐您 2015-10-10通过受让他人既有的个人间借款合同的方式,出借资金给如下借款人,详见《债权列表》,在您接受该批债权转让并按时支付对价的情况下,预期您的出借获益情况如下:",normal_fontChinese));



Table table = new Table(8);
table.setBorderWidth(0);
table.setBorderColor(new Color(0, 0, 255));
table.setPadding(1);
table.setSpacing(0);


Cell cell = new Cell(new Paragraph("货币单位:人民币(元)",keyfont));
cell.setHeader(true);
cell.setColspan(8);
table.addCell(cell);
table.endHeaders();
cell = new Cell(new Paragraph("出借编号",keyfont));
cell.setHeader(true);
cell.setColspan(2);
table.addCell(cell);
table.addCell(new Paragraph("资金出借及回收方式",keyfont));
table.addCell(new Paragraph("初始出借日期",keyfont));
table.addCell(new Paragraph("初始出借金额",keyfont));
table.addCell(new Paragraph("下一个报告日",keyfont));
table.addCell(new Paragraph("下一个报告期借款人应还款额",keyfont));
table.addCell(new Paragraph("预计下一个报告日您的资产总额",keyfont));

cell = new Cell(new Paragraph("CJ20151208114352863",keyfont));
cell.setHeader(true);
cell.setColspan(2);
table.addCell(cell);
table.addCell(new Paragraph("月月赢",keyfont));
table.addCell(new Paragraph("2015/12/12",keyfont));
table.addCell(new Paragraph("140000",keyfont));
table.addCell(new Paragraph("2015/12/12",keyfont));
table.addCell(new Paragraph("14267.27",keyfont));
table.addCell(new Paragraph("141108.33",keyfont));
document.add(table);

document.add(new Paragraph(" ", bold_fontChinese));
document.add(new Paragraph(" 债权列表",normal_fontChinese));
document.add(new Paragraph(" ", normal_fontChinese));
document.add(new Paragraph("转让人(原债权人):邹文华 受让人(新债权人):路博玉",normal_fontChinese));
document.add(new Paragraph("身份证(护照)号码:2202031987****0034 身 份 证 (护 照):2201041935****1547",normal_fontChinese));


document.add(new Paragraph(" ", bold_fontChinese));
document.add(new Paragraph("债权基本信息:", bold_fontChinese));

Table table2 = new Table(12);
table2.setBorderWidth(0);
table2.setBorderColor(new Color(0, 0, 255));
table2.setPadding(1);
table2.setSpacing(0);

Cell cell2 = new Cell(new Paragraph("货币单位:人民币(元)",keyfont));
cell2.setHeader(true);
cell2.setColspan(12);
table2.addCell(cell2);
table2.endHeaders();
table2.addCell(new Paragraph("序号",keyfont));
table2.addCell(new Paragraph("借款人姓名",keyfont));
cell2 = new Cell(new Paragraph("借款人身份证",keyfont));
cell2.setHeader(true);
cell2.setColspan(2);
table2.addCell(cell2);
table2.addCell(new Paragraph("本次转让债权价值",keyfont));
table2.addCell(new Paragraph("需支付对价",keyfont));
table2.addCell(new Paragraph("借款人职业情况",keyfont));
table2.addCell(new Paragraph("借款人借款用途",keyfont));
table2.addCell(new Paragraph("还款起始日期",keyfont));
table2.addCell(new Paragraph("还款期限",keyfont));
table2.addCell(new Paragraph("剩余还款月数",keyfont));
table2.addCell(new Paragraph("预计债权收益率(年)",keyfont));

table2.addCell(new Paragraph("1",keyfont));
table2.addCell(new Paragraph("韩松",keyfont));
cell2 = new Cell(new Paragraph("2202041965****0318",keyfont));
cell2.setHeader(true);
cell2.setColspan(2);
table2.addCell(cell2);
table2.addCell(new Paragraph("70000",keyfont));
table2.addCell(new Paragraph("70000",keyfont));
table2.addCell(new Paragraph("个体",keyfont));
table2.addCell(new Paragraph("资金周转",keyfont));
table2.addCell(new Paragraph("2015/10/1",keyfont));
table2.addCell(new Paragraph("12",keyfont));
table2.addCell(new Paragraph("11",keyfont));
table2.addCell(new Paragraph("13.2%",keyfont));

table2.addCell(new Paragraph("2",keyfont));
table2.addCell(new Paragraph("韩松",keyfont));
cell2 = new Cell(new Paragraph("2202041965****0318",keyfont));
cell2.setHeader(true);
cell2.setColspan(2);
table2.addCell(cell2);
table2.addCell(new Paragraph("70000",keyfont));
table2.addCell(new Paragraph("70000",keyfont));
table2.addCell(new Paragraph("个体",keyfont));
table2.addCell(new Paragraph("资金周转",keyfont));
table2.addCell(new Paragraph("2015/10/1",keyfont));
table2.addCell(new Paragraph("12",keyfont));
table2.addCell(new Paragraph("11",keyfont));
table2.addCell(new Paragraph("13.2%",keyfont));

table2.addCell(new Paragraph("3",keyfont));
table2.addCell(new Paragraph("韩松",keyfont));
cell2 = new Cell(new Paragraph("2202041965****0318",keyfont));
cell2.setHeader(true);
cell2.setColspan(2);
table2.addCell(cell2);
table2.addCell(new Paragraph("70000",keyfont));
table2.addCell(new Paragraph("70000",keyfont));
table2.addCell(new Paragraph("个体",keyfont));
table2.addCell(new Paragraph("资金周转",keyfont));
table2.addCell(new Paragraph("2015/10/1",keyfont));
table2.addCell(new Paragraph("12",keyfont));
table2.addCell(new Paragraph("11",keyfont));
table2.addCell(new Paragraph("13.2%",keyfont));

cell2 = new Cell(new Paragraph("合计:",keyfont));
cell2.setHeader(true);
cell2.setColspan(4);
table2.addCell(cell2);
table2.addCell(new Paragraph("70000",keyfont));
table2.addCell(new Paragraph("70000",keyfont));
document.add(table2);

document.add(new Paragraph(" ",keyfont));

document.add(new Paragraph(" 转让人声明", nameandsex));
//自款项到账之日次日起,上述债权转让即生效;债权转让生效后,署有本人签章的本文件即代表受让人对上述债权的权利。
document.add(new Paragraph(" 自款项到账之日次日起,上述债权转让即生效;债权转让生效后,署有本人签章的本文件即代表受让人对上述债权的权利。",keyfont));
document.add(new Paragraph(" ",keyfont));
document.add(new Paragraph("转让人: 见证人:吉林经济信息咨询有限公司",keyfont));
document.add(new Paragraph(" ", bold_fontChinese));

document.add(new Paragraph("签章: 盖章: ",keyfont));
document.add(new Paragraph(" ", bold_fontChinese));

/*Image jpeg = Image.getInstance("d:\\ima\\3.png");
jpeg.scaleAbsoluteHeight(30);
jpeg.scalePercent(30);
// 图片居中
//jpeg.setAlignment(Image.ALIGN_CENTER);
jpeg.setAbsolutePosition(60, 60);
document.add(jpeg);*/


//印章
String imagePath = Util.getPathOfProject(request)+"corporatesealImage\\corporateseal.png";
Image png2 = Image.getInstance(imagePath);
png2.scaleAbsoluteHeight(30);
png2.scalePercent(30);
// 图片居中
//jpeg.setAlignment(Image.ALIGN_CENTER);
png2.setAbsolutePosition(370, 50);
document.add(png2);

document.add(new Paragraph("日期:2015/10/10 日期:2015/10/10 ",keyfont));
document.add(new Paragraph(" ", bold_fontChinese));
document.add(new Paragraph("玖和提示:", nameandsex));
document.add(new Paragraph(" ", bold_fontChinese));
document.add(new Paragraph(" 如果受让人对上述债权全部或部分不认可,请填写回执,标明所拒绝的债权序号,在 3 日 前将回执送达玖和并 自己保留一份取得玖和盖章的回执,由玖和协调进行其他债权的推荐。 ",keyfont));

document.add(new Paragraph(" ", bold_fontChinese));
document.add(new Paragraph(" 债权转让拒绝回执", nameandsex));
document.add(new Paragraph(" 本人拒绝转让人 上述欲转让给本人的第 号(依次填写序号)债权;对于其他未拒绝的债权,本人不可撤销地同意予以受让,请玖和公司知悉并见证。",keyfont));

document.add(new Paragraph("本人特此签章证明。",keyfont));
document.add(new Paragraph(" ", bold_fontChinese));
document.add(new Paragraph(" ", bold_fontChinese));
document.add(new Paragraph("转让人: 见证人:吉林经济信息咨询有限公司",keyfont));
document.add(new Paragraph(" ", bold_fontChinese));
document.add(new Paragraph("签章: 盖章:",keyfont));
document.add(new Paragraph(" ", bold_fontChinese));
document.add(new Paragraph("日期: 日期:",keyfont));




} catch (DocumentException de) {
System.err.println(de.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
}

// 关闭打开的文档
document.close();
}

}

 


posted @ 2015-12-09 09:31  吐槽村  阅读(1637)  评论(0编辑  收藏  举报