`

iText创建PDF文件

阅读更多
 	   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());
		}
 

 

 

 

分享到:
评论

相关推荐

    itext 生成pdf 目录

    itext 生成pdf 目录,itext 生成pdf 目录,itext 生成pdf 目录

    iText PDF中文字体文件.rar

    Java使用iText PDF插件导出PDF文档时,需引入中文字体文件,并创建不同大小风格的中文字体

    itext解析pdf全能最新jar

    通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。如果PDF是标记的且包含一个结构树,就可以借助于iText将PDF文档转换成XML文档(这往往取决于PDF文档是如何创建的)。另外还可以从页面...

    itextpdf 导出pdf 表格 自动分页中文 目录

    包含 itext-asian-5.2.0.jar itextpdf-5.5.5.jar

    权威Itext生成pdf

    利用iText五步创建一个PDF文件:helloword。 第一步,创建一个 iTextSharp.text.Document对象的实例: Document document = new Document(); 第二步,为该Document创建一个Writer实例: PdfWriter.getInstance...

    Java实现PDF读写(Itext)与解析XML读写(Dom4j)

    生成PDF文件,引用到的JAR包是itext5.5.1 项目文件为PDFText.java 主要实现功能: 1、创建一个PDF文件:HelloWorld.pdf,给文档设置加密,密码为zhouyl或111 2、将PDF文件压缩成ZIP文件 3、生成一个PDF文件D:\\...

    iText2.1.7资料大全(制作PDF的java开源框)

    iText是一个能够快速产生PDF文件的java类库。iText的java类对于那些要产生包含文本,表格,图形的只读文档是很有用的。它的类库尤其与javaServlet有很好的给合。使用iText与PDF能够使你正确的控制Servlet的输出。 相关...

    Java 使用iText7生成带页码的PDF文件(同时生成目录,但是不会合并两个PDF)

    gradle项目,首次使用请先下载jar包(右键项目→Gradle→Refresh),不然报错!

    MyPdf:Android使用iText生成pdf文件并读取pdf内容

    MyPdf:Android使用iText生成pdf文件并读取pdf内容

    itext-5.3.4 PDF中文語言包

    itext-5.3.4 PDF中文语言包,在利用itext-5.3.4创建PDF文件时,可导入此文件来支持中文显示。

    iText中文帮助文档

    利用iText五步创建一个PDF文件:helloword。 第一步,创建一个 iTextSharp.text.Document对象的实例: Document document = new Document(); 第二步,为该Document创建一个Writer实例: PdfWriter.getInstance...

    itext5pdf表格行变色实现过程源码+doc文档

    内含所有测试代码,java源码,两个java文件即可实现行变色,所到之处,所向披靡。代码下载下来,导入eclipse即可运行。实例呼之欲出。 配合博客: 1.http://blog.csdn.net/ae6623/article/details/8987645 2....

    itext文档组件,生成PDF

    创建PDF文件,可由HTML代码生成PDF文件

    iText7 dll文件

    使用iText7 创建PDF; 设置:Project Settings——&gt;Player——&gt;Api Compatibility Level 设置为 .Net 4.

    OpenPDF是用于创建和编辑PDF文件的开源Java库

    OpenPDF是用于创建和编辑PDF文件的开源Java库,基于 iText 4实现。

    使用poi+itext将excel转为pdf

    看到现在网上excel转pdf的代码很少,在csdn上找到一个还不能用,只能做简单的转换,只好自己写了一个,代码是一个maven工程,用eclipse创建,支持单元格合并等复杂的excel,同时能同步单元格样式到pdf中。...

    poi创建word、生成html、itext将html转换成pdf。pd4ml将jsp转成pdf

    2.通过读取数据库更改原始的.doc文件、读取数据库生成pdf。 -然后更改webRoot下面的.doc文件。然后调用ReadWriteAndDownloadDocAction生成一 个新的word。 -读取数据库文件生成pdf调用createDBPdf类。 ...

    iText中文教程.pdf

    利用iText五步创建一个PDF文件:helloword 。第一步,创建一个iTextSharp.text.Document对象的实例: Document document = new Document(); 第二步,为该Document创建一个 Writer 实例: PdfWriter.getInstance...

    The ABC of ITextPdf

    所以,当你需要你的ASP.Net Web应用程序中包含创建或与PDF文件交互的部分时,就不得不去找可用的第三方组件.使用谷歌可以搜索到在你预算之内的收费组件,当然同时也有一些开源组件。其中之一就是iTextSharp,这个程序...

    itext:通过itext批处理操作更改PDF

    目录路径:它将以递归方式扫描所有查找.pdf文件的目录(警告:这是一个缓慢的操作) 一般流程 生成要以上述任一方式处理的文件的列表 处理每个文件 检查注解链接,进行适当的更改 如果文件至少具有一个更新的链接: ...

Global site tag (gtag.js) - Google Analytics