itextpdf 使用demo

一.引入依赖

        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.13</version>
        </dependency>

        <!-- PDF文件字体 防止中文乱码 -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>

二.表格操作


    public static void main(String[] args) throws DocumentException, IOException {
        BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
        //使用字体并给出颜色
        Font font = new Font(baseFont, 20, Font.BOLD, BaseColor.BLACK);

        Font newfont = new Font(baseFont, 12, Font.NORMAL, BaseColor.BLACK);
        Document document = new Document(new Rectangle(842F, 595F));
        PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream("C:\\Users\\18151\\Desktop\\output21.pdf"));
        document.open();
        //设置标题
        Paragraph paragraph = new Paragraph("这是标题文档标题", font);
        paragraph.setAlignment(1);
        //引用字体
        document.add(paragraph);
        float[] widths = {25f, 25f};
        PdfPTable table = new PdfPTable(widths);
        table.setSpacingBefore(20f);
        // 设置表格宽度为100%
        table.setWidthPercentage(100.0F);
        String[] titleList = new String[]{"序号", "功能名称"};
        PdfPCell cell1 = new PdfPCell(new Paragraph("序号", font));
        cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell1.setFixedHeight(30);
        table.addCell(cell1);
        PdfPCell cell2 = new PdfPCell(new Paragraph("功能名称", font));
        cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell2.setFixedHeight(30);
        table.addCell(cell2);

        for (int i = 0; i < 2; i++) {
            PdfPCell cell = null
            cell = new PdfPCell(new Paragraph("内容1", newfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setFixedHeight(30);
            table.addCell(cell);
            cell = new PdfPCell(new Paragraph("内容2", newfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setFixedHeight(30);
            table.addCell(cell);
        }

        document.add(new Paragraph("\n"));
        document.add(new Paragraph("▋ " + "哈哈哈", font));
        document.add(table);
        document.add(new Paragraph("\n"));
        // 加水印
        PdfContentByte waterMar = pdfWriter.getDirectContent();
        String text = "12342234";
        addTextFullWaterMark(waterMar, text, baseFont);
        document.close();
    }


    /**
     * 给表格添加表头
     *
     * @param table
     * @param content
     * @param titleList
     */
    public static void addTableTitle(PdfPTable table, Font content, String[] titleList) {
        for (String title : titleList) {
            PdfPCell cell = new PdfPCell(new Paragraph(title, content));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setFixedHeight(30);
            table.addCell(cell);
        }
    }


    /**
     * 给pdf添加文字水印(平铺)
     *
     * @param waterMar
     * @param text     水印文本
     * @throws Exception
     */
    public static void addTextFullWaterMark(PdfContentByte waterMar, String text, BaseFont bf) {
        waterMar.beginText();
        PdfGState gs = new PdfGState();
        // 设置填充字体不透明度为0.2f
        gs.setFillOpacity(0.2f);
        waterMar.setFontAndSize(bf, 20);
        // 设置透明度
        waterMar.setGState(gs);
        // 设置水印对齐方式 水印内容 X坐标 Y坐标 旋转角度
        for (int x = 0; x <= 900; x += 200) {
            for (int y = -50; y <= 800; y += 200) {
                waterMar.showTextAligned(Element.ALIGN_RIGHT, text, x, y, 35);
            }
        }
        // 设置水印颜色
        waterMar.setColorFill(BaseColor.GRAY);
        //结束设置
        waterMar.endText();
        waterMar.stroke();
    }

三.新建页面

 document.newPage();

四.注意

 1.如果一个pdf页面内容过多 自动新增页,但是 加水印的时候只会加到最后一页,解决办法是排版好一页的内容加一次水印,newPage 再加一次水印

五:实现模板生成

1.思路1 使用 itexpdf html 转pdf html 使用模板引擎 写好样式直接生成 

2.思路2 直接做pdf模板这个 要使用一些工具(Adobe Acrod)等

    public static void main(String[] args) throws IOException, DocumentException {
        BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
        //使用字体并给出颜色
        Font font = new Font(baseFont, 20, Font.BOLD, BaseColor.BLACK);
        // 读取本地文件,当然线上环境肯定不这么写
        PdfReader reader = new PdfReader("C:\\Users\\18151\\Desktop\\未命名1_加水印.pdf");
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("C:\\Users\\18151\\Desktop\\my.pdf"));

        // 获取表单
        AcroFields form = stamper.getAcroFields();
        //form.setGenerateAppearances(true);
        // 表单填充
        form.setField("userName","购买方对应公司");
        form.setField("other","购买方对应公司");
        stamper.close();
        reader.close();
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值