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

做报表pdf导出功能实现_插入图片

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(datas[i][j]), getPdfChineseFont());

 

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