Java导出pdf并且在pdf里面插入图片

本文介绍了如何使用Java和iTextPDF库将数据导出到PDF,并在PDF中插入图片。代码展示了如何配置依赖项、创建PDF文档、设置页面样式和插入表格数据,以及插入图片的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java导出pdf并且在pdf里面插入图片

先贴效果图

在这里插入图片描述

添加依赖

        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.13.3</version>
        </dependency>
              <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>
        <dependency>
            <groupId>com.lowagie</groupId>
            <artifactId>itext</artifactId>
            <version>2.1.7</version>
        </dependency>

业务代码

1.export.exportPdf(“/Users///ToP00.pdf”);这个是输出地址改为自己的路径

    public static void main(String[] args) {
        ExportPDfController export = new ExportPDfController();
        export.exportPdf("/Users/**/**/ToP00.pdf");
    }

1.Image.getInstance(“/Users/**/s/gz/2781705997354_.pic.jpg”);这个是图片地址,改为自己想要插入的图片

    public void exportPdf(String optDir) {
        try {
            //文档对象  实现A4纸页面

            Document document = new Document();
            //设置文档的页边距就是距离页面边上的距离,分别为:左边距,右边距,上边距,下边距
            document.setMargins(10, 10, 20, 20);

            File pdfFile = new File("后厨菜品记录.pdf");
            FileOutputStream fileOutputStream = new FileOutputStream(pdfFile);
            PdfWriter.getInstance(document, fileOutputStream);
            //打开文档
            document.open();
            //使用字体,正文字体
            Font font = new Font(BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 8, Font.BOLD);
            font.setColor(19, 19, 19);
            Font font_small = new Font(BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 5, Font.BOLD);

            Font font_white = new Font(BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 8, Font.BOLD);
            font_white.setColor(255, 255, 255);

            Font font_red = new Font(BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 8, Font.BOLD);
            font_red.setColor(228, 9, 9);

            Font font_green = new Font(BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 8, Font.BOLD);
            font_green.setColor(34, 116, 5);

            Font font_blue = new Font(BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 8, Font.BOLD);
            font_blue.setColor(12, 78, 244);

            document.setPageSize(PageSize.A4);
            //创建新的一页
            document.newPage();
            Paragraph title = new Paragraph("后厨菜品记录", new Font(BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 21, Font.BOLD));
            title.setAlignment(Element.ALIGN_CENTER);
            title.setPaddingTop(100f);
            title.setSpacingAfter(20f);
            document.add(title);
            //提示
            Paragraph tips = new Paragraph("2023年后厨后厨需要保持清洁卫生,遵守食品安全和卫生标准,确保食品的质量和安全", new Font(BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 10, Font.BOLD));
            //
            tips.setAlignment(Element.ALIGN_LEFT);
            tips.setPaddingTop(100f);
            tips.setSpacingAfter(20f);
            document.add(tips);


            PdfPTable table = new PdfPTable(9);
            table.setWidthPercentage(100); // Width 100%

            float[] columnWidths = {0.8f, 2f, 1f, 1f, 1f, 1f, 1.2f, 1.2f, 2.3f};


            table = new PdfPTable(9);
            table.setWidths(columnWidths);
            table.setWidthPercentage(100);
            table.addCell(getPdfPCell_lan("行次", font_white));
            table.addCell(getPdfPCell_lan("菜品", font_white));
            table.addCell(getPdfPCell_lan("价格", font_white));
            table.addCell(getPdfPCell_lan("产地", font_white));
            table.addCell(getPdfPCell_lan("日期", font_white));

            table.addCell(getPdfPCell_lan("部门", font_white));
            table.addCell(getPdfPCell_lan("同比", font_white));
            table.addCell(getPdfPCell_lan("环比", font_white));
            table.addCell(getPdfPCell_lan("统计日期", font_white));

            document.add(table);

            table.setWidthPercentage(100);
            table.addCell(getPdfPCell_qian_lan("1", font));
            table.addCell(getPdfPCell_qian_lan("黄瓜", font));
            table.addCell(getPdfPCell_qian_lan("55.79", font));
            table.addCell(getPdfPCell_qian_lan("北京", font));
            table.addCell(getPdfPCell_qian_lan("2023-11-11", font));
            table.addCell(getPdfPCell_qian_lan("后厨部", font));
            table.addCell(getPdfPCell_qian_lan("12.25%", font));
            table.addCell(getPdfPCell_hong("1.6%", font_red));


            Image icon = Image.getInstance("/Users/**/s/gz/2781705997354_.pic.jpg");
            icon.scaleToFit(10f, 10f);
            Font font1 = new Font(BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 8, Font.BOLD, BaseColor.RED);
            Chunk textChunk = new Chunk("55.79%", font1);

            Chunk imageChunk = new Chunk(icon, 0, 0, true); // 图片作为一个Chunk

            Phrase phrase = new Phrase();
            phrase.add(textChunk);
            phrase.add(new Chunk(" ")); // 添加一个空格作为分隔符,可以根据需要调整间距
            phrase.add(new Chunk(" ")); // 添加一个空格作为分隔符,可以根据需要调整间距
            phrase.add(new Chunk(" ")); // 添加一个空格作为分隔符,可以根据需要调整间距
            phrase.add(new Chunk(" ")); // 添加一个空格作为分隔符,可以根据需要调整间距

            phrase.add(imageChunk);

            PdfPCell cell = new PdfPCell(phrase);
            cell.setBorder(Rectangle.NO_BORDER);
            cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); // 设置对齐方式
            cell.setBackgroundColor(new BaseColor(139, 192, 246, 50));
            cell.setPaddingTop(12f); // 设置对齐方式
            table.addCell(cell);


            document.add(table);


            document.close();
            //pdfWriter.close();

            FileInputStream fis = new FileInputStream(pdfFile);

            // 创建FileOutputStream对象
            FileOutputStream fos = new FileOutputStream(optDir);

            // 读取数据并写入文件
            int data;
            while ((data = fis.read()) != -1) {
                fos.write(data);
            }

            // 关闭流
            fis.close();
            fos.close();


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

工具类

 public PdfPCell getPdfPCell(String value, Font font) {
        return getPdfPCell(value, font, 0, 0, null, 0);
    }

    public PdfPCell getPdfPCell_lan(String value, Font font) {
        //0 蓝 1 浅蓝 2 深蓝
        return getPdfPCell_color(value, font, 0, 0, null, 0);
    }

    /**
     * 浅蓝色背景单元格
     */
    public PdfPCell getPdfPCell_qian_lan(String value, Font font) {
        return getPdfPCell_color(value, font, 0, 0, null, 1);
    }

    /**
     * 深蓝色背景单元格
     */
    public PdfPCell getPdfPCell_shen_lan(String value, Font font) {
        return getPdfPCell_color(value, font, 0, 0, null, 2);
    }

    /**
     * 红色背景单元格
     */
    public PdfPCell getPdfPCell_hong(String value, Font font) {
        return getPdfPCell_color(value, font, 0, 0, null, 3);
    }

    /**
     * 绿色背景单元格
     */
    public PdfPCell getPdfPCell_lv(String value, Font font) {
        return getPdfPCell_color(value, font, 0, 0, null, 4);
    }

    public PdfPCell getPdfPCell1(String value, Font font, Integer flow) {
        return getPdfPCell(value, font, 0, 0, null, flow);
    }

    public PdfPCell getPdfPCell(String value, Font font, int colspan, int rowspan) {
        return getPdfPCell(value, font, colspan, rowspan, null, 0);
    }

    public PdfPCell getPdfPCell(String value, Font font, int colspan, int rowspan, Image image, Integer flow) {
        PdfPCell cell = new PdfPCell(new Phrase(value, font));
        cell.setUseBorderPadding(true);
        //        cell.setBorderColor(BaseColor.LIGHT_GRAY);
        cell.setBorderColor(BaseColor.WHITE);
        //        cell.setBorder(0);
        cell.setFixedHeight(40);
        if (flow == 0) {
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        } else if (flow == 1) {
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        } else {
            cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        }

        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setColspan(colspan);
        cell.setRowspan(rowspan);

        return cell;
    }

    public PdfPCell getPdfPCell_color(String value, Font font, int colspan, int rowspan, Image image, Integer color) {
        PdfPCell cell = new PdfPCell(new Phrase(value, font));
        cell.setUseBorderPadding(true);
        cell.setBorderColor(BaseColor.WHITE);
        //        cell.setBorder(0);
        //        cell.setBorderColor(BaseColor.WHITE);
        cell.setFixedHeight(40);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        //0 蓝 1 浅蓝 2 深蓝 3 红 4 绿
        if (color == 0) {
            cell.setBackgroundColor(new BaseColor(12, 78, 244, 255));
        } else if (color == 1) {
            cell.setBackgroundColor(new BaseColor(139, 192, 246, 50));
        } else if (color == 2) {
            cell.setBackgroundColor(new BaseColor(187, 217, 247, 50));
        } else if (color == 3) {
            cell.setBackgroundColor(new BaseColor(247, 187, 217, 50));
        } else if (color == 4) {
            cell.setBackgroundColor(new BaseColor(187, 247, 231, 50));
        }
        //        if (image != null) { // 检查图片是否为空
        //            image.scaleToFit(10f, 10f);
        //            Paragraph textContent = new Paragraph(periodLastYearPremium);
        //            cell.addElement(textContent); // 添加文本元素
        //            cell.addElement(image); // 添加图片元素
        //            table.addCell(cell);
        //        }


        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setColspan(colspan);
        cell.setRowspan(rowspan);

        return cell;
    }


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值