itext生成pdf

公司的最近有些业务要生成pdf,这里我们用第三方包 itext

 <!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
	<dependency>
	    <groupId>com.itextpdf</groupId>
	    <artifactId>itextpdf</artifactId>
	    <version>5.5.13</version>
	</dependency>
    

itext生成pdf 简单demo 

package com.bocan.pdf;

import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
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 PdfOne {
	public static void main(String[] args) {
		//System.out.println("hello world");
		
		Document document = new Document(PageSize.A4,20,20,20,100);
		
		try {
			//写入文本到文件中   文件生成地址: C:\Users\asus\Desktop\printPdf
			PdfWriter.getInstance(document, new FileOutputStream("C:\\Users\\asus\\Desktop\\printPdf\\"+UUID.randomUUID().toString()+".pdf"));
			
			//打开文本
			document.open();
			BaseFont baseFont = BaseFont.createFont("C:/Windows/Fonts/SIMYOU.TTF",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
	        BaseFont baseFontHei = BaseFont.createFont("C:/Windows/Fonts/simhei.ttf",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
			
	        //日期 和 审批
	        Paragraph paragraph = new Paragraph();
	        //1 2 3  中右左
	        paragraph.setAlignment(3);  //对齐方式
	        Font font = new Font(baseFont);
	        font.setSize(9); //设置字体大小
	        paragraph.setFont(font);
	        paragraph.setSpacingAfter(10);  //设置当前行 与  下一行的 行距
	        Date date = new Date();
	        SimpleDateFormat sdf = new SimpleDateFormat("YYYY/MM/dd");
	        String timeStr = sdf.format(date);
	        Chunk chunk = new Chunk("           "+timeStr);
	        chunk.append("                                           审核:");
	        paragraph.add(chunk);
	        document.add(paragraph);
	        
	        //标题
	        Paragraph paragraph1 = new Paragraph();
	        Font fontTitle = new Font(baseFontHei);
	        fontTitle.setSize(15);
	        paragraph1.setFont(fontTitle);
	        paragraph1.setAlignment(1);  //对齐方式
	        //paragraph1.setSpacingAfter(20);
	        Chunk chunk1 = new Chunk("政策兑现收件回执");
	        
	        paragraph1.add(chunk1); 
	        document.add(paragraph1);
	        
	        
	        //申请编号
	        Paragraph paragraph2 = new Paragraph();
	        paragraph2.setAlignment(3);
	        paragraph2.setFont(font);
	        paragraph2.setSpacingAfter(5);
	        paragraph2.add("           申请编号:"+date.getTime());
	        document.add(paragraph2);
	       
	        PdfPTable bigtable = new PdfPTable(1);
	        bigtable.setTotalWidth(455);
	        bigtable.setLockedWidth(true);
	        
	        //或一个 1列的大表格
	       // PdfPTable table = new PdfPTable(1);
	         //填写个人信息  2列的表格
	        PdfPTable personInfo = new PdfPTable(2);
	      
	        personInfo.setTotalWidth(455);
	        personInfo.setLockedWidth(true);
	        PdfPCell cell01 = drawPdfPCell("基本信息",baseFont,9,3,10);
	        personInfo.addCell(cell01);
	        PdfPCell cell01a = drawPdfPCell("",baseFont,9,3,10);
	        personInfo.addCell(cell01a);
	        
	        PdfPCell cell02 = drawPdfPCell("申请人(自然人、法人或其他组织):  广州市全诚货运代理有限公司",baseFont,9,3,10);
	        cell02.setColspan(2);
	        personInfo.addCell(cell02);
	        
//	        PdfPCell cell02a = drawPdfPCell("",baseFont,9,3,10);
//	        personInfo.addCell(cell02a);
	        
	       
	        PdfPCell cell03 = drawPdfPCell("具体经办人:芦苇",baseFont,9,3,10);
	       
	        PdfPCell cell04 = drawPdfPCell("具体经办人联系方式:18588732123",baseFont,9,3,10);
	        personInfo.addCell(cell03);
	        personInfo.addCell(cell04);
	        
	        PdfPCell cell05 = drawPdfPCell("申请事项名称:abc",baseFont,9,3,10);
	        PdfPCell cell06 = drawPdfPCell("业务主管部门:口岸工作室",baseFont,9,3,10);
	        personInfo.addCell(cell05);
	        personInfo.addCell(cell06);
	        
	        PdfPCell cell07 = drawPdfPCell("部门联系方式:",baseFont,9,3,10);
	        PdfPCell cell08 = drawPdfPCell("承诺办理时限:42工作日",baseFont,9,3,10);
	        personInfo.addCell(cell07);
	        personInfo.addCell(cell08);
	        
	        PdfPCell cell09 = drawPdfPCell("收取材料",baseFont,9,3,10);
	        PdfPCell cell10 = drawPdfPCell("",baseFont,9,3,10);
	        personInfo.addCell(cell09);
	        personInfo.addCell(cell10);
	        
	        PdfPCell cell11 = drawPdfPCell("申请人提交材料",baseFont,9,3,10);
	        cell11.setPaddingBottom(10f);
	        PdfPCell cell12 = drawPdfPCell("",baseFont,9,3,10);
	        cell12.setPaddingBottom(10f);
	        personInfo.addCell(cell11);
	        personInfo.addCell(cell12);
	        
	        //每行最多的最大长度为66
	        //材料
	        PdfPCell materials01 = drawPdfPCell("1.航运物流企业仓储补贴申请表(见附件1);(原件一式两份并加盖单位公章)",baseFont,7,3,10);
            materials01.setColspan(2);
	      
	        materials01.setPaddingBottom(0);
	        personInfo.addCell(materials01);
	      
	        
	        PdfPCell materials02 = drawPdfPCell("2.营业执照(复制件一式两份,并加盖单位公章,核原件)",baseFont,7,3,10);
	        materials02.setColspan(2);
	        materials02.setPaddingBottom(0);
	        personInfo.addCell(materials02);
	       
	        
	        PdfPCell materials03 = drawPdfPCell("3.投资协议或承诺书(见附件2);(原件一式两份并加盖单位公章)",baseFont,7,3,10);
	        materials03.setColspan(2);
	        materials03.setPaddingBottom(0);
	        personInfo.addCell(materials03);
	       
	        
	        PdfPCell materials04 = drawPdfPCell("4.场地说明,应包括地点、自用仓储面积、用途、使用部门,以及现场照片;(原件一式两份并加盖单位公章)",baseFont,7,3,10);
	        materials04.setColspan(2);
	        materials04.setPaddingBottom(0);
	        personInfo.addCell(materials04);
	      
	        
	        PdfPCell materials05 = drawPdfPCell("5.场地租赁合同、租金发票(发票按月份进行标记),对码头内采取堆存计价的汽车贸易企业需提供租赁合同、租金发票和码头《港口经营许可证》",baseFont,7,3,10);
	        materials05.setColspan(2);
	        materials05.setPaddingBottom(0);
	        personInfo.addCell(materials05);
	        
	       
	        PdfPCell materials05b = drawPdfPCell(");(复印件一式两份 并加盖单位公章)",baseFont,7,3,10);
	        materials05b.setColspan(2);
	        materials05b.setPaddingBottom(0);
	        personInfo.addCell(materials05b);
	        
	       
	        
	        PdfPCell materials06 = drawPdfPCell("6.企业或机构的基本开户许可证;(复印件一式两份并加盖单位公章)",baseFont,7,3,10);
	        materials06.setColspan(2);
	        materials06.setPaddingBottom(0);
	        personInfo.addCell(materials06);
	      
	        
	        PdfPCell materials07 = drawPdfPCell("7.企业办理人委托书原件、办理人身份证复印件(核原件);(一式两份并加盖单位公章)",baseFont,7,3,10);
	        materials07.setColspan(2);
	        personInfo.addCell(materials07);
	      
	        
	        
	        PdfPCell signature01 = drawPdfPCell("签名确认",baseFont,9,3,10);
	        PdfPCell signature01a = drawPdfPCell("",baseFont,9,3,10);
	        personInfo.addCell(signature01);
	        personInfo.addCell(signature01a);
	        
	        PdfPCell signature02 = drawPdfPCell("政策兑现窗口经办人(签名):",baseFont,9,3,10);
	        PdfPCell signature02a = drawPdfPCell("",baseFont,9,3,10);
	        signature02.setPaddingBottom(15);
	        signature02a.setPaddingBottom(15);
	        personInfo.addCell(signature02);
	        personInfo.addCell(signature02a);
	        
	        
	        PdfPCell contact = drawPdfPCell("联系方式:",baseFont,9,3,10);
	        PdfPCell contact0a = drawPdfPCell("",baseFont,9,3,10);
	        contact.setPaddingBottom(20);
	        contact0a.setPaddingBottom(20);
	        personInfo.addCell(contact);
	        personInfo.addCell(contact0a);
	        
	        //公章
	        PdfPCell cachet = drawPdfPCell("",baseFont,9,3,10);
	        PdfPCell cachetContent = drawPdfPCell("政策兑现收件专用章",baseFont,9,3,10);
	        cachet.setPaddingBottom(20f);
	        cachetContent.setPaddingBottom(20f);
	        personInfo.addCell(cachet);
	        personInfo.addCell(cachetContent);
	        
	       
	        //时间
	        SimpleDateFormat sdf02 = new SimpleDateFormat("YYYY年MM月dd日HH时mm分");
	        String dateStr = sdf02.format(date);
	        PdfPCell dateTime = drawPdfPCell("",baseFont,9,3,10);
	        PdfPCell dateTimeContent = drawPdfPCell(dateStr,baseFont,9,3,10);
	        personInfo.addCell(dateTime);
	        personInfo.addCell(dateTimeContent);
	        
	        //重要说明
	        PdfPCell explain = drawPdfPCell("重要说明",baseFont,9,3,10);
	        PdfPCell explain0a = drawPdfPCell("",baseFont,9,3,10);
	        personInfo.addCell(explain);
	        personInfo.addCell(explain0a);
	        
	        
	        
	       // PdfPTable pdfPTable = new PdfPTable(1);
	       // personInfo = new PdfPTable(1);
	        // pdfPTable.
	        //每行最多77个字符
	        PdfPCell explainContent01 = drawPdfPCell("1.承诺办理实现说明:审批过程中如需专家论、网上公示等特殊环节,按有关规定执行,特殊环节所需时限不包括在承诺办理时限内。",baseFont,6,3,10);
	        explainContent01.setColspan(2); 
	        personInfo.addCell(explainContent01);
	        
	        
	        PdfPCell explainContent02 = drawPdfPCell("2.申请人(自然人,法人或其他组织)及具体经办人应当如实提交有关材料和反映真实情况,并对申请材料实质内容的真实性负责。",baseFont,6,3,10);
	        explainContent02.setColspan(2);
	        personInfo.addCell(explainContent02);
	        
	        
	        PdfPCell explainContent03 = drawPdfPCell("3.广州南沙政务服务中心政策兑现窗口仅负责对有关材料进行审查,材料实质审查由业务主管部门负责。本次收件不代表同意兑现,具体解释权归南沙开发区管委会、南沙区",baseFont,6,3,10);
	        explainContent03.setColspan(2);
	        personInfo.addCell(explainContent03);
	        
	        
	        PdfPCell explainContent04 = drawPdfPCell("政府所有。",baseFont,6,3,10);
	        explainContent04.setColspan(2);
	        explainContent04.setPaddingBottom(15);
	        personInfo.addCell(explainContent04);
	        
	        
	        //本人已清楚知悉以上内容
	        PdfPCell explianContentSure = drawPdfPCell("本人已清楚知悉以上内容",baseFont,6,2,10);
	        PdfPCell explianContentSureA = drawPdfPCell("",baseFont,6,3,10);
	       
	        explianContentSure.setPaddingBottom(15);
	        explianContentSureA.setPaddingBottom(15);
	        personInfo.addCell(explianContentSure);
	        personInfo.addCell(explianContentSureA);
	        
	        //具体经办人(签名)
	        PdfPCell explianContentSureB = drawPdfPCell("",baseFont,6,3,10);
	        PdfPCell explianContentSureBa= drawPdfPCell("具体经办人(签名):",baseFont,6,3,10);
	        explianContentSureB.setPaddingBottom(10);
	        explianContentSureB.setPaddingBottom(10);
	        personInfo.addCell(explianContentSureB);
	        personInfo.addCell(explianContentSureBa);
	        
	        //bottom date  底部时间填写
	        PdfPCell bottomDate= drawPdfPCell("",baseFont,6,3,10);
	        PdfPCell bottomDateContent= drawPdfPCell("   年     月     日",baseFont,6,3,10);
	        personInfo.addCell(bottomDate);
	        personInfo.addCell(bottomDateContent);
	        //备注
	        PdfPCell markContent= drawPdfPCell("备注:本通知书一式两份,一份申请人,一份有政务中心留存。",baseFont,6,3,10);
	        //PdfPCell mark= drawPdfPCell("",baseFont,6,3,10);
	        markContent.setColspan(2);
	        markContent.setPaddingBottom(3f);
	        //mark.setPaddingBottom(3f);
	        personInfo.addCell(markContent);
	       // personInfo.addCell(mark);
	        
	        bigtable.addCell(personInfo);
	        
	        document.add(bigtable);
	        
	        //窗口管理岗
	        //申请编号
	        Paragraph paragraph4 = new Paragraph();
	        paragraph4.setAlignment(3);
	        paragraph4.setSpacingBefore(-8);
	        font.setSize(6);
	        paragraph4.setFont(font);
	       // paragraph4.setSpacingAfter(5);
	        Chunk bottom = new Chunk();
	        for (int i = 0; i < 17; i++) {
	        	bottom.append(" ");
	        }
	        bottom.append("窗口管理岗");
	        for (int i = 0; i < 90; i++) {
				bottom.append(" ");
			}
	        bottom.append("工号:chenxi");
	        paragraph4.add(bottom);
	        document.add(paragraph4);
	        
	        
	        //关闭文本  
	        document.close();  
	        System.out.println("成功");
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	/**
	 * 
	 * Method description : 画一行中的一列(格子)
	 * Author:        linyi                
	 * Create Date:   2018年7月24日 下午2:17:07
	 * History:  2018年7月24日 下午2:17:07   linyi   Created.
	 * @param cellText 格子的文字
	 * @param baseFont 字体类型
	 * @param size 字体大小
	 * @param alignment 对齐方式
	 * @return
	 * @throws Exception
	 *
	 */
	private static PdfPCell drawPdfPCell(String cellText,BaseFont baseFont,float size,int alignment,int minimumHeight ) throws Exception{
		//为null会报错  防止报错
		if(cellText==null){
			cellText=" ";
		}
		//表格开始
        Paragraph paragraph = new Paragraph();
        paragraph.setAlignment(alignment);  //对齐方式
        Font font = new Font(baseFont);//字体
        font.setSize(size);//字体大小
        paragraph.setFont(font);//设置段落字体
        Chunk chunk = new Chunk(cellText);  
        paragraph.add(chunk);  
        PdfPCell cell = new PdfPCell();
        cell.setUseAscender(true);
        cell.setVerticalAlignment(cell.ALIGN_MIDDLE);//设置cell垂直居中
        cell.setMinimumHeight(minimumHeight);//设置单元格最小高度,当前行最小高度
        cell.disableBorderSide(15);  //无边框
        cell.setPaddingBottom(5f);  //设置表格与下一行表格的行距
        cell.addElement(paragraph);
		return cell;
	}
}

生成效果图

 

参考资料:

https://blog.csdn.net/weixin_37020977/article/details/81254613

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值