java + jfreechart + itextpdf创建折线图饼图并导出为pdf

本文介绍了如何在Java项目中结合JFreeChart库绘制折线图和饼图,并利用iTextPDF库将这些图表导出为PDF文件。文章详细讲解了所需的Maven依赖、生成PDF的工具类、创建图表的工具类,以及相关的测试数据和DTO。通过参考链接提供了进一步的学习资源。

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

一、添加需要的maven依赖

<!--用于生成pdf-->
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.9</version>
</dependency>
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itext-asian</artifactId>
    <version>5.2.0</version>
</dependency>
<!--用于jfreechart生成图片  -->
<dependency>
    <groupId>org.jfree</groupId>
    <artifactId>jfreechart</artifactId>
    <version>1.5.0</version>
</dependency>

二、工具类生成pdf

import com.alibaba.fastjson.JSONArray;
import com.itextpdf.text.*;
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 com.manager.util.echarts.dto.VehicleIndex;
import com.manager.util.echarts.dto.VehicleIndex1;
import com.manager.util.echarts.dto.VehicleIndex2;
import com.manager.util.echarts.dto.VehicleIndex3;
import org.apache.commons.lang3.ObjectUtils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author yanjj
 * @ClassName CreateTable
 * @date 2022/7/12
 */
public class CreateEchartsPdfUtils {

    public static void createTable(ByteArrayOutputStream outputStream, List<VehicleIndex> tableContentList, List<VehicleIndex> tableContentList2, Map<String,Map<String, BigDecimal>> linePort, List<VehicleIndex> tableContentList3, Map<String, BigDecimal> piePort,List<VehicleIndex> tableContentList4) throws DocumentException, IOException {
        // 创建一个文档(默认大小A4,边距36, 36, 36, 36)
        Document document = new Document();
        // 设置文档大小
        document.setPageSize(PageSize.A4);
        // 设置边距,单位都是像素,换算大约1厘米=28.33像素
        document.setMargins(50, 50, 50, 50);
        // 创建writer,通过writer将文档写入磁盘
        PdfWriter writer = PdfWriter.getInstance(document, outputStream);
        //标题
        String title = "XXXX投资基金周度报告";
        //设置标题时间
        String createHeadTime = "报告截止日期:2022-07-08";
        // 打开文档,只有打开后才能往里面加东西
        document.open();
        // 设置作者
        document.addAuthor("XX");
        // 设置创建者
        document.addCreator("XX");
        // 设置主题
        document.addSubject(title);
        // 设置标题
        document.addTitle(title);
        // 设置标题
        Paragraph paragraphHead1 = createHead1(title);
        document.add(paragraphHead1);
        // 设置标题时间
        Paragraph paragraphHeadTime = createHeadTime(createHeadTime);
        document.add(paragraphHeadTime);
        //产品净值表格
        buildProductNetValue(document,tableContentList);
        //收益率指标
        buildYield(document,tableContentList2);
        //净值走势折线图
        JFreeChartUtils.createLinePortImage(document,"净值走势", linePort, "日期", "净值");
        //风险指标
        buildRiskIndicator(document,tableContentList3);
        //创建饼状图
        JFreeChartUtils.createPiePortImage(document,"组合配置", piePort);
        //产品信息
        buildProductDetail(document,tableContentList4);
        // 关闭文档,才能输出
        document.close();
        writer.close();
    }

    /**
     * 根据传入的参数生成PDF文档使用的字体
     *
     * @param fontSize  字体大小
     * @param fontMode  字体正常、加粗、下划线等
     * @param fontColor 字体颜色
     * @return 返回生成的字体
     */
    private static Font createFont(int fontSize, int fontMode, BaseColor fontColor) throws DocumentException, IOException {
        BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        return new Font(bfChinese, fontSize, fontMode, fontColor);
    }

    /**
     * 设置一级标题
     *
     * @param title 标题内容
     * @return 标题段落
     */
    private static Paragraph createHead1(String title) throws DocumentException, IOException {
        Font font = createFont(22, Font.BOLD, BaseColor.BLACK);
        Paragraph paragraph = new Paragraph(title, font);
        paragraph.setAlignment(Element.ALIGN_CENTER);
        return paragraph;
    }

    /**
     * 设置标题下的时间字段
     *
     * @param time 时间
     * @return 时间的段落
     */
    private static Paragraph createHeadTime(String time) throws DocumentException, IOException {
        Font font = createFont(14, Font.BOLD, BaseColor.BLACK);
        Paragraph paragraph = new Paragraph(time, font);
        paragraph.setAlignment(Element.ALIGN_CENTER);
        return paragraph;
    }

    /**
     * 设置一级标题
     *
     * @param title 标题内容
     * @return 标题段落
     */
    public static Paragraph createHead2(String title) throws DocumentException, IOException {
        Font font = createFont(12, Font.BOLD, BaseColor.BLACK);
        Paragraph paragraph = new Paragraph(title, font);
        paragraph.setAlignment(Element.ALIGN_CENTER);
        return paragraph;
    }

    /**
     * 根据传入的标题名称:批量设置标题值
     *
     * @param table        创建的表格对象
     * @param tableHeadStr 每个标题值按顺序来
     */
    private static void createTableHead(PdfPTable table, String[] tableHeadStr) throws DocumentException, IOException {
        if (ObjectUtils.isEmpty(tableHeadStr)) {
            return;
        }
        //获取设置表格标题的字体
        Font font = createFont(10, Font.NORMAL, BaseColor.BLACK);
        for (String tableHead : tableHeadStr) {
            PdfPCell cell = new PdfPCell(new Phrase(tableHead, font));
            cell.setBackgroundColor(new BaseColor(221, 221, 221));
            cell.setMinimumHeight(20);
            cell.setUseAscender(true);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(cell);
        }
    }

    /**
     * 创建表格内容
     *
     * @param table            表格对象
     * @param tableContentList 表格内容对象
     */
    private static void createTableContent(PdfPTable table, List<VehicleIndex> tableContentList) throws DocumentException, IOException {
        if (ObjectUtils.isEmpty(tableContentList)) {
            return;
        }
        //判断类型是VehicleIndex2还是VehicleIndex
        VehicleIndex index = tableContentList.get(0);
      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

《小书生》

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值