PDFTable表格生成PDF

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();

	}
}




你可能感兴趣的:(apache,C++,c,C#,J#)