ITEXT5 批量导出、手写pdf 模板

***IText5 根据模板批量导出pdf

禁止抄袭,请填写转载地址

ITEXT 记录生成pdf 导出(记录点滴,滴水成流),根据自己需要可以去官网查找相关API (官网API地址)

需要引入maven 依赖

		<dependency>
			<groupId>com.itextpdf</groupId>
			<artifactId>itextpdf</artifactId>
			<version>5.5.13</version>
		</dependency>
		<dependency>
			<groupId>com.itextpdf</groupId>
			<artifactId>itext-asian</artifactId>
			<version>5.2.0</version>
		</dependency>

示例代码

public static void fillTemplate(List<Map<String, Object>> mapJsonList,
			HttpServletResponse response, String templatePath, String fileName) {
		PdfReader reader;
		ByteArrayOutputStream bos;
		PdfStamper stamper;
		ServletOutputStream out = null;
		try {
			response.reset();
			// 设置resphone 头信息
			response.setContentType("application/PDF;charset=utf-8");
			response.setHeader("content-disposition", "attachment;filename="
					+ URLEncoder.encode(fileName, "UTF-8"));
			out = response.getOutputStream();
			Document doc = new Document();
			PdfCopy copy = new PdfCopy(doc, out);
			doc.open();
			for (Map<String, Object> mapJson : mapJsonList) {
				// 读取pdf模板
				reader = new PdfReader(templatePath);
				bos = new ByteArrayOutputStream();
				stamper = new PdfStamper(reader, bos);
				AcroFields form = stamper.getAcroFields();
				java.util.Iterator<String> it = form.getFields().keySet()
						.iterator();
				while (it.hasNext()) {
					// 获取模板里面的字段值key
					String name = it.next();
					// 往模板对应的属性赋值
					form.setField(name, MapUtils.getString(mapJson, name));
					/* 图片的处理 */
					String imgUrl = host
							+ String.valueOf(mapJson.get("picUrl"));
					int pageNo = form.getFieldPositions("img").get(0).page;
					Rectangle signRect = form.getFieldPositions("img").get(0).position;
					float x = signRect.getLeft();
					float y = signRect.getBottom();
					// 根据路径读取图片
					Image image = Image.getInstance(imgUrl);
					// 获取图片页面
					PdfContentByte under = stamper.getOverContent(pageNo);
					// 图片大小自适应
					image.scaleToFit(signRect.getWidth(), signRect.getHeight());
					// 添加图片
					image.setAbsolutePosition(x, y);
					under.addImage(image);
				}
				stamper.setFormFlattening(true);
				stamper.close();
				PdfImportedPage importPage = copy.getImportedPage(
						new PdfReader(bos.toByteArray()), 1);
				copy.addPage(importPage);

			}
			if (doc != null) {
				doc.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

实例效果图
在这里插入图片描述

ITEXT5 手写表格pdf 导出模板

代码示例

public static void userDataToPdf(HttpServletResponse response,
			String fileName, List<Map<String, Object>> param) {
		Document doc = null;
		try {
			doc = getInstance(response, fileName);
			doc.open();
			// 使用语言包字体
			BaseFont abf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
					BaseFont.NOT_EMBEDDED);
			// 字体
			Font font = new Font(abf, 8);
			// 段落
			Paragraph p = new Paragraph("放射人员信息统计表", new Font(abf, 12,
					Font.BOLD));
			p.setAlignment(Paragraph.ALIGN_CENTER);
			doc.add(p);
			// 表格
			PdfPTable table = new PdfPTable(7);// numcolumns:列数
			int widths[] = { 10, 10, 25, 10, 25, 20, 20 };
			table.setWidths(widths);
			table.setSpacingBefore(16f);// 表格与上面段落的空隙
			// 表格列创建并赋值
			PdfPCell cell = new PdfPCell();
			// 首行
			cell = new PdfPCell(new Phrase("姓名", font));
			cell.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(cell);
			cell = new PdfPCell(new Phrase("性别", font));
			cell.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(cell);
			cell = new PdfPCell(new Phrase("所属机构", font));
			cell.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(cell);
			cell = new PdfPCell(new Phrase("人员类别", font));
			cell.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(cell);
			cell = new PdfPCell(new Phrase("职业照射种类", font));
			cell.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(cell);
			cell = new PdfPCell(new Phrase("身份证号", font));
			cell.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(cell);
			cell = new PdfPCell(new Phrase("放射工作人员证号", font));
			cell.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(cell);
			if (param != null && param.size() > 0) {
				for (int i = 0; i < param.size(); i++) {
					cell = new PdfPCell(new Phrase(MapUtils.getString(
							param.get(i), "realName"), font));
					cell.setHorizontalAlignment(Element.ALIGN_CENTER);
					table.addCell(cell);
					cell = new PdfPCell(new Phrase(MapUtils.getString(
							param.get(i), "sex"), font));
					cell.setHorizontalAlignment(Element.ALIGN_CENTER);
					table.addCell(cell);
					cell = new PdfPCell(new Phrase(MapUtils.getString(
							param.get(i), "deptName"), font));
					cell.setHorizontalAlignment(Element.ALIGN_CENTER);
					table.addCell(cell);
					cell = new PdfPCell(new Phrase(MapUtils.getString(
							param.get(i), "userType"), font));
					cell.setHorizontalAlignment(Element.ALIGN_CENTER);
					table.addCell(cell);
					cell = new PdfPCell(new Phrase(MapUtils.getString(
							param.get(i), "nameOccupationalExposure"), font));
					cell.setHorizontalAlignment(Element.ALIGN_CENTER);
					table.addCell(cell);
					cell = new PdfPCell(new Phrase(MapUtils.getString(
							param.get(i), "IDCard"), font));
					cell.setHorizontalAlignment(Element.ALIGN_CENTER);
					table.addCell(cell);
					cell = new PdfPCell(new Phrase(MapUtils.getString(
							param.get(i), "code"), font));
					cell.setHorizontalAlignment(Element.ALIGN_CENTER);
					table.addCell(cell);
				}
			} else {
				// 表格列创建并赋值
				cell = new PdfPCell(new Phrase("暂无数据", font));
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);// 居中
				cell.disableBorderSide(13);// 去除左右上边框,保留下边框
				cell.setColspan(6);// 合并列数
				table.addCell(cell);
			}
			doc.add(table);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (doc != null) {
				doc.close();
			}

		}
	}

效果显示
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值