Java导出PDF文件

package com.oracle.Test_pdf;


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


import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;


public class Test_PDF_table {
public static void main(String[] args) {
Document document = new Document(PageSize.A4);
PdfPCell cell = null;
PdfPTable itable = null;
PdfPCell icell = null;
float[] widths = { 30, 30, 30, 50, 30 };
try {
PdfWriter.getInstance(document, new FileOutputStream(new File(
"table.pdf")));
document.open();
BaseFont bfComic14 = BaseFont.createFont(
"c:\\windows\\fonts\\SIMLI.TTF", BaseFont.IDENTITY_H,
BaseFont.NOT_EMBEDDED);


String str1 = "招聘人员登记表"; // 生成第一行
Font font = new Font(bfComic14, 17, Font.BOLD);
Paragraph title = new Paragraph(str1, font);
title.setAlignment(Element.ALIGN_CENTER);
document.add(title);


PdfPTable table = new PdfPTable(5);
table.setWidths(widths);
table.setSpacingBefore(27f); // 设置表格上面空白宽度


font = new Font(bfComic14, 12, Font.NORMAL);
itable = new PdfPTable(1);
icell = new PdfPCell(new Paragraph("姓名", font));
icell.setHorizontalAlignment(Element.ALIGN_CENTER);
icell.setVerticalAlignment(Element.ALIGN_MIDDLE);
icell.setFixedHeight((float) 25.0);
itable.addCell(icell);


icell = new PdfPCell(new Paragraph("学历", font));
icell.setHorizontalAlignment(Element.ALIGN_CENTER);
icell.setVerticalAlignment(Element.ALIGN_MIDDLE);
icell.setFixedHeight((float) 25.0);
itable.addCell(icell);


icell = new PdfPCell(new Paragraph("专业", font));
icell.setHorizontalAlignment(Element.ALIGN_CENTER);
icell.setVerticalAlignment(Element.ALIGN_MIDDLE);
icell.setFixedHeight((float) 25.0);
itable.addCell(icell);


icell = new PdfPCell(new Paragraph("健康状况", font));
icell.setHorizontalAlignment(Element.ALIGN_CENTER);
icell.setVerticalAlignment(Element.ALIGN_MIDDLE);
icell.setFixedHeight((float) 25.0);
itable.addCell(icell);


cell = new PdfPCell(itable);
cell.setPadding(0);
table.addCell(cell); // 第一个格子


itable = new PdfPTable(2);
icell = new PdfPCell(new Paragraph(""));
icell.setHorizontalAlignment(Element.ALIGN_CENTER);
icell.setVerticalAlignment(Element.ALIGN_MIDDLE);
icell.setFixedHeight((float) 25.0);
itable.addCell(icell);


icell = new PdfPCell(new Paragraph("性别", font));
icell.setHorizontalAlignment(Element.ALIGN_CENTER);
icell.setVerticalAlignment(Element.ALIGN_MIDDLE);
icell.setFixedHeight((float) 25.0);
itable.addCell(icell);


icell = new PdfPCell(new Paragraph(""));
icell.setHorizontalAlignment(Element.ALIGN_CENTER);
icell.setVerticalAlignment(Element.ALIGN_MIDDLE);
icell.setFixedHeight((float) 25.0);
itable.addCell(icell);


icell = new PdfPCell(new Paragraph("婚否", font));
icell.setHorizontalAlignment(Element.ALIGN_CENTER);
icell.setVerticalAlignment(Element.ALIGN_MIDDLE);
icell.setFixedHeight((float) 25.0);
itable.addCell(icell);


icell = new PdfPCell(new Paragraph(""));
icell.setHorizontalAlignment(Element.ALIGN_CENTER);
icell.setVerticalAlignment(Element.ALIGN_MIDDLE);
icell.setFixedHeight((float) 25.0);
icell.setColspan(2);
itable.addCell(icell);


icell = new PdfPCell(new Paragraph(""));
icell.setHorizontalAlignment(Element.ALIGN_CENTER);
icell.setVerticalAlignment(Element.ALIGN_MIDDLE);
icell.setFixedHeight((float) 25.0);
icell.setColspan(2);
itable.addCell(icell);


cell = new PdfPCell(itable);
cell.setPadding(0);
table.addCell(cell); // 第二个格子


itable = new PdfPTable(1);
icell = new PdfPCell(new Paragraph(""));
icell.setHorizontalAlignment(Element.ALIGN_CENTER);
icell.setVerticalAlignment(Element.ALIGN_MIDDLE);
icell.setFixedHeight((float) 25.0);
itable.addCell(icell);


icell = new PdfPCell(new Paragraph(""));
icell.setHorizontalAlignment(Element.ALIGN_CENTER);
icell.setVerticalAlignment(Element.ALIGN_MIDDLE);
icell.setFixedHeight((float) 25.0);
itable.addCell(icell);


icell = new PdfPCell(new Paragraph("毕业学校", font));
icell.setHorizontalAlignment(Element.ALIGN_CENTER);
icell.setVerticalAlignment(Element.ALIGN_MIDDLE);
icell.setFixedHeight((float) 25.0);
itable.addCell(icell);


icell = new PdfPCell(new Paragraph("户籍所在地", font));
icell.setHorizontalAlignment(Element.ALIGN_CENTER);
icell.setVerticalAlignment(Element.ALIGN_MIDDLE);
icell.setFixedHeight((float) 25.0);
itable.addCell(icell);


icell = new PdfPCell(new Paragraph("婚否", font));
icell.setHorizontalAlignment(Element.ALIGN_CENTER);
icell.setVerticalAlignment(Element.ALIGN_MIDDLE);
icell.setFixedHeight((float) 25.0);
itable.addCell(icell);


cell = new PdfPCell(itable);
cell.setPadding(0);
table.addCell(cell); // 第三个格子


itable = new PdfPTable(2);
icell = new PdfPCell(new Paragraph("出生年月", font));
icell.setHorizontalAlignment(Element.ALIGN_CENTER);
icell.setVerticalAlignment(Element.ALIGN_MIDDLE);
icell.setFixedHeight((float) 25.0);
itable.addCell(icell);


icell = new PdfPCell(new Paragraph(""));
icell.setHorizontalAlignment(Element.ALIGN_CENTER);
icell.setVerticalAlignment(Element.ALIGN_MIDDLE);
icell.setFixedHeight((float) 25.0);
itable.addCell(icell);


icell = new PdfPCell(new Paragraph("民族", font));
icell.setHorizontalAlignment(Element.ALIGN_CENTER);
icell.setVerticalAlignment(Element.ALIGN_MIDDLE);
icell.setFixedHeight((float) 25.0);
itable.addCell(icell);


icell = new PdfPCell(new Paragraph(""));
icell.setHorizontalAlignment(Element.ALIGN_CENTER);
icell.setVerticalAlignment(Element.ALIGN_MIDDLE);
icell.setFixedHeight((float) 25.0);
itable.addCell(icell);


icell = new PdfPCell(new Paragraph(""));
icell.setHorizontalAlignment(Element.ALIGN_CENTER);
icell.setVerticalAlignment(Element.ALIGN_MIDDLE);
icell.setFixedHeight((float) 25.0);
icell.setColspan(2);
itable.addCell(icell);


icell = new PdfPCell(new Paragraph(""));
icell.setHorizontalAlignment(Element.ALIGN_CENTER);
icell.setVerticalAlignment(Element.ALIGN_MIDDLE);
icell.setFixedHeight((float) 25.0);
icell.setColspan(2);
itable.addCell(icell);


cell = new PdfPCell(itable);
cell.setPadding(0);
table.addCell(cell); // 第四个格子


cell = new PdfPCell(new Paragraph(""));
icell.setHorizontalAlignment(Element.ALIGN_CENTER);
icell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setPadding(0);
cell.setFixedHeight((float) 100.0);
table.addCell(cell); // 第五个格子


document.add(table);

float[] width={30,30,30,80};
table=new PdfPTable(width);


document.close();


} catch (Exception e) {
e.printStackTrace();
}
}


public static Font ChineseFont() {
BaseFont baseFont = null;
try {
baseFont = BaseFont
.createFont("STSong-Light", "UniGB-UCS2-H", true);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Font chineseFont = new Font(baseFont, 8, Font.NORMAL);
return chineseFont;
}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值