iText打印PDF文档

导入jar包,这两个jar包组合版本不能改,会报错


public String printPDF() throws Exception{
		//创建文档
		Document document = new Document(PageSize.A4, 15, 15, 15, 15);
		//设置输出位置
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
PdfWriter.getInstance(document, bos);
		
		//打开文档
		document.open();
		//内容
		//创建根字体
		BaseFont baseFont = BaseFont.createFont(AsianFontMapper.ChineseSimplifiedFont, AsianFontMapper.ChineseSimplifiedEncoding_H, BaseFont.NOT_EMBEDDED);
		
		/*******大标题********/
		//设置字体
		Font BigTitleFont = new Font(baseFont, 24, Font.BOLD, BaseColor.BLACK);
		//设置内容
		Paragraph BigTitleparagraph = new Paragraph("出货表", BigTitleFont);
		//设置对齐方式
		BigTitleparagraph.setAlignment(Paragraph.ALIGN_CENTER);
		//放入文档
		document.add(BigTitleparagraph);
		
		/*********小标题**********/
		Font titleFont = new Font(baseFont, 12, Font.BOLD, BaseColor.BLACK);
		//设置内容
		//新建一个8列的表格
		PdfPTable table = new PdfPTable(8);
		//设置当前面table的上边距
		table.setSpacingBefore(20f);

		String[] titles = {"客户","订单号","货号","数量","工厂","工厂交期","船期","贸易条款"};
		for (String s : titles) {
			table.addCell(new PdfPCell(new Phrase(s, titleFont)));
		}
		/***********内容**********/
		Font font = new Font(baseFont, 12, Font.NORMAL, BaseColor.BLACK);
		//获取对应船期的货物
		String hql = "from ContractProduct where to_char(contract.shipTime,'yyyy-mm')=?";
		List<ContractProduct> cpList = contractProductService.find(hql, ContractProduct.class, new String[]{inputDate});
		//将数据循环写入
		for(ContractProduct cp:cpList){
			
			table.addCell(new PdfPCell(new Phrase(cp.getContract().getCustomName(), font)));
			table.addCell(new PdfPCell(new Phrase(cp.getContract().getContractNo(), font)));
			table.addCell(new PdfPCell(new Phrase(cp.getProductNo(), font)));
			table.addCell(new PdfPCell(new Phrase(cp.getCnumber()+"", font)));
			table.addCell(new PdfPCell(new Phrase(cp.getFactoryName(), font)));
			table.addCell(new PdfPCell(new Phrase(UtilFuns.dateTimeFormat(cp.getContract().getDeliveryPeriod()), font)));
			table.addCell(new PdfPCell(new Phrase(UtilFuns.dateTimeFormat(cp.getContract().getShipTime()), font)));
			table.addCell(new PdfPCell(new Phrase(cp.getContract().getTradeTerms(), font)));
			
		}
		//添加至文档
		document.add(table);	
		//关闭文档
		document.close();
		//下载
		DownloadUtil downloadUtil = new DownloadUtil();
		downloadUtil.download(bos, ServletActionContext.getResponse(), "出货表.pdf");
		bos.close();
		
		return NONE;//写NONE否则会因为DownloadUtil工具类报response重复响应
		
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值