itext的转换pdf的尝试

星期日, 2012-02-12 | Author: Lee | JAVA-and-J2EE | 4,447 views

今天写了个转换成pdf的文档,做个生成个表格的东东.没有做中文支持的问题,支持中文可以下载扩展包处理.
下载itext的jar包:可以去:http://sourceforge.net/projects/itext/files/ 下载更多版本,目前最新为itext-5.1.3.zip
对应的api文档可以翻阅:itext–api

生成个类似下列表格的图片:

代码如下:

package com.liyz.text;
 
import java.awt.Color;
import java.io.FileOutputStream;
 
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
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 TablePdf {
 
	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		TablePdf tp = new TablePdf();
		String pathName = "D:/args.pdf";
		tp.createPdfForReport(pathName);
	}
 
	private void createPdfForReport(String pathName) throws Exception {
		Document document = new Document();
		document.setPageSize(PageSize.A4);
		PdfWriter.getInstance(document, new FileOutputStream(pathName));
		document.open();
		BaseFont bfChinese = BaseFont.createFont();
		Font hBlue = new Font(bfChinese, 8, Font.NORMAL);
		hBlue.setColor(new BaseColor(Color.BLUE));
		 Font headFont = new Font(bfChinese, 10, Font.BOLD);
		// Font headFont1 = new Font(bfChinese, 8, Font.BOLD);
		// Font headFont2 = new Font(bfChinese, 10, Font.NORMAL);
		Font headFont3 = new Font(bfChinese, 8, Font.BOLD);
		Paragraph pr=new Paragraph("ARGS report example");
		pr.setFont(headFont);
		pr.setAlignment(Element.ALIGN_CENTER);
		document.add(pr);
		float[] widths = { 100f, 100f, 100f, 100f };
		PdfPTable table = new PdfPTable(widths);
		table.setSpacingBefore(5f);
		table.setTotalWidth(400);
		table.setLockedWidth(true);
 
		PdfPCell cell = new PdfPCell(new Paragraph("Name", headFont3));
		cell.setRowspan(2);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph("apple", hBlue));
		cell.setRowspan(2);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph("Gender", headFont3));
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph("Male", hBlue));
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph("Phone", headFont3));
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph("+86-139123456789", hBlue));
		table.addCell(cell);
		//----two line
		 cell = new PdfPCell(new Paragraph("Birthday", headFont3));
		 table.addCell(cell);
		 cell = new PdfPCell(new Paragraph("July 4,1986", hBlue));
		 table.addCell(cell);
		 cell = new PdfPCell(new Paragraph("Nationality", headFont3));
		 table.addCell(cell);
		 cell = new PdfPCell(new Paragraph("USA", hBlue));
		 table.addCell(cell);
 
		 cell = new PdfPCell(new Paragraph("Course", headFont3));
		 cell.setColspan(2);
		 cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		 table.addCell(cell);
 
		 cell = new PdfPCell(new Paragraph("Credit", headFont3));
		 table.addCell(cell);
		 cell = new PdfPCell(new Paragraph("Grade", headFont3));
		 table.addCell(cell);
 
		 cell = new PdfPCell(new Paragraph("Computer Graphics:", hBlue));
		 cell.setColspan(2);
		 cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		 table.addCell(cell);
 
		 cell = new PdfPCell(new Paragraph("5", hBlue));
		 table.addCell(cell);
		 cell = new PdfPCell(new Paragraph("A", hBlue));
		 table.addCell(cell);
 
		 cell = new PdfPCell(new Paragraph("Chinese Culture:", hBlue));
		 cell.setColspan(2);
		 cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		 table.addCell(cell);
 
		 cell = new PdfPCell(new Paragraph("2", hBlue));
		 table.addCell(cell);
		 cell = new PdfPCell(new Paragraph("B-", hBlue));
		 table.addCell(cell);
 
		 cell = new PdfPCell(new Paragraph("Basic Swimming:", hBlue));
		 cell.setColspan(2);
		 cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		 table.addCell(cell);
 
		 cell = new PdfPCell(new Paragraph("1", hBlue));
		 table.addCell(cell);
		 cell = new PdfPCell(new Paragraph("B+", hBlue));
		 table.addCell(cell);
 
		document.add(table);
		document.close();
 
	}
}

Tags: , , ,

文章作者: Lee

本文地址: https://www.pomelolee.com/908.html

除非注明,Pomelo Lee文章均为原创,转载请以链接形式标明本文地址

No comments yet.

Leave a comment

Search

文章分类

Links

Meta