使用itext創建PDF文檔-導出大表格-每頁顯示標題-項目實戰

導出PDF文件 


下載 itext  jar 包 , 【開源中國】 的地址: https://www.oschina.net/p/itext  


import java.io.*;
import java.util.*;
import java.util.List;

import com.lowagie.text.*;
import com.lowagie.text.pdf.*;

/** 
 *  創建Pdf文檔
 */
public class HelloPdf{

	//先建立Document對象
	Document document = new Document(PageSize.A4, 36.0F, 36.0F, 36.0F, 36.0F);  

	String filenameuuid = UUID.randomUUID().toString();

	public  void getPdfIo() throws DocumentException, IOException{  


		//字體的定義
		BaseFont fontChinesetitle = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);  

		BaseFont fontChinesecontent = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);

		Font FontChinese = new Font(fontChinesetitle, 12, Font.NORMAL);

		Font chinese = new Font(fontChinesecontent, 10, Font.NORMAL);   

		//保存本地指定路徑  
		saveLocal();

		//設置頁眉
		Paragraph  para  = new Paragraph("會議管理綜合查詢" , FontChinese);
		HeaderFooter header=new HeaderFooter(para,false);  
		header.setBorder(Rectangle.NO_BORDER);  //設置爲沒有邊框
		header.setAlignment(1);  
		document.setHeader(header);  


		//設置頁腳
		HeaderFooter footer_right = new HeaderFooter(new Paragraph("",chinese), true);      
		footer_right.setAlignment(1);  
		footer_right.setBorder(Rectangle.NO_BORDER);    
		document.setFooter(footer_right);    

		//打開文件
		document.open();  

		//生成八列表格
		PdfPTable table = new PdfPTable(8);

		//設置表格具體寬度  100爲整張寬度
		table.setTotalWidth(100);

		//設置每一列所佔的長度
		table.setWidths(new float[]{6,6,13,12,12,15,24,12});

		//設置表頭
		String titleName[] = {"序號","上下午","會議時間","會議主題","地  點","主辦處/召集人","參會範圍","備註"};

		for(int i=0;i<titleName.length;i++){
			PdfPCell celltitle = new PdfPCell();
			celltitle.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
			celltitle.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
			Paragraph  paratitle  = new Paragraph( titleName[i] ,chinese);
			paratitle.setAlignment(1);
			celltitle.addElement(paratitle);
			table.addCell(celltitle);
		}

		List list = new ArrayList();
		Vector vector = new Vector(); 
		vector.add("1");
		vector.add("上午");
		vector.add("2011-04-11 14:30—2011-04-11 17:00");
		vector.add("會議");
		vector.add("會議室");
		vector.add("書記");
		vector.add("所有人");
		vector.add("空");

		for(int i=0; i<50; i++){
			list.add(vector);
		}

		//獲取要寫入的內容
		Vector info = new Vector(); 

		for(int i=0; i<list.size(); i++){
			info = (Vector)list.get(i);
			for(int j=0; j<8; j++){
				PdfPCell cellcon = new PdfPCell();
				cellcon.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
				cellcon.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
				Paragraph  paracon  = new Paragraph( (String)info.get( j ) ,chinese);
				paracon.setAlignment(1);
				cellcon.addElement(paracon);
				table.addCell(cellcon);
			}
		}

		//將表格的第一行設置爲表頭 讓它在每一頁都顯示出來
		table.setHeaderRows(1);

		//將表格添加到文檔中
		document.add(table);

		//關閉文檔  
		document.close();  
		
		System.out.println("ok");

	}  


	//指定一個文件進行保存 這裏吧文件保存到D盤的text.pdf  
	public void saveLocal() throws IOException, DocumentException{  

		//直接生成PDF 制定生成到D盤test.pdf  
		File file = new File("D:\\"+filenameuuid+".pdf");  
		file.createNewFile();  
		PdfWriter.getInstance(document, new FileOutputStream(file));  

	}
	
	
	//返回路徑
	public String returnFileName(){
		return filenameuuid;
    }


	public static void main(String[] args) {

		HelloPdf exportPdf = new HelloPdf();

		try {
			exportPdf.getPdfIo();
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}


}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章