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

一、添加需要的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);
      
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你可以使用iTextPDF库来生成PDF文档,并通过添加图表来生成折线图。以下是一个示例代码,演示了如何使用iTextPDF生成折线图: ```java import com.itextpdf.io.IOException; import com.itextpdf.kernel.colors.ColorConstants; import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.layout.Document; import com.itextpdf.layout.element.*; import com.itextpdf.layout.property.HorizontalAlignment; import com.itextpdf.layout.property.TextAlignment; import org.jfree.chart.ChartFactory; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.category.DefaultCategoryDataset; import java.awt.*; import java.io.File; public class LineChartExample { public static void main(String[] args) { // 创建数据集 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.addValue(1, "Series 1", "Category 1"); dataset.addValue(2, "Series 1", "Category 2"); dataset.addValue(3, "Series 1", "Category 3"); dataset.addValue(4, "Series 1", "Category 4"); // 创建折线图 JFreeChart chart = ChartFactory.createLineChart( "Line Chart Example", "Category", "Value", dataset, PlotOrientation.VERTICAL, true, true, false ); // 创建PDF文档 String outputFilePath = "line_chart.pdf"; try { PdfWriter writer = new PdfWriter(outputFilePath); PdfDocument pdfDoc = new PdfDocument(writer); Document doc = new Document(pdfDoc); // 添加标题 Paragraph title = new Paragraph("Line Chart Example") .setFontSize(20) .setBold() .setTextAlignment(TextAlignment.CENTER) .setMarginTop(50); doc.add(title); // 将折线图转换为图片 File chartImageFile = new File("line_chart.png"); ChartUtils.saveChartAsPNG(chartImageFile, chart, 500, 300); // 添加折线图图片到PDF文档 Image chartImage = new Image(ImageDataFactory.create(chartImageFile.getAbsolutePath())); doc.add(chartImage.setHorizontalAlignment(HorizontalAlignment.CENTER)); // 关闭文档 doc.close(); System.out.println("PDF生成成功!"); } catch (IOException e) { e.printStackTrace(); } catch (java.io.IOException e) { e.printStackTrace(); } } } ``` 这个示例代码使用了iTextPDFJFreeChart库来生成PDF文档和折线图。首先创建了一个数据集,然后使用数据集创建折线图。接下来,通过iTextPDF创建PDF文档,并将折线图转换为图片,最后将图片添加到PDF文档中。 请确保在运行代码之前已经添加了iTextPDFJFreeChart的依赖库。你可以从官方网站下载并导入这些库。 希望这个示例能帮到你!如果有任何问题,请随时向我提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

《小书生》

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

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

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

打赏作者

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

抵扣说明:

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

余额充值