itext2.1.7动态生成PDF实例

itext2.1.7动态生成PDF实例

itext2.1.7 API 见附件

 

package com.aegon_cnooc.giss.util;

import java.awt.Color;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.aegon_cnooc.giss.vo.Preservation;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.AcroFields;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.PdfWriter;


public class PDFGeneratorUtil {
	// 模板存放的目录
	public static final String TEMPLATE_PDF_PATH = Constants.TEMPLATE_PDF_PATH;
	// 生成PDF存放路径
	public static final String PDF_PATH = Constants.PDF_PATH;
    public static String createPreservationListPDF(String fileName, Map<String, String> baseInfo, String[] tableHeads, List<Preservation> tableDataList) throws Exception{
    	//Document doc = new Document(PageSize.B5);
    	String sysdate = GetSysdate();//获取系统当前时间
		String fullFileName = PDF_PATH + "\\" + sysdate + "_" +fileName+ ".pdf";//文件全路径
		
		Rectangle rect = new Rectangle(842.0F, 595.0F);//设置页面大小,在源码中有PageSize.A4  = new Rectangle(595.0F, 842.0F)
        Document document = new Document(rect, 80, 80, 90, 25); //设置成A4纸大小,内容距左右上下的距离
        
        //String fullFileName = PDF_PATH + fileName+".pdf"; //文件全路径
        try {
        	PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(fullFileName));
           
            // 插入一个段落
            //设置中文字体
            BaseFont baseFontChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            //1.标题字体
            Font headFont =  new  Font(baseFontChinese , 16 , Font.BOLD);;
            //2.正文字体
            Font contentFontSecond =  new  Font(baseFontChinese , 10 , Font.NORMAL);
            
            //设置页脚
            HeaderFooter footer=new HeaderFooter(new Phrase("保险公司盖章:                                               \n\n   ",contentFontSecond),false);  
            /** 
             * 0是靠左 
             * 1是居中 
             * 2是居右 
             */  
            footer.setBorderColor(Color.black);
            footer.setBorder(Rectangle.TOP);  
            footer.setAlignment(2);//设置右对齐
            document.setFooter(footer);  
            
            
            document.open();//打开PDF文件进行编辑
            //设置标题
            Paragraph head1 = new Paragraph("团险保全明细", headFont);
            head1.setAlignment(1);//设置段落居中
            head1.setSpacingAfter(30);  
            document.add(head1);
           
            //使用表格布局
            PdfPTable baseInfoTable = new PdfPTable(6); //设置表格有多少列
            int width[] = {6,14,8,50,8,14};//设置每列宽度比例   
            baseInfoTable.setWidths(width); 
            baseInfoTable.setWidthPercentage(100);//占页面宽度比例 
            //设置表格中单元格的属性,所有单元格都可以使用
            PdfPCell pdfPCell = new PdfPCell();//创建一个单元格
	       	//设置字体水平居中
            pdfPCell.setHorizontalAlignment(PdfPCell.LEFT);//设置水平对齐
	       	//设置字体垂直对齐居中
            pdfPCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
            pdfPCell.setBorder(0);
            
            pdfPCell.setPhrase(new Phrase("保单号:" , contentFontSecond));
            baseInfoTable.addCell(pdfPCell);
            pdfPCell.setPhrase(new Phrase(baseInfo.get("policyCode"), contentFontSecond));
            baseInfoTable.addCell(pdfPCell);
            pdfPCell.setPhrase(new Phrase("投保单位:" , contentFontSecond));
            baseInfoTable.addCell(pdfPCell);
            pdfPCell.setPhrase(new Phrase(baseInfo.get("companyName"), contentFontSecond));
            baseInfoTable.addCell(pdfPCell);
            pdfPCell.setPhrase(new Phrase(""));//设置空白列
            baseInfoTable.addCell(pdfPCell);
            pdfPCell.setPhrase(new Phrase(""));//设置空白列
            baseInfoTable.addCell(pdfPCell);
            //第二行
            pdfPCell.setBorder(Rectangle.BOTTOM);//设置底部颜色,实现一条横线的效果
            pdfPCell.setBorderColor(Color.BLACK);
            pdfPCell.setPhrase(new Phrase("批单号:" , contentFontSecond));
            baseInfoTable.addCell(pdfPCell);
            pdfPCell.setPhrase(new Phrase(baseInfo.get("policyChangeId"), contentFontSecond));
            baseInfoTable.addCell(pdfPCell);
            pdfPCell.setPhrase(new Phrase("保全单位:" , contentFontSecond));
            baseInfoTable.addCell(pdfPCell);
            pdfPCell.setPhrase(new Phrase("团单短险批次增加被保人", contentFontSecond));
            baseInfoTable.addCell(pdfPCell);
            pdfPCell.setPhrase(new Phrase("生效日期:",contentFontSecond));
            baseInfoTable.addCell(pdfPCell);
            pdfPCell.setPhrase(new Phrase(baseInfo.get("effectiveDate"), contentFontSecond));
            baseInfoTable.addCell(pdfPCell);
            baseInfoTable.setSpacingAfter(30);//设置间距
            document.add(baseInfoTable);
            
            
            
            
            //清单
            PdfPTable presListTable = new PdfPTable(tableHeads.length); //设置表格有多少列
            int presWidth[] = {16,16,17,17,17,17};//设置每列宽度比例   
            presListTable.setWidths(presWidth); 
            presListTable.setWidthPercentage(100);//占页面宽度比例 
            //设置表格中单元格的属性,所有单元格都可以使用
            //PdfPCell pdfPCell = new PdfPCell();//创建一个单元格
	       	//设置字体水平居中
            pdfPCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);//设置水平对齐
	       	//设置字体垂直对齐居中
            pdfPCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
            pdfPCell.setBorder(0);
            //保险方案内容:
            for(String head : tableHeads){
            	 pdfPCell.setPhrase(new Phrase(head , contentFontSecond));
            	 presListTable.addCell(pdfPCell);
            }
            presListTable.completeRow();
            for(Preservation preservation : tableDataList){
	           	pdfPCell.setPhrase(new Phrase(preservation.getInsuredName(), contentFontSecond));
	           	presListTable.addCell(pdfPCell);
	        	pdfPCell.setPhrase(new Phrase(preservation.getInsuredIdcard(), contentFontSecond));
	           	presListTable.addCell(pdfPCell);
	        	pdfPCell.setPhrase(new Phrase(preservation.getJobLevel(), contentFontSecond));
	           	presListTable.addCell(pdfPCell);
	        	pdfPCell.setPhrase(new Phrase(preservation.getPackageName(), contentFontSecond));
	           	presListTable.addCell(pdfPCell);
	        	pdfPCell.setPhrase(new Phrase(preservation.getPolicyFee(), contentFontSecond));
	           	presListTable.addCell(pdfPCell);
	        	pdfPCell.setPhrase(new Phrase(preservation.getEffectiveDate(), contentFontSecond));
	           	presListTable.addCell(pdfPCell);
	           	presListTable.completeRow();//完成行
	           
            }  
            document.add(presListTable);
            
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
        return fullFileName;
    }


	public static String GetSysdate() {
		java.text.DateFormat df = new java.text.SimpleDateFormat("yyyyMMddhhmmssSSS");
		java.util.Date date = new java.util.Date();
		String sysdate = df.format(date);
		return sysdate;
	}
	// 获取pdf模板的全路径
	public static String GetPdfPath(String fileName) {
		String pdfpath = TEMPLATE_PDF_PATH + "\\"+ fileName + ".pdf";
		return pdfpath;
	}
}

 

生成的PDF效果图如附件demo。。。。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您可以使用iText 2.1.7来生成PDF文件的页码。以下是一个简单的示例代码: ``` import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.PageSize; import com.lowagie.text.pdf.PdfContentByte; import com.lowagie.text.pdf.PdfNumber; import com.lowagie.text.pdf.PdfWriter; import java.io.FileNotFoundException; import java.io.FileOutputStream; public class PageNumberExample { public static void main(String[] args) { Document document = new Document(PageSize.A4); try { PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("example.pdf")); document.open(); PdfContentByte cb = writer.getDirectContent(); int totalPages = writer.getPageNumber(); for (int currentPage = 1; currentPage <= totalPages; currentPage++) { document.newPage(); cb.beginText(); cb.moveText(550, 30); cb.setFontAndSize(cb.getFont(), 10); cb.showText(String.format("Page %d of %d", currentPage, totalPages)); cb.endText(); } document.close(); writer.close(); } catch (DocumentException | FileNotFoundException ex) { ex.printStackTrace(); } } } ``` 在此示例中,我们创建了一个`Document`对象,并使用`PdfWriter`将其写入到文件中。我们使用`PdfContentByte`在每个页面上添加页码。在`for`循环中,我们遍历所有页面,为每个页面添加一个新的页面和页码。最后,我们关闭文档和写入器。 请注意,此示例是基于iText 2.1.7版本创建的。如果您使用的是不同版本的iText,则可能需要更改代码。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值