做报表pdf导出功能实现

简介: 做报表pdf导出功能实现

Java使用itext组件导出pdf报表,导出pdf文件一般是系统中常支持的功能,输出pdf之后可以打印,下面介绍导出pdf一般使用的功能,介绍怎么 在pdf插入浮动层图片(类似HTML中div),有(X、Y)坐标确定图片的位置。

iText使用的版本是:itextpdf-5.5.1.jar, 可以去iText官网下载

输出pdf表格
iText输出pdf最基本是输出table表格,下面是输出区域、总销售额(万元)、总利润(万元)简单的表格,创建Document文档对象,其可以加入表格table,pdf文档大小设置为A4,通过PdfWriter对象将pdf文件写入到输出流中。getPdfChineseFont()方法解决中文乱码,需要加入itext-asian.jar

float[] widths = {144, 113, 191};

FileOutputStream fos = new FileOutputStream("/Users/mike/table1.pdf");

Document document = new Document();

PdfWriter writer = PdfWriter.getInstance(document, fos);

writer.setViewerPreferences(PdfWriter.PageModeUseThumbs);

document.setPageSize(PageSize.A4);//设置A4

document.open();

PdfPTable table = new PdfPTable(widths);

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(); //表格的单元格

    Paragraph paragraph = new Paragraph(StringUtils.trimToNull(datas[i][j]), getPdfChineseFont());

    pdfCell.setPhrase(paragraph);

    table.addCell(pdfCell);

}

}

document.add(table); //pdf文档中加入table

document.close();

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;

}

设置表格行高、列宽
有时单元格文本比较多,需要设置表格的列宽度,在初始化PdfTable可以指定每列的宽度,使用单元格对象PdfCell设置表格行高度,

table的setLockedWidth(true)方法设置表格为固定宽度,这时必须设置setTotalWidth(458)表格宽度

FileOutputStream fos = new FileOutputStream("/Users/mike/table2.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);//设置表格行高

    Paragraph paragraph = new Paragraph(StringUtils.trimToNull(datas[i][j]), getPdfChineseFont());

    pdfCell.setPhrase(paragraph);

    table.addCell(pdfCell);

}

}

document.add(table);

document.close();

设置pdf单元格样式
单元格可以设置居左、居中、居右、上下居中、设置边框、设置边框颜色、设置单元格背景颜色等, 使用PdfCell对象设置,颜色比比较简单直接时16进制rgb值。

pdfCell.setHorizontalAlignment(Element.ALIGN_CENTER);

pdfCell.setVerticalAlignment(Element.ALIGN_MIDDLE);

pdfCell.setBackgroundColor(new BaseColor(0xdd7e6b));

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.setStyle()方法,

这个font是com.itextpdf.text.Font不是Java的Font, setStyle方法内部会做或操作。

Font font = getPdfChineseFont();

font.setColor(new BaseColor(0xff0000));

font.setSize(16);

font.setStyle("bold");

font.setStyle("italic");

font.setStyle("underline");

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;

}

合并单元格

PdfCell做单元格合并比较简单,直接调用setRowspan与setColspan方法,

如下第一行3列合并, 被合并的单元格不需要加入table中(table.addCell(pdfCell)),否则会出现表格错乱的情况。

pdfCell.setRowspan(1);

pdfCell.setColspan(3);

单元格中加入图片

单元格中不仅是文本、数值、也可以加入图片,直接调用pdfCell.setImage()方法。

//单元格插入图片

byte[] bt = FileUtils.readFileToByteArray(new File("/Users/mike/pie.png"));

PdfPCell pdfCell = new PdfPCell();

pdfCell.setImage(Image.getInstance(bt));//插入图片

table.addCell(pdfCell);

table.addCell(new PdfPCell());

table.addCell(new PdfPCell());

ClientAnchor anchor = helper.createClientAnchor();

anchor.setCol1(0); //图片开始列数

anchor.setRow1(4); //图片开始行数

anchor.setCol2(3); //图片结束列数

anchor.setRow2(25);//图片结束行数

drawing.createPicture(anchor, pictureIdx);

pdf中插入浮动层图片类似html中div
pdf中插入图片, iText导出pdf可以根据绝对位置X、Y坐标值插入图片,然后将image加入到pdf文档document中,上面的例子是document中加入表格table, pdf文档开始坐标位置在左下角位置,所以使用document的高度(PageSize.A4.getHeight())减去y值和图片自身高度.

//文档插入绝对位置图片

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

pdf单元格中插入子表格

pdf表格中可以加入子表格,通过pdfCell.addElement(suTtable)方法加入(sub1、sub2)子表格

//插入子表格

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

单元格中画斜线
iText pdf单元格中画斜线另一篇有详细介绍:iText报表表格画斜线

完整例子

iText导出pdf例子

import java.io.File;

import java.io.FileOutputStream;

import org.apache.commons.io.FileUtils;

import com.itextpdf.text.BaseColor;

import com.itextpdf.text.Document;

import com.itextpdf.text.Element;

import com.itextpdf.text.Font;

import com.itextpdf.text.Image;

import com.itextpdf.text.PageSize;

import com.itextpdf.text.Paragraph;

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 org.apache.commons.lang.StringUtils;

public class TestExportPdf {

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

    

    FileOutputStream fos = new FileOutputStream("/Users/mike/table8.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(StringUtils.trimToNull(datas[i][j]), font);

            pdfCell.setPhrase(paragraph);

                             

            table.addCell(pdfCell);

        }

    }

     

    //单元格插入图片

    byte[] bt = FileUtils.readFileToByteArray(new File("/Users/mike/pie.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();

}

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;

}

}
补充一下这里有个StringUtil.trimToNull工具类jar包,commons-lang和commons-lang3都可以使用

Paragraph paragraph = new Paragraph(StringUtils.trimToNull(datasi), getPdfChineseFont());

        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.3.2</version>


报表工具:https://www.veryreport.com

相关文章
|
29天前
|
数据安全/隐私保护 Windows
深度剖析:PDF 工具箱功能,编辑器操作及页面 / 图像提取技巧
PDF24 Tools是一款德国开发的免费PDF工具箱,18年始终免费,支持网页与Windows客户端。内置近50个工具,涵盖编辑、转换、合并、提取、加密等功能,操作简单,可离线使用,是高效处理PDF的理想选择。
358 0
|
11天前
|
机器学习/深度学习 文字识别 Shell
高效率办公PDF批量处理:批量OCR识别PDF区域文字内容,用PDF内容批量改名或导出表格的货物运单应用案例
针对铁路货运物流单存档需求,本项目基于WPF与飞桨OCR技术,实现批量图片多区域文字识别与自动重命名。用户可自定义识别区域,系统提取关键信息(如车号、批次号)并生成规范文件名,提升档案管理效率与检索准确性,支持PDF及图像文件处理。
113 0
|
6月前
|
存储 JSON API
如何将 Swagger 文档导出为 PDF 文件
你会发现自己可能需要将 Swagger 文档导出为 PDF 或文件,以便于共享和存档。在这篇博文中,我们将指导你完成将 Swagger 文档导出为 PDF 格式的过程。
|
3月前
|
缓存 测试技术 网络安全
05百融云策略引擎项目交付-laravel实战完整交付定义常量分文件配置-独立建立lib类处理-成功导出pdf-优雅草卓伊凡
05百融云策略引擎项目交付-laravel实战完整交付定义常量分文件配置-独立建立lib类处理-成功导出pdf-优雅草卓伊凡
71 0
05百融云策略引擎项目交付-laravel实战完整交付定义常量分文件配置-独立建立lib类处理-成功导出pdf-优雅草卓伊凡
|
2月前
|
人工智能 JavaScript 前端开发
基于iTextSharp实现PDF加密功能
本教程介绍使用C#与iTextSharp库实现PDF文档加密的方法。内容包括环境搭建、界面设计及后台逻辑编写,涵盖选择文件、设置用户与所有者密码、加密操作等步骤,帮助开发者快速掌握PDF安全处理技巧。
62 0
|
4月前
|
人工智能 开发工具 开发者
【HarmonyOS 5】鸿蒙应用实现发票扫描、文档扫描输出PDF图片或者表格的功能
HarmonyOS 系统提供的核心场景化视觉服务,旨在帮助开发者快速实现移动端文档数字化功能。
178 0
|
8月前
|
文字识别
【PDF提取全自动改名】如何批量提取PDF指定区域的文字内容,用内容批量给PDF命名或者导出表格,学会全自动解放双手
在生活和工作中,我们常需处理大量PDF文件,如银行单据、税收单据等。手动处理效率低下,而使用“咕嘎批量PDF多区域内容提取重命名导表格系统”可快速完成数千份文档的处理,大幅提高效率。该工具通过获取PDF各区域内容坐标,导入并处理文件,最终将信息提取至表格,并根据关键信息对PDF进行重命名,方便管理和查找。
1109 2
|
8月前
|
人工智能 JSON 搜索推荐
猫步简历 - 开源免费AI简历生成器 | 一键导出PDF/JSON
猫步简历是一款免费开源的AI简历生成器,帮助用户轻松创建独特、专业的简历。支持导出超高清PDF、图片、JSON等多种格式,并提供AI智能创作、润色和多语种切换等功能。拥有海量模板、高度定制化模块及完善的后台管理系统,助力求职者脱颖而出。官网:https://maobucv.com,GitHub开源地址:https://github.com/Hacker233/resume-design。
1764 10
|
10月前
|
XML C# 数据格式
一个.NET开源、免费、功能强大的 PDF 处理工具
一个.NET开源、免费、功能强大的 PDF 处理工具
274 8
|
Java BI API
spring boot 整合 itextpdf 导出 PDF,写入大文本,写入HTML代码,分析当下导出PDF的几个工具
这篇文章介绍了如何在Spring Boot项目中整合iTextPDF库来导出PDF文件,包括写入大文本和HTML代码,并分析了几种常用的Java PDF导出工具。
2704 0
spring boot 整合 itextpdf 导出 PDF,写入大文本,写入HTML代码,分析当下导出PDF的几个工具

热门文章

最新文章