Document doc = new Document (PageSize.A4);
PdfWriter.getInstance (doc, new FileOutputStream ("c:/test/pdf/test.pdf"));
doc.open ();
//标题字体
BaseFont bfTitle = BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font titleFont = new Font(bfTitle, 18, Font.NORMAL);
//内容字体
BaseFont bfComic = BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font font = new Font(bfComic, 9, Font.NORMAL);
Paragraph titleP=new Paragraph("儿童信息 Child Information\n\n",titleFont);
titleP.setAlignment(titleP.ALIGN_CENTER);
doc.add(titleP);
//生成4列的表格
PdfPTable table = new PdfPTable (4);
table.setWidthPercentage(100);
table.setWidthPercentage(100);
table.addCell (new Paragraph ("Children-id",font));
PdfPCell cell = new PdfPCell (new Paragraph ("09140800002",font));
cell.setColspan (3);
table.addCell (cell);
// 添加第一行
table.addCell (new Paragraph ("Name(CN)",font));
table.addCell (new Paragraph ("党宁生",font));
table.addCell (new Paragraph ("Name(EN)",font));
table.addCell (new Paragraph ("DANG NING SHENG",font));
//添加第二行
table.addCell (new Paragraph ("Sex(CN)",font));
table.addCell (new Paragraph ("男",font));
table.addCell (new Paragraph ("Sex(EN)",font));
table.addCell (new Paragraph ("MALE",font));
//添加第8行
table.addCell (new Paragraph ("Note",font));
cell = new PdfPCell (new Paragraph ("儿童资料",font));
cell.setColspan (3);
table.addCell (cell);
//添加第9行
table.addCell (new Paragraph ("Pictures",font));
Image photo=Image.getInstance("c:/test/pdf/1246588678828.jpg");
cell = new PdfPCell (photo);
cell.setColspan (3);
table.addCell (cell);
for(PdfPRow row:(ArrayList<PdfPRow>)table.getRows()){
for(PdfPCell cells:row.getCells()){
if(cells!=null){
cells.setPadding(10.0f);
}
}
}
doc.add (table);
doc.newPage();
//插入图片
doc.newPage();
Image image1 = Image.getInstance ("c:/test/pdf/1246588315281.jpg");
image1.setAlignment(image1.ALIGN_CENTER);
image1.scaleToFit( PageSize.A4.getHeight(),PageSize.A4.getWidth());
doc.add (image1);
doc.close ();
第二种用PDFPtable 创建表格:
package com.xishui.action;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.taglibs.standard.lang.jstl.EmptyOperator;
import com.itextpdf.text.Element;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class iTextTablePage1 {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(
new FileOutputStream("c:\\iTextTablePage1.pdf"));
document.open();
float[] widths = { 1f };
PdfPTable table = new PdfPTable(widths);
table.setTotalWidth(800);// Total的高度,但不起作用j
PdfPCell cell = new PdfPCell(new Paragraph(
"Student Basic Data................\n\n"));
cell.setFixedHeight(75);// 设置第一个表的高度
table.setWidthPercentage(100);// 设置第一个表的宽度
document.add(table);
table.addCell(cell);
document.add(table);
document.add(new Paragraph("\n\n"));
// 第二个table开始
float[] widths2 = { 0.3f };
PdfPTable table2 = new PdfPTable(widths2);
PdfPCell cell2 = new PdfPCell(new Paragraph("SWOT Analysis\n\n\n"
+ "Strength:\n\n\n\n" + "IELTSWeakness:\n\n\n"
+ "Opportunity:\n\n\n" + "Threat:\n\n\n"
+ "Memo of the meeting:\n\n\n"));
cell2.setFixedHeight(588);// 设置第二个表格的高度
table2.setWidthPercentage(58);// 设置第二个表的宽度
document.add(table2);
document.add(table2);
table2.addCell(cell2);
document.add(table2);
// /
// 第三个测试开始
float[] widths3 = { 1f };
PdfPTable table3 = new PdfPTable(widths3);
PdfPCell cell3 = new PdfPCell(new Paragraph("SWOT Analysis\n\n\n"
+ "Strength:\n\n\n\n" + "IELTSWeakness:\n\n\n"
+ "Opportunity:\n\n\n" + "Threat:\n\n\n"
+ "Memo of the meeting:\n\n\n"));
cell3.setFixedHeight(80);// 设置第三个表格的高度
table3.setWidthPercentage(39);// 设置第三个表的宽度
table3.setHorizontalAlignment(Element.ALIGN_RIGHT); table3.addCell(cell3);
document.add(table3);
document.add(new Paragraph("\n\n"));
float[] widths4 = { 0.5f, 2f, 2f };
PdfPTable table4 = new PdfPTable(widths4);
PdfPCell cell4 = new PdfPCell(new Paragraph("Bath"));
table4.addCell("1");
table4.addCell("Accounting an ");
table4.addCell("City University");
table4.addCell("2");
table4.addCell("City University");
table4.addCell("City University");
table4.addCell("3");
table4.addCell("2.2");
table4.addCell("3.2");
table4.addCell("4");
table4.addCell("City University");
table4.addCell("City University");
table4.setHorizontalAlignment(Element.ALIGN_RIGHT);
table4.setWidths(widths4);
document.add(table4);
document.add(new Paragraph("\n\n"));
//
/* 第2页 */
PdfPTable tb1 = new PdfPTable(3);
cell = new PdfPCell(
new Paragraph(
"CASS Business School, City University - Quantitative Finance!(1)\n\n"));
cell.setColspan(3); // 设为几,就是几列,另加之前设置的表格数量
tb1.setWidthPercentage(100);
tb1.addCell(cell);// 此cell占用3个table
tb1.addCell("3.1\n\n");
tb1.addCell("3.2\n\n");
tb1.addCell("3.3\n\n");
cell = new PdfPCell(new Paragraph(
"profession describe (3) !!\n\n\n\n\n\n\n\n\n\n\n"));
cell.setColspan(3);
tb1.addCell(cell);
document.add(tb1);
document.add(new Paragraph("\n\n"));
PdfPTable tb2 = new PdfPTable(3);
cell = new PdfPCell(
new Paragraph(
"CASS Business School, City University - Quantitative Finance(2)\n\n"));
cell.setColspan(3); // 设为几,就是几列,另加之前设置的表格数量
tb2.setWidthPercentage(100);
tb2.addCell(cell);// 此cell占用3个table
tb2.addCell("2.1\n\n");
tb2.addCell("2.2\n\n");
tb2.addCell("2.3\n\n");
cell = new PdfPCell(new Paragraph(
"profession describe (2) \n\n\n\n\n\n\n\n\n\n\n"));
cell.setColspan(3);
tb2.addCell(cell);
document.add(tb2);
document.add(new Paragraph("\n\n"));
PdfPTable tb3 = new PdfPTable(3);
cell = new PdfPCell(
new Paragraph(
"CASS Business School, City University - Quantitative Finance!(3)\n\n"));
cell.setColspan(3); // 设为几,就是几列,另加之前设置的表格数量
tb3.setWidthPercentage(100);
tb3.addCell(cell);// 此cell占用3个table
tb3.addCell("3.1\n\n");
tb3.addCell("3.2\n\n");
tb3.addCell("3.3\n\n");
cell = new PdfPCell(new Paragraph(
"profession describe (3) !!\n\n\n\n\n\n\n\n\n\n\n"));
cell.setColspan(3);
tb3.addCell(cell);
document.add(tb3);
document.add(new Paragraph("\n\n"));
} catch (DocumentException de) {
System.err.println(de.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
}
System.out.println("iTextTablePage1.pdf to c:\\");
document.close();
}
}
第二页
package com.xishui.action;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class iTextTablePage2 {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(
new FileOutputStream("c:\\iTextTable_Page_2.pdf"));
document.open();
document.add(new Paragraph("Potential programs for you")); // 小标题
PdfPTable table = new PdfPTable(3);
PdfPCell cell = new PdfPCell(
new Paragraph(
"CASS Business School, City University - Quantitative Finance(1)\n\n"));
cell.setColspan(3); // 设为几,就是几列,另加之前设置的表格数量
table.addCell(cell);// 此cell占用3个table
table.addCell("1.1\n\n");
table.addCell("2.1\n\n");
table.addCell("3.1\n\n");
cell = new PdfPCell(new Paragraph(
" profession describe (1) !!\n\n\n\n\n\n\n\n\n\n\n"));
cell.setColspan(3);
table.addCell(cell);
document.add(table);
document.add(new Paragraph("\n\n"));
PdfPTable tb2 = new PdfPTable(3);
cell = new PdfPCell(
new Paragraph(
"CASS Business School, City University - Quantitative Finance(2)\n\n"));
cell.setColspan(3); // 设为几,就是几列,另加之前设置的表格数量
tb2.addCell(cell);// 此cell占用3个table
tb2.addCell("2.1\n\n");
tb2.addCell("2.2\n\n");
tb2.addCell("2.3\n\n");
cell = new PdfPCell(new Paragraph(
"profession describe (2) \n\n\n\n\n\n\n\n\n\n\n"));
cell.setColspan(3);
tb2.addCell(cell);
document.add(tb2);
document.add(new Paragraph("\n\n"));
PdfPTable tb3 = new PdfPTable(3);
cell = new PdfPCell(
new Paragraph(
"CASS Business School, City University - Quantitative Finance!(3)\n\n"));
cell.setColspan(3); // 设为几,就是几列,另加之前设置的表格数量
tb3.addCell(cell);// 此cell占用3个table
tb3.addCell("3.1\n\n");
tb3.addCell("3.2\n\n");
tb3.addCell("3.3\n\n");
cell = new PdfPCell(new Paragraph(
"profession describe (3) !!\n\n\n\n\n\n\n\n\n\n\n"));
cell.setColspan(3);
tb3.addCell(cell);
document.add(tb3);
document.add(new Paragraph("\n\n"));
} catch (DocumentException de) {
System.err.println(de.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
}
System.out.println("iTextTable_Page_2.pdf to c:\\");
// step 5: we close the document
document.close();
}
}
第三种 用 Table 创建表格:
try {
Document document = new Document(PageSize.A4, 20, 20, 20, 20);
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream("c:\\Temp\\ITextTest.pdf"));
document.open();
BaseFont bfChinese;
bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
false);
Font fontChinese = new Font(bfChinese, 9, Font.BOLD, Color.black);
Table t = new Table(12, 2); //2行12列
t.setBorderColor(new Color(220, 255, 100));
int width[] = { 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, };
t.setWidths(width);
t.setWidth(100); // 占页面宽度 %
t.setPadding(1);
t.setSpacing(0);
t.setBorderWidth(2);
Cell c1 = new Cell("header1");
t.addCell(c1);
c1 = new Cell("Header2");
t.addCell(c1);
c1 = new Cell("Header3");
t.addCell(c1);
c1 = new Cell("Header4");
t.addCell(c1);
c1 = new Cell("Header5");
t.addCell(c1);
c1 = new Cell("Header6");
t.addCell(c1);
c1 = new Cell("Header7");
t.addCell(c1);
c1 = new Cell("Header8");
t.addCell(c1);
c1 = new Cell("Header9");
t.addCell(c1);
c1 = new Cell("Header10");
t.addCell(c1);
c1 = new Cell("Header11");
t.addCell(c1);
c1 = new Cell("Header12");
t.addCell(c1);
int k = 0;
while (k < 3) {
for (int q = 0; q < 12; q++) {
Paragraph par = new Paragraph(k + "-" + q, fontChinese);
c1 = new Cell(par);
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
t.addCell(c1);
}
k++;
}
c1 = new Cell("1");
t.addCell(c1);
c1 = new Cell("2");
t.addCell(c1);
c1 = new Cell("3");
t.addCell(c1);
c1 = new Cell("4");
c1.setColspan(4);
c1.setRowspan(7);
t.addCell(c1);
c1 = new Cell("5");
c1.setColspan(5);
c1.setRowspan(4);
t.addCell(c1);
c1 = new Cell("6");
t.addCell(c1);
c1 = new Cell("7");
t.addCell(c1);
c1 = new Cell("8");
t.addCell(c1);
c1 = new Cell("9");
t.addCell(c1);
c1 = new Cell("10");
t.addCell(c1);
c1 = new Cell("11");
t.addCell(c1);
c1 = new Cell("12");
t.addCell(c1);
c1 = new Cell("13");
t.addCell(c1);
c1 = new Cell("14");
t.addCell(c1);
c1 = new Cell("15");
t.addCell(c1);
c1 = new Cell("16");
t.addCell(c1);
c1 = new Cell("17");
t.addCell(c1);
c1 = new Cell("18");
t.addCell(c1);
c1 = new Cell("19");
t.addCell(c1);
c1 = new Cell("20");
t.addCell(c1);
c1 = new Cell("21");
t.addCell(c1);
c1 = new Cell("22");
t.addCell(c1);
c1 = new Cell("23");
t.addCell(c1);
c1 = new Cell("24");
t.addCell(c1);
c1 = new Cell("25");
t.addCell(c1);
c1 = new Cell("26");
c1.setColspan(5);
c1.setRowspan(2);
t.addCell(c1);
c1 = new Cell("27");
t.addCell(c1);
c1 = new Cell("28");
t.addCell(c1);
c1 = new Cell("29");
t.addCell(c1);
document.add(t);
document.close();
} catch (Exception e2) {
System.out.println(e2.getMessage());
}