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;
}
}
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;
}
}