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();
    }

相关推荐

  1. itextpdf 使用demo

    2024-07-19 05:16:02       26 阅读
  2. itextpdf使用使用PdfReader添加图片水印

    2024-07-19 05:16:02       51 阅读
  3. itexpdf使用网页链接

    2024-07-19 05:16:02       48 阅读
  4. 使用freemarker和itextpdf结合,将html转化为pdf

    2024-07-19 05:16:02       50 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-19 05:16:02       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-19 05:16:02       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-19 05:16:02       58 阅读
  4. Python语言-面向对象

    2024-07-19 05:16:02       69 阅读

热门阅读

  1. chatglm2-6b-prompt尝试

    2024-07-19 05:16:02       20 阅读
  2. IKM 外企常用

    2024-07-19 05:16:02       20 阅读
  3. Linux源码阅读笔记13-进程通信组件上

    2024-07-19 05:16:02       20 阅读
  4. 分布式唯一id的7种方案

    2024-07-19 05:16:02       24 阅读
  5. 编程中的智慧之设计模式三

    2024-07-19 05:16:02       21 阅读
  6. 解决用PicGo为typora配置github图床失败的问题

    2024-07-19 05:16:02       19 阅读
  7. shape_trans 变换区域的形状

    2024-07-19 05:16:02       22 阅读
  8. 【21】读感 - 架构整洁之道(三)

    2024-07-19 05:16:02       15 阅读
  9. Bootstrap 5:现代前端开发的新篇章

    2024-07-19 05:16:02       17 阅读
  10. python 乌龟绘图

    2024-07-19 05:16:02       19 阅读
  11. qt 国际化语言,英文和中文切换

    2024-07-19 05:16:02       17 阅读
  12. 翁恺-C语言程序设计-10-4. 字符串循环左移

    2024-07-19 05:16:02       17 阅读