前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >怎么通过itextpdf架包实现报表导出为pdf文件?

怎么通过itextpdf架包实现报表导出为pdf文件?

作者头像
程序猿川子
发布2024-08-01 11:36:28
540
发布2024-08-01 11:36:28

1、添加依赖

代码语言:javascript
复制
XML 代码解读复制代码<dependency>
	<groupId>com.itextpdf</groupId>
	<artifactId>itextpdf</artifactId>
	<version>5.5.5</version>
</dependency>
<dependency>
	<groupId>com.itextpdf</groupId>
	<artifactId>itext-asian</artifactId>
	<version>5.2.0</version>
</dependency>

2、实现代码

代码语言:javascript
复制
java 代码解读复制代码package com.shucha.deveiface.biz.test;

import java.io.File;
import java.io.FileOutputStream;

import com.itextpdf.text.*;
import org.apache.commons.io.FileUtils;

import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
/**
 * @author tqf
 * @Description
 * @Version 1.0
 * @since 2022-05-25 10:59
 */
public class TestExportPdf {

    public static void main(String[] args) throws Exception {
        exportPdf();
    }

    public static void exportPdf() throws Exception {
        // 生成的pdf文件存放路径
        FileOutputStream fos = new FileOutputStream("D:/123/报表.pdf");

        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, fos);
        writer.setViewerPreferences(PdfWriter.PageModeUseThumbs);
        document.setPageSize(PageSize.A4);
        document.open();

        float[] widths = {144, 113, 191};
        PdfPTable table = new PdfPTable(widths);
        table.setLockedWidth(true);
        table.setTotalWidth(458);
        table.setHorizontalAlignment(Element.ALIGN_LEFT);

        Object[][] datas = {{"区域产品销售额"},{"区域", "总销售额(万元)", "总利润(万元)简单的表格"}, {"江苏省" , 9045,  2256}, {"广东省", 3000, 690}};
        for(int i = 0; i < datas.length; i++) {
            for(int j = 0; j < datas[i].length; j++) {
                PdfPCell pdfCell = new PdfPCell();
                pdfCell.setMinimumHeight(30);

                //设置单元格样式
                pdfCell.setHorizontalAlignment(Element.ALIGN_CENTER);
                pdfCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                pdfCell.setBackgroundColor(new BaseColor(0xdd7e6b));

                pdfCell.setBorder(0);
                pdfCell.setBorderWidthTop(0.1f);
                pdfCell.setBorderWidthBottom(0.1f);
                pdfCell.setBorderWidthLeft(0.1f);
                pdfCell.setBorderWidthRight(0.1f);
                pdfCell.setBorderColorBottom(new BaseColor(0x674ea7));
                pdfCell.setBorderColorLeft(new BaseColor(0x674ea7));
                pdfCell.setBorderColorRight(new BaseColor(0x674ea7));
                pdfCell.setBorderColorTop(new BaseColor(0x674ea7));

                //设置单元格文本字体样式
                Font font = getPdfChineseFont();
                if(i == datas.length - 1 && j == 3 - 1) {
                    font.setColor(new BaseColor(0xff0000));
                    font.setSize(16);
                    font.setStyle("bold");
                    font.setStyle("italic");
                    font.setStyle("underline");
                }

                //合并单元格
                if(i == 0 && j == 0) {
                    pdfCell.setRowspan(1);
                    pdfCell.setColspan(3);
                }

                Paragraph paragraph = new Paragraph(datas[i][j].toString(), font);
                pdfCell.setPhrase(paragraph);

                table.addCell(pdfCell);
            }
        }

        //单元格插入图片
        byte[] bt = FileUtils.readFileToByteArray(new File("D:/123/1.png"));
        PdfPCell pdfCell = new PdfPCell();
        pdfCell.setImage(Image.getInstance(bt));
        table.addCell(pdfCell);

        //插入子表格
        pdfCell = new PdfPCell();
        pdfCell.setRowspan(1);
        pdfCell.setColspan(2);

        PdfPTable suTtable = new PdfPTable(new float[]{100, 100});
        PdfPCell subPdfCell = new PdfPCell();
        subPdfCell.setPhrase(new Paragraph("sub1", getPdfChineseFont()));
        suTtable.addCell(subPdfCell);
        subPdfCell = new PdfPCell();
        subPdfCell.setPhrase(new Paragraph("sub2", getPdfChineseFont()));
        suTtable.addCell(subPdfCell);

        pdfCell.addElement(suTtable);
        table.addCell(pdfCell);

        document.add(table);

        //文档插入绝对位置图片
        Image image = Image.getInstance(bt);
        int x = 30;
        int y = 230;
        image.setAbsolutePosition(x + document.leftMargin(),  PageSize.A4.getHeight() - y -
                image.getHeight() - document.topMargin());
        // document.add(image);

        document.close();
    }

    /**
     * 设置字体
     * @return
     * @throws Exception
     */
    public static Font getPdfChineseFont() throws Exception {
        BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",
                BaseFont.NOT_EMBEDDED);
        Font fontChinese = new Font(bfChinese, 12, Font.NORMAL);
        return fontChinese;
    }
}

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、添加依赖
  • 2、实现代码
相关产品与服务
腾讯云 BI
腾讯云 BI(Business Intelligence,BI)提供从数据源接入、数据建模到数据可视化分析全流程的BI能力,帮助经营者快速获取决策数据依据。系统采用敏捷自助式设计,使用者仅需通过简单拖拽即可完成原本复杂的报表开发过程,并支持报表的分享、推送等企业协作场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档