itextpdf生成表格的常见用法

itextpdf生成表格的常见用法随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)、博主微信(guyun297890152)、QQ技术交流群(183198395)。在前面的文章介绍了itextpdf基本用法和使用itextpdf生成图片,itextpdf还可以实现很多功能,非常强大,今天主要介绍如何使用itextpdf生成表格式的pdf,在实际项目中也非常常用,首先举一个非常…

大家好,又见面了,我是你们的朋友全栈君。

随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)

itextpdf生成表格的常见用法

在前面的文章介绍了itextpdf基本用法使用itextpdf生成图片,itextpdf还可以实现很多功能,非常强大,今天主要介绍如何使用itextpdf生成表格式的pdf,在实际项目中也非常常用,首先举一个非常简单的例子,熟悉一下生成表格的基本步骤和流程:

	public static void createSimpleTable() throws IOException, DocumentException {
		Document document = new Document();
		PdfWriter.getInstance(document, new FileOutputStream(DEST));
		document.open();
		PdfPTable table = new PdfPTable(5);
		for (int aw = 0; aw < 10; aw++) {
			// 构建每一格
			table.addCell("cell");
		}
		document.add(table);
		document.close();
	}

这是最简单的步骤,表格的每一格内容,风格都一样,效果如下:
itextpdf生成表格的常见用法当然,在实际使用中,很有可能需求不会简单,比如要求设置背景颜色,边框颜色,每行宽度也可能不一致,甚至跨行,跨列,添加图片等等,下面就举一个综合的例子,展示这些设置的用法,请看示例:

	/**
	 * 表格各种属性综合使用
	 * 
	 * @throws IOException
	 * @throws DocumentException
	 */
	public static void createTablePdf() throws IOException, DocumentException {
		Document document = new Document();
		// 创建PdfWriter对象
		PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(DEST));
		// 打开文档
		document.open();

		// 添加表格,4列
		PdfPTable table = new PdfPTable(4);
		 设置表格宽度比例为%100
		table.setWidthPercentage(100);
		// 设置表格的宽度
		table.setTotalWidth(500);
		// 也可以每列分别设置宽度
		table.setTotalWidth(new float[] { 160, 70, 130, 100 });
		// 锁住宽度
		table.setLockedWidth(true);
		// 设置表格上面空白宽度
		table.setSpacingBefore(10f);
		// 设置表格下面空白宽度
		table.setSpacingAfter(10f);
		// 设置表格默认为无边框
		table.getDefaultCell().setBorder(0);
		PdfContentByte cb = writer.getDirectContent();

		// 构建每个单元格
		PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1"));
		// 边框颜色
		cell1.setBorderColor(BaseColor.BLUE);
		// 设置背景颜色
		cell1.setBackgroundColor(BaseColor.ORANGE);
		// 设置跨两行
		cell1.setRowspan(2);
		// 设置距左边的距离
		cell1.setPaddingLeft(10);
		// 设置高度
		cell1.setFixedHeight(20);
		// 设置内容水平居中显示
		cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
		// 设置垂直居中
		cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
		table.addCell(cell1);

		PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2"));
		cell2.setBorderColor(BaseColor.GREEN);
		cell2.setPaddingLeft(10);
		cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
		table.addCell(cell2);

		PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3"));
		cell3.setBorderColor(BaseColor.RED);
		cell3.setPaddingLeft(10);
		// 设置无边框
		cell3.setBorder(Rectangle.NO_BORDER);
		cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
		table.addCell(cell3);

		// 在表格添加图片
		Image cellimg = Image.getInstance(IMG1);
		PdfPCell cell4 = new PdfPCell(cellimg, true);
		cell4.setBorderColor(BaseColor.RED);
		cell4.setPaddingLeft(10);
		cell4.setFixedHeight(30);
		cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell4.setVerticalAlignment(Element.ALIGN_MIDDLE);
		table.addCell(cell4);

		// 增加一个条形码到表格
		Barcode128 code128 = new Barcode128();
		code128.setCode("14785236987541");
		code128.setCodeType(Barcode128.CODE128);
		// 生成条形码图片
		Image code128Image = code128.createImageWithBarcode(cb, null, null);
		// 加入到表格
		PdfPCell cellcode = new PdfPCell(code128Image, true);
		cellcode.setHorizontalAlignment(Element.ALIGN_CENTER);
		cellcode.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cellcode.setFixedHeight(30);
		table.addCell(cellcode);

		PdfPCell cell5 = new PdfPCell(new Paragraph("Cell 5"));
		cell5.setPaddingLeft(10);
		// 设置占用列数
		cell5.setColspan(2);
		cell5.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell5.setVerticalAlignment(Element.ALIGN_MIDDLE);
		table.addCell(cell5);
		document.add(table);
		// 关闭文档
		document.close();
	}

上面需要注意的地方都有注释,相信通过这些设置基本应该能满足需求了,效果如下:

itextpdf生成表格的常见用法

 下面再给一个将一张图片作为表格背景的例子,请看代码:

	/**
	 * 创建以图片为背景的表格
	 * 
	 * @throws IOException
	 * @throws DocumentException
	 */
	public static void createImgBackgroundTable() throws IOException, DocumentException {
		Document document = new Document();
		PdfWriter.getInstance(document, new FileOutputStream(DEST));
		document.open();
		// 一列
		PdfPTable table = new PdfPTable(1);
		// 宽度300
		table.setTotalWidth(300);
		table.setLockedWidth(true);
		PdfPCell cell = new PdfPCell();
		Font font = new Font(FontFamily.HELVETICA, 12, Font.NORMAL, GrayColor.GRAYWHITE);
		Paragraph p = new Paragraph("A cell with an image as background color.", font);
		cell.addElement(p);
		// 构造图片
		Image image = Image.getInstance(BACKGROUD_IMG);
		// 设置CellEvent
		cell.setCellEvent(new ImageBackgroundEvent(image));
		// 按比例设置cell高度
		cell.setFixedHeight(200 * image.getScaledHeight() / image.getScaledWidth());
		table.addCell(cell);
		document.add(table);
		document.close();
	}
	static class ImageBackgroundEvent implements PdfPCellEvent {
		protected Image image;

		public ImageBackgroundEvent(Image image) {
			this.image = image;
		}

		public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
			try {
				PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS];
				image.scaleAbsolute(position.getWidth(), position.getHeight());
				image.setAbsolutePosition(position.getLeft(), position.getBottom());
				cb.addImage(image);
			} catch (DocumentException e) {
				throw new ExceptionConverter(e);
			}
		}
	}

效果图如下:

itextpdf生成表格的常见用法

在itextpdf官网还有许多针对各种需求的例子,可以解决更多特殊的需求,地址:The Leading PDF Library for Developers | iText

 今天就介绍到这里,后续还会推出一些比较实用的干货,请大家持续关注csdn官网博客和源代码社区公众号。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/147656.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • sublime3激活码-激活码分享

    (sublime3激活码)2021最新分享一个能用的的激活码出来,希望能帮到需要激活的朋友。目前这个是能用的,但是用的人多了之后也会失效,会不定时更新的,大家持续关注此网站~https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~ML…

    2022年3月21日
    43
  • B2C电商系统源码 在线商城源码[通俗易懂]

    B2C电商系统源码 在线商城源码[通俗易懂]B2C产品采用SSH+Jquery框架开发。具备构建大型电商平台的底层技术体系。支持Oracl与Mysql等多个主流数据库。一、商品系统支持实物与虚拟商品体系。无限制级商品分类与扩展。智能商品模板、个性定义商品属性与属性继承与关联。商品与资讯的智能关联、商品关键字维护,SEO效果更为高效、精准。二、订单系统完善订单流管理、精准跟踪状态与执行人分派。支持来自电商、电话以及渠道的多订单体系。支持订单全流程服务(订单打印、发运、到货、退货、换货、拒收)等。三、会员系统围绕会员精细服

    2026年1月28日
    4
  • linux 新建文件的命令

    图形界面下就不用说了,终端下键入:touchtest.java就创建了一个新文件test.javahttp://hovertree.com/menu/linux/http://www.cnbl

    2021年12月24日
    46
  • java中executeQuery()方法

    java中executeQuery()方法介绍使用 JDBC 连接数据库需要 4 步 executeQuery 方法是第四步执行查询 要用 statement 类的 executeQuery 方法来下达 select 指令以查询数据库 executeQuery 方法会把数据库响应的查询结果存放在 ResultSet 类对象中供我们使用 举例如下 Stringstr9 selectsnofro

    2025年7月11日
    7
  • Idea激活码永久有效Idea2018.2.7激活码教程-持续更新,一步到位「建议收藏」

    Idea激活码永久有效Idea2018.2.7激活码教程-持续更新,一步到位「建议收藏」Idea激活码永久有效2018.2.7激活码教程-Windows版永久激活-持续更新,Idea激活码2018.2.7成功激活

    2022年6月17日
    68
  • 区块链技术「建议收藏」

    区块链技术「建议收藏」https://www.zhihu.com/question/37290469作者:汪乐-LaiW3n链接:https://www.zhihu.com/question/37290469/answer/107612456来源:知乎著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。更新:将私信问答放在了最后–

    2022年5月17日
    32

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号