项目练习:使用itextpdf制作pdf报表

0、情况说明

本来是用jasperreport软件来制作pdf模板的。
但是,它有点局限性。

它的循环部分是detail。
我的需求是,在最后一页,紧贴着detail表格增加一块文本框。

但是
我用新增一个detail+判断的话,会导致总页数发生变化,无法确定最后一页。
用column footer+条件判断的话,会导致,最后一页有文本框,前面的页面最低下都留了一个空白区。
用last page footer,可以实现最后一页出现文本框,但是,这个文本框和detail部分有空白区。

上面三种办法都无法完美实现效果。

所以,想着用Java编码方式,自定义报表部分。

1、pom

pdf_demo项目

            <!-- pdf:start -->
            <dependency>
                <groupId>com.itextpdf</groupId>
                <artifactId>itextpdf</artifactId>
                <version>5.5.11</version>
            </dependency>
            <!-- 支持中文 -->
            <dependency>
                <groupId>com.itextpdf</groupId>
                <artifactId>itext-asian</artifactId>
                <version>5.2.0</version>
            </dependency>

2、demo

package com.pzj.pdf_demo;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import net.sf.jasperreports.engine.JRException;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.UUID;

public class Test01 {
	public static void main(String[] args) throws FileNotFoundException, JRException {
		generatePDFDoc();
	}

	/**
	 *		固定行高	20
	 * @param value 文本
	 * @param font 字体
	 * @param horizontalAlignment 水平样式 0-left, 1-center, 2-right
	 * @param verticalAlignment 垂直样式  4-top, 5-middle, 6-bottom;
	 * @param colspan 列合并
	 * @param rowspan 行合并
	 * @param borderSide 外边框
	 *  0-默认
	 *  1-隐藏上边框
	 *  2-隐藏下边框
	 *  3-隐藏上、下边框
	 *  4-隐藏左边框
	 *  5-隐藏左、上边框
	 *  6-隐藏左、下边框
	 *  7-隐藏左、上、下边框
	 *  8-隐藏右边框
	 *  9-隐藏右、上边框
	 *  10-隐藏右、下边框
	 *  11-隐藏右、上、下边框
	 *  12-隐藏左、右边框
	 *  13-隐藏上、左、右边框
	 *  14-隐藏下、左、右边框
	 *  15-隐藏全部
	 * @return
	 */
	public static PdfPCell createCell(String value, Font font, int horizontalAlignment, int verticalAlignment,
									  int colspan, int rowspan, int borderSide) {
		PdfPCell cell = new PdfPCell();
		cell.setPhrase(new Phrase(value, font));//存值
		cell.setHorizontalAlignment(horizontalAlignment);//水平居中
		if(verticalAlignment>0){
			cell.setUseAscender(true);//垂直居中
		}
		cell.setVerticalAlignment(verticalAlignment);//垂直居中
		if(colspan>0 ){
			cell.setColspan(colspan);
		}
		if(rowspan>0){
			cell.setRowspan(rowspan);
		}
		if(borderSide>0){
			cell.disableBorderSide(borderSide);
		}
		cell.setMinimumHeight(20);
		return cell;
	}

	public static PdfPCell createCell(String value, Font font, int horizontalAlignment, int verticalAlignment,
									  int colspan, int rowspan, int borderSide,int minHeight) {
		PdfPCell cell = new PdfPCell();
		cell.setPhrase(new Phrase(value, font));//存值
		cell.setHorizontalAlignment(horizontalAlignment);//水平居中
		if(verticalAlignment>0){
			cell.setUseAscender(true);//垂直居中
		}
		cell.setVerticalAlignment(verticalAlignment);//垂直居中
		if(colspan>0 ){
			cell.setColspan(colspan);
		}
		if(rowspan>0){
			cell.setRowspan(rowspan);
		}
		if(borderSide>0){
			cell.disableBorderSide(borderSide);
		}
		if (minHeight > 0) {
			cell.setMinimumHeight(minHeight);
		}
		return cell;
	}

	public static void generatePDFDoc(){
		Document doc = new Document(PageSize.A4);//定义doc
		try {
			File file = new File("C:\\Users\\admin\\Desktop\\code\\test.pdf");//生成目标文件
			file.createNewFile();
			PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(file));
//			writer.setPageEvent(new Watermark("LeaSoft"));// 水印

			doc.open();//open
			BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
			//2. BaseFont.createFont("C:/WINDOWS/Fonts/simsun.ttc", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
			//3. BaseFont.createFont("/simsun.ttc", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
			Font font = new Font(bf, 10, Font.NORMAL, BaseColor.BLACK);//字体
			//使用中文字体有三种方式
			//1.使用itext自带的字体集包
			//2.使用本地的 字体资源
			//  2.1 windows下,字体资源在目录 C:/WINDOWS/Fonts,注意 ttc后缀要 加 ',1'
			//  2.2 linux 未尝试,不过同理,找到字体资源的绝对路径即可
			//3.可以将 字体资源 放到项目 当中
			Paragraph paragraph = new Paragraph("中文测试", font);//段落
			paragraph.setAlignment(PdfPCell.ALIGN_CENTER);
			paragraph.setLeading(14f); //行间距
			//paragraph.setIndentationLeft(12); //设置左缩进
			//paragraph.setIndentationRight(12); //设置右缩进
			//paragraph.setFirstLineIndent(24); //设置首行缩进
			//paragraph.setSpacingBefore(5f); //设置段落上空白
			//paragraph.setSpacingAfter(10f); //设置段落下空白

			//itext中没有行和列的概念,只有单元格,因此在声明table时就要指定列数
			//然后按table.addCell(cell)顺序添加		指定每列宽度
			PdfPTable table = new PdfPTable(new float[] { 2f, 2f, 2f, 2f});
			table.setSpacingBefore(10);
			table.setSpacingAfter(10);
			//表头
			table.addCell(createCell("XXX", font, PdfPCell.ALIGN_LEFT, PdfPCell.ALIGN_MIDDLE, 0,0,15));
			table.addCell(createCell("XXX", font, PdfPCell.ALIGN_LEFT, PdfPCell.ALIGN_MIDDLE,3,0,15));
			table.addCell(createCell("XXX", font, PdfPCell.ALIGN_LEFT, PdfPCell.ALIGN_MIDDLE,0,0,15));
			table.addCell(createCell("XXX", font, PdfPCell.ALIGN_LEFT, PdfPCell.ALIGN_MIDDLE,0,0,15));
			table.addCell(createCell("XXX", font, PdfPCell.ALIGN_LEFT, PdfPCell.ALIGN_MIDDLE,0,0,15));
			table.addCell(createCell("XXX", font, PdfPCell.ALIGN_LEFT, PdfPCell.ALIGN_MIDDLE,0,0,15));
			//动态表格
			for(int i = 0; i<50; i++){
				//一行4个单元格
				table.addCell(createCell("v", font, Element.ALIGN_CENTER, PdfPCell.ALIGN_MIDDLE,0,0,0));
				table.addCell(createCell("v", font, Element.ALIGN_CENTER, PdfPCell.ALIGN_MIDDLE,0,0,0));
				table.addCell(createCell("v", font, Element.ALIGN_CENTER, PdfPCell.ALIGN_MIDDLE,0,0,0));
				table.addCell(createCell("v", font, Element.ALIGN_CENTER, PdfPCell.ALIGN_MIDDLE,0,0,0));
			}
			table.addCell(createCell("表格备注内容", font, Element.ALIGN_CENTER, PdfPCell.ALIGN_MIDDLE,
					0,0,0,150));
			table.addCell(createCell("", font, Element.ALIGN_CENTER, PdfPCell.ALIGN_MIDDLE,
					3,0,0,150));

			doc.add(paragraph);
			doc.add(table);
		}catch (Exception e){
			e.printStackTrace();
		}finally {
			// 5.关闭文档
			if(doc!=null){
				doc.close();
			}
		}
	}

}

3、效果

在这里插入图片描述
参考文档:Java利用itext实现生成导出PDF文件

4、总结

jasperreport软件制作报表固然方便,快捷
但是,它貌似只适合两种情况的PDF报表
1、单页报表
2、结构简单的多页报表

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值