Java通过itextpdf架包实现报表导出为pdf文件

目录

1、添加依赖

2、实现代码

3、生成的效果


1、添加依赖

<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、实现代码

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

3、生成的效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

码奴生来只知道前进~

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

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

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

打赏作者

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

抵扣说明:

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

余额充值