itextpdf使用Demo

import com.itextpdf.text.*;
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 java.io.File;
import java.io.FileOutputStream;

public class PdfTest {

    /**
     *
     * @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);
        }
        return cell;
    }

    public static void generatePDFDoc(){
        Document doc = new Document(PageSize.A4);//定义doc
        try {
            File file = new File("D:\\Downloads\\test.pdf");//生成目标文件
            file.createNewFile();
            PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(file));

            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 paragraph1 = new Paragraph("我爱北京天安门天安门上太阳升 This is a very long text that should be broken into multiple lines within the cell", font);//段落
            paragraph1.setAlignment(PdfPCell.ALIGN_CENTER);
            paragraph1.setLeading(14f); //行间距
            //paragraph1.setIndentationLeft(12); //设置左缩进
            //paragraph1.setIndentationRight(12); //设置右缩进
            //paragraph1.setFirstLineIndent(24); //设置首行缩进
            //paragraph1.setSpacingBefore(5f); //设置段落上空白
            //paragraph1.setSpacingAfter(10f); //设置段落下空白

            //itext中没有行和列的概念,只有单元格,因此在声明table时就要指定列数
            //然后按table.addCell(cell)顺序添加
            PdfPTable table = new PdfPTable(new float[] { 1f, 1.5f, 1f, 1.5f});
            //表头
            table.addCell(createCell("指定列数", font, PdfPCell.ALIGN_LEFT, PdfPCell.ALIGN_MIDDLE, 0,0,15));
            table.addCell(createCell("声明", font, PdfPCell.ALIGN_LEFT, PdfPCell.ALIGN_MIDDLE,3,0,15));
            table.addCell(createCell("单元格", font, PdfPCell.ALIGN_LEFT, PdfPCell.ALIGN_MIDDLE,0,0,15));
            table.addCell(createCell("列的概念", font, PdfPCell.ALIGN_LEFT, PdfPCell.ALIGN_MIDDLE,0,0,15));
            table.addCell(createCell("XXX5", font, PdfPCell.ALIGN_LEFT, PdfPCell.ALIGN_MIDDLE,0,0,15));
            table.addCell(createCell("XXX6", font, PdfPCell.ALIGN_LEFT, PdfPCell.ALIGN_MIDDLE,0,0,15));
            //动态表格
            for(int i = 0; i<6; i++){
                table.addCell(createCell("v1", font, Element.ALIGN_CENTER, PdfPCell.ALIGN_MIDDLE,0,0,0));
                table.addCell(createCell("v2", font, Element.ALIGN_CENTER, PdfPCell.ALIGN_MIDDLE,0,0,0));
                table.addCell(createCell("v3", font, Element.ALIGN_CENTER, PdfPCell.ALIGN_MIDDLE,0,0,0));
                table.addCell(createCell("v4", font, Element.ALIGN_CENTER, PdfPCell.ALIGN_MIDDLE,0,0,0));
//                table.addCell(createCell("v5", font, Element.ALIGN_CENTER, PdfPCell.ALIGN_MIDDLE,0,0,0));
//                table.addCell(createCell("v6", font, Element.ALIGN_CENTER, PdfPCell.ALIGN_MIDDLE,0,0,0));
//                table.addCell(createCell("v7", font, Element.ALIGN_CENTER, PdfPCell.ALIGN_MIDDLE,0,0,0));
//                table.addCell(createCell("v8", font, Element.ALIGN_CENTER, PdfPCell.ALIGN_MIDDLE,0,0,0));
            }

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

    public static void main(String[] args) {
        generatePDFDoc();
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值