PDFUtils.java 1.98 KB
Newer Older
王飞's avatar
王飞 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
package com.ruoyi.pdf;

import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import com.ruoyi.pdf.report.ReportTableRow;

import java.util.List;

public class PDFUtils {


    public static void drawTable(List<ReportTableRow> rows) {

    }

    private static void addTextToPdfCenter(AcroFields form, PdfStamper stamper, String text, String fieldName, BaseFont baseFont){
        // 通过模板表单单元格名获取所在页和坐标,左下角为起点
        int pageNo = form.getFieldPositions(fieldName).get(0).page;
        Rectangle signRect = form.getFieldPositions(fieldName).get(0).position;
        // 获取操作的页面
        PdfContentByte contentByte = stamper.getOverContent(pageNo);
        //创建表单
        PdfPTable table = new PdfPTable(1);
        //获取当前模板表单宽度
        float totalWidth = signRect.getRight() - signRect.getLeft() - 1;
        //设置新表单宽度
        table.setTotalWidth(totalWidth);
        //设置中文格式
        Font font = new Font(baseFont);
        // 设置字号
        font.setSize(10.56F);
        //设置单元格格式
        PdfPCell cell = new PdfPCell(new Phrase(text,font));
        //设置单元格高度
        cell.setFixedHeight(signRect.getTop()-signRect.getBottom()-1);
        cell.setBorderWidth(0);
        //设置垂直居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        //设置水平居中
//        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);

//      设置文字在单元格中换行时的行间距 :最终行间距 :totalLeading= fixedLeading+fontSize(字体大小)* multipliedLeading;
        cell.setLeading(0.0f,1.2f);

        //添加到表单中
        table.addCell(cell);
        //写入pdf
        table.writeSelectedRows(0, -1, signRect.getLeft(), signRect.getTop(), contentByte);
    }

}