itext 制作 pdf 工具类

根据自己项目中用到得进行封装工具类。

public class PDFUtil {

	public static final Logger LOGGER = Logger.getLogger(PDFUtil.class);

	// 标准字体
	public static Font NORMALFONT;
	// 加粗字体
	public static Font BOLDFONT;
	//固定高
	public static float fixedHeight = 27f;
	//间距
	public static int spacing = 5;

	static {
		try {
			BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
			NORMALFONT = new Font(bfChinese, 10, Font.NORMAL);
			BOLDFONT = new Font(bfChinese, 14, Font.BOLD);
		} catch (Exception e) {
			LOGGER.error("设置字体错误!错误信息:{}", e);
			e.printStackTrace();
		}

	}
	
	/**
	 * 新建标准段落
	 * @param text 段落内容
	 * @return
	 */
	public static Paragraph createNormalParagraph(String text){
        Paragraph elements = new Paragraph(text, NORMALFONT);
        elements.setSpacingAfter(spacing);
        return elements;
    }

	/**
	 * 新建加粗段落
	 * @param text	段落内容
	 * @return
	 */
    public static Paragraph createBoldParagraph(String text) {
        Paragraph elements = new Paragraph(text, BOLDFONT);
        elements.setSpacingAfter(spacing);
        elements.setAlignment(Paragraph.ALIGN_CENTER);
        return elements;
    }

    /**
     * 隐藏表格边框线
     * @param cell	单元格
     */
    public static void disableBorderSide(PdfPCell cell){
        if (cell != null) {
            cell.disableBorderSide(1);
            cell.disableBorderSide(2);
            cell.disableBorderSide(4);
            cell.disableBorderSide(8);
        }
    }

    /**
     * 创建固定高度得单元格
     * @return
     */
    public static PdfPCell createPdfPCell() {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setFixedHeight(fixedHeight);
        return cell;
    }

    /**
     * 创建居中得单元格
     * @return
     */
    public static PdfPCell createCenterPdfPCell() {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setFixedHeight(fixedHeight);
        return cell;
    }
    
    /**
     * 创建指定背景颜色得单元格
     * @param color
     * @return
     */
    public static PdfPCell createCenterPdfPCell(BaseColor color) {
    	PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setFixedHeight(fixedHeight);
        cell.setBackgroundColor(color);
        return cell;
    }
    
    /**
     * 创建指定文字得单元格
     * @param text
     * @return
     */
    public static PdfPCell createCenterPdfPCell(String text) {
    	PdfPCell cell = new PdfPCell(new Paragraph(text, NORMALFONT));
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setFixedHeight(fixedHeight);
        return cell;
    }
    
    /**
     * 创建指定背景颜色和文字得单元格
     * @param text
     * @return
     */
    public static PdfPCell createCenterPdfPCell(String text, BaseColor color) {
    	PdfPCell cell = new PdfPCell(new Paragraph(text, NORMALFONT));
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setFixedHeight(fixedHeight);
        cell.setBackgroundColor(color);
        return cell;
    }
    
    /**
     * 创建指定宽度得表格
     * @param widths	列宽数组
     * @return
     */
    public static PdfPTable createPdfPTable(float[] widths) {
        PdfPTable pdfPTable = new PdfPTable(widths);
        pdfPTable.setHorizontalAlignment(Element.ALIGN_CENTER);
        return pdfPTable;
    }
    
    /**
     * 创建居左表格
     * @param len	表格列数
     * @return
     */
    public static PdfPTable createLeftPdfPTable(int len) {
    	PdfPTable pdfPTable = new PdfPTable(len);
    	pdfPTable.setHorizontalAlignment(Element.ALIGN_LEFT);
    	return pdfPTable;
    }
    
    /**
     * 创建居左表格
     * @param len	表格列数
     * @return
     */
    public static PdfPTable createLeftPdfPTable(float[] columnWidths) {
    	PdfPTable pdfPTable = new PdfPTable(columnWidths);
    	pdfPTable.setHorizontalAlignment(Element.ALIGN_LEFT);
    	return pdfPTable;
    }
    
    /**
     * 往指定pdf文件中加入虚线分割线
     * @param document
     * @throws DocumentException
     */
    public static void createPdfPTableSplitLine(Document document) throws DocumentException {
    	//CustomCell 为自制表格
    	CustomCell border = new CustomCell();
        PdfPTable table = new PdfPTable(1);
        table.setSpacingAfter(5f);
        table.setWidthPercentage(100);
        PdfPCell cell = new PdfPCell(new Phrase("表格作为分割线"));
        cell.setFixedHeight(10f);
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setCellEvent(border);
        table.addCell(cell);
        document.add(table);
    }

}

自制表格用于创建虚线分割线

public class CustomCell implements PdfPCellEvent{

	@Override
	public void cellLayout(PdfPCell arg0, Rectangle position, PdfContentByte[] canvases) {
		PdfContentByte cb = canvases[PdfPTable.LINECANVAS];
        cb.saveState();
        cb.setLineWidth(0.5f);
        cb.setColorStroke(new BaseColor(136, 136, 136));
        cb.setLineDash(new float[] { 6.0f, 2.0f }, 0);
        cb.moveTo(position.getLeft(), position.getBottom());
        cb.lineTo(position.getRight(), position.getBottom());
        cb.stroke();
        cb.restoreState();
	}

}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值