2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > itextPDF生成表格的pdf

itextPDF生成表格的pdf

时间:2023-10-22 16:55:33

相关推荐

itextPDF生成表格的pdf

今天主要介绍如何使用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();}

这是最简单的步骤,表格的每一格内容,风格都一样,效果如下:

当然,在实际使用中,很有可能需求不会简单,比如要求设置背景颜色,边框颜色,每行宽度也可能不一致,甚至跨行,跨列,添加图片等等,下面就举一个综合的例子,展示这些设置的用法,请看示例:

/*** 表格各种属性综合使用* * @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);设置表格宽度比例为%100table.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();}

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

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

/*** 创建以图片为背景的表格* * @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);// 宽度300table.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);// 设置CellEventcell.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官网还有许多针对各种需求的例子,可以解决更多特殊的需求,地址:/itext-5-examples

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。