JAVA生成PDF文件

package test;

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

public class MyIText {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

System.out.println("MyIText");
// 步骤 1: 创建一个document对象,大小为A4,上下左右边距都为36
Document document = new Document(PageSize.A4, 35, 35, 20, 20);

try {

// 步骤 2:
// 我们为document创建一个监听,并把PDF流写到文件中
PdfWriter.getInstance(document, new FileOutputStream("D:\\aerchi_Text.pdf"));

// 步骤 3:打开文档
document.open();

/** pdf文档中中文字体的设置,注意一定要添加iTextAsian.jar包 */
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);//加入document:
Font FontChinese1 = new Font(bfChinese, 10, Font.NORMAL);//加入document:
Font FontChinese2 = new Font(bfChinese, 8, Font.NORMAL);//加入document:

/** 创建章节对象 */
Paragraph title1 = new Paragraph("PDF推荐表大标题\n\n", FontChinese);
document.add(title1);

Paragraph title2 = new Paragraph("编号:AAA000001\n\n", FontChinese1);
document.add(title2);

Paragraph title3 = new Paragraph("■ 推荐单位\n\n", FontChinese1);
document.add(title3);

// 创建一个有2列的表格,它们之间的相关比率为 15%,85%
float[] widths = { 0.15f, 0.85f};
PdfPTable table = new PdfPTable(widths);

table.setWidthPercentage(100);//table100%

//第一行
PdfPCell cell=new PdfPCell(new Paragraph("单位名称", FontChinese1));
cell.setFixedHeight(25);
cell.setPaddingLeft(20);
cell.setPaddingTop(6);
table.addCell(cell);

cell=new PdfPCell(new Paragraph("15%", FontChinese1));
cell.setFixedHeight(25);
cell.setPaddingLeft(5);
cell.setPaddingTop(6);
table.addCell(cell);
//第二行
cell=new PdfPCell(new Paragraph("单位性质\n(勾选)", FontChinese1));
cell.setFixedHeight(45);
cell.setPaddingLeft(20);
cell.setPaddingTop(10);
table.addCell(cell);

String cc="";
String cc1="□ 产业研发企业 ";
String cc2="□ 产品制造企业 ";
String cc3="□ 设计公司 ";
String cc4="□ 院校 ";
String cc5="□ 其他服务机构 ";
String cc6="□ 设计师 ";

String[] characters="1,2,5".split(",");
for(int i=0; i<characters.length; i++){
if(characters[i].equals("1")){
cc1="■ 产业研发企业 ";
}else if(characters[i].equals("2")){
cc2="■ 产品制造企业 ";
}else if(characters[i].equals("3")){
cc3="■ 设计公司 ";
}else if(characters[i].equals("4")){
cc4="■ 院校 ";
}else if(characters[i].equals("5")){
cc5="■ 其他服务机构 ";
}else if(characters[i].equals("6")){
cc6="■ 设计师 ";
}
}
cc=cc1+cc2+cc3+"\n\n"+cc4+cc5+cc6;



cell=new PdfPCell(new Paragraph(cc, FontChinese2));
cell.setFixedHeight(45);
cell.setPaddingLeft(25);
cell.setPaddingTop(10);
table.addCell(cell);

//Image png = Image.getInstance("f:/http_imgload.jpg");//图片的地址
//cell.setImage(png);
table.addCell(cell);

// 把定义好的表格增加到文档中
document.add(table);

Paragraph title4 = new Paragraph("\n■ 联系人\n\n", FontChinese1);
document.add(title4);

// 创建一个有4列的表格,它们之间的相关比率为 15%,40%,15%,30%
float[] widths1 = { 0.15f, 0.4f, 0.15f, 0.3f};
PdfPTable table1 = new PdfPTable(widths1);
table1.setWidthPercentage(100);//table100%

//第一行
cell=new PdfPCell(new Paragraph("姓名", FontChinese1));
cell.setFixedHeight(25);
cell.setPaddingLeft(30);
cell.setPaddingTop(6);
table1.addCell(cell);

cell=new PdfPCell(new Paragraph("Aerchi", FontChinese1));
cell.setFixedHeight(25);
cell.setPaddingLeft(5);
cell.setPaddingTop(6);
table1.addCell(cell);

cell=new PdfPCell(new Paragraph("性别", FontChinese1));
cell.setFixedHeight(25);
cell.setPaddingLeft(30);
cell.setPaddingTop(6);
table1.addCell(cell);

cell=new PdfPCell(new Paragraph("女", FontChinese1));
cell.setFixedHeight(25);
cell.setPaddingLeft(5);
cell.setPaddingTop(6);
table1.addCell(cell);

//第二行
cell=new PdfPCell(new Paragraph("单位", FontChinese1));
cell.setFixedHeight(25);
cell.setPaddingLeft(30);
cell.setPaddingTop(6);
table1.addCell(cell);

cell=new PdfPCell(new Paragraph("云南昆明", FontChinese1));
cell.setFixedHeight(25);
cell.setPaddingLeft(5);
cell.setPaddingTop(6);
table1.addCell(cell);

cell=new PdfPCell(new Paragraph("职位", FontChinese1));
cell.setFixedHeight(25);
cell.setPaddingLeft(30);
cell.setPaddingTop(6);
table1.addCell(cell);

cell=new PdfPCell(new Paragraph("程序员", FontChinese1));
cell.setFixedHeight(25);
cell.setPaddingLeft(5);
cell.setPaddingTop(6);
table1.addCell(cell);

//第三行
cell=new PdfPCell(new Paragraph("联系电话", FontChinese1));
cell.setFixedHeight(25);
cell.setPaddingLeft(20);
cell.setPaddingTop(6);
table1.addCell(cell);

cell=new PdfPCell(new Paragraph("15908718888", FontChinese1));
cell.setFixedHeight(25);
cell.setPaddingLeft(5);
cell.setPaddingTop(6);
table1.addCell(cell);

cell=new PdfPCell(new Paragraph("电子邮件", FontChinese1));
cell.setFixedHeight(25);
cell.setPaddingLeft(20);
cell.setPaddingTop(6);
table1.addCell(cell);

cell=new PdfPCell(new Paragraph("aerchi@gmail.com", FontChinese1));
cell.setFixedHeight(25);
cell.setPaddingLeft(5);
cell.setPaddingTop(6);
table1.addCell(cell);

//第四行
cell=new PdfPCell(new Paragraph("通讯地址", FontChinese1));
cell.setFixedHeight(25);
cell.setPaddingLeft(20);
cell.setPaddingTop(6);
table1.addCell(cell);

cell=new PdfPCell(new Paragraph("文贸大厦", FontChinese1));
cell.setFixedHeight(25);
cell.setPaddingLeft(5);
cell.setPaddingTop(6);
cell.setColspan(3);
table1.addCell(cell);

//第五行
cell=new PdfPCell(new Paragraph("身份证号", FontChinese1));
cell.setFixedHeight(25);
cell.setPaddingLeft(20);
cell.setPaddingTop(6);
table1.addCell(cell);

cell=new PdfPCell(new Paragraph("530102198508152418", FontChinese1));
cell.setFixedHeight(25);
cell.setPaddingLeft(5);
cell.setPaddingTop(6);
cell.setColspan(3);
table1.addCell(cell);

// 把定义好的表格增加到文档中
document.add(table1);

Paragraph title5 = new Paragraph("\n■ 建议项目设置\n\n", FontChinese1);
document.add(title5);

// 创建一个有2列的表格,它们之间的相关比率为 15%,85%
float[] widths2 = { 0.15f, 0.85f};
PdfPTable table2 = new PdfPTable(widths2);
table2.setWidthPercentage(100);//table100%

//第一行
cell=new PdfPCell(new Paragraph("项目名称", FontChinese1));
cell.setFixedHeight(25);
cell.setPaddingLeft(20);
cell.setPaddingTop(6);
table2.addCell(cell);

cell=new PdfPCell(new Paragraph("机器猫", FontChinese1));
cell.setFixedHeight(25);
cell.setPaddingLeft(5);
cell.setPaddingTop(6);
table2.addCell(cell);

//第二行
cell=new PdfPCell(new Paragraph("项目领域", FontChinese1));
cell.setFixedHeight(25);
cell.setPaddingLeft(20);
cell.setPaddingTop(6);
table2.addCell(cell);

cell=new PdfPCell(new Paragraph("信息产业", FontChinese1));
cell.setFixedHeight(25);
cell.setPaddingLeft(5);
cell.setPaddingTop(6);
table2.addCell(cell);

//第二行
cell=new PdfPCell(new Paragraph("推荐理由\n(可附页说明)", FontChinese1));
cell.setFixedHeight(270);
cell.setPaddingLeft(12);
cell.setPaddingTop(110);
table2.addCell(cell);

table2.addCell(new Paragraph("项目开发意向/建议\n\n项目创新性说明\n\n项目社会价值说明\n\n产业化前景说明\n\n产业化基础说明\n\n", FontChinese2));

//第三行
cell=new PdfPCell(new Paragraph("单位签章\n(个人签章)", FontChinese1));
cell.setFixedHeight(25);
cell.setPaddingLeft(20);
cell.setPaddingTop(6);
table2.addCell(cell);

cell=new PdfPCell(new Paragraph("", FontChinese1));
cell.setFixedHeight(25);
cell.setPaddingLeft(5);
cell.setPaddingTop(6);
table2.addCell(cell);

//第四行
cell=new PdfPCell(new Paragraph("组委会意见", FontChinese1));
cell.setFixedHeight(60);
cell.setPaddingLeft(15);
cell.setPaddingTop(20);
table2.addCell(cell);

table2.addCell(new Paragraph("不错,不错", FontChinese1));

// 把定义好的表格增加到文档中
document.add(table2);

} catch (Exception de) {
de.printStackTrace();
}
// 步骤 5:关闭文档
document.close();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值