最近用iText生成pdf文件供下载和当做附件email, 第一次使用,跨行和实现背景图片卡了n久,g了n久,都是提问的,没见给出的解答的,还得靠自己,倒腾了n久,总算解决了,贴出来!
iText的介绍参考http://www.china1024.com/bytesoft/info_show.jsp?news_id=968或者iText的官网,如果想做进一步的了解,到http://www.51leifeng.net/上下本<<iText in Action>>,英文的,耐心点就行了。下面直入主题。
1. iText的跨行
iText的API中Cell可以很容易实现跨行和跨列,但是没法设置列宽和列高(好像是,找了n久没有找到)。只能打PdfPCell的主意了(PdfPCell也是iText推荐的),但是PdfPCell中只有设置跨列的方法,没有提供跨行的方法。用table嵌套,在外层的table中嵌套table,应该可以实现跨列,html中有这样做过。表格是画出来,但是嵌套的表格之间的间隙没发去掉。其实table的嵌套思路是对的,但是应该把table放到cell(PdfPCell的对象)中,再将这个cell放到最外层的table中。总的思路:最外层的table中只addCell,嵌套的table放到被加入到外层table之中的cell中。有点拗口,看代码吧:
- package cn.chenkun.iText;
- import java.io.FileOutputStream;
- import com.lowagie.text.Document;
- import com.lowagie.text.PageSize;
- import com.lowagie.text.Paragraph;
- import com.lowagie.text.pdf.PdfPCell;
- import com.lowagie.text.pdf.PdfPTable;
- import com.lowagie.text.pdf.PdfWriter;
- public class Colspan {
- public static void main(String[] args) {
- colspan();
- }
- private static void colspan(){
- Document document = new Document(PageSize.A4, 36, 36, 36, 36);
- try {
- PdfWriter.getInstance(document, new FileOutputStream("d:\\coslpan.pdf"));
- document.open();
- PdfPTable table = new PdfPTable(4); // 最外层table
- float[] wid = {80f, 100f, 80f, 60f};
- table.setTotalWidth(wid);
- table.setLockedWidth(true);
- PdfPCell cell = null; // 最外层table的cell
- PdfPTable iTable = null; // 嵌套的table
- PdfPCell iCell = null; // 嵌套的table的cell
- iTable = new PdfPTable(3);
- float[] iWid = {80f, 100f, 80f};
- iTable.setTotalWidth(iWid);
- iTable.setLockedWidth(true);
- iCell = new PdfPCell(new Paragraph("column 1"));
- iCell.setFixedHeight(30);
- iTable.addCell(iCell);
- iCell.setColspan(2);
- iTable.addCell(iCell);
- iCell = new PdfPCell(new Paragraph("column 2"));
- iCell.setFixedHeight(30);
- iTable.addCell(iCell);
- iTable.addCell(iCell);
- iTable.addCell(iCell);
- iCell = new PdfPCell(new Paragraph("column 3"));
- iCell.setFixedHeight(30);
- iTable.addCell(iCell);
- iTable.addCell(iCell);
- iTable.addCell(iCell);
- cell = new PdfPCell(iTable); // 用这个table初始外层table的cell
- cell.setColspan(3); // 设置它跨3列
- cell.setFixedHeight(3*30); // 设置它的高度
- table.addCell(cell); // 将这个cell加入table中
- iTable = new PdfPTable(1);
- float[] iWid2 = {60f};
- iTable.setTotalWidth(iWid2);
- iTable.setLockedWidth(true);
- iCell = new PdfPCell(new Paragraph("i am here"));
- iTable.addCell(iCell);
- cell = new PdfPCell(iTable);
- cell.setFixedHeight(3*30); // 跨3列了
- table.addCell(cell);
- document.add(table);
- } catch (Exception de) {
- de.printStackTrace();
- }
- document.close();
- }
- }
2. iText的背景图片
iText中的PdfPCell都是有自己默认的布局的,要实现自己的布局,必须实现PdfPCellEvent接口,在方法cellLayout中定义自己的布局。更多信息见<<iText in Action>>中10.2 Working with iText’s direct content。Figure 10.6实际上已经实现了背景图片,这里将代码改写如下:
- package cn.chenkun.iText;
- import java.io.FileOutputStream;
- import com.lowagie.text.Document;
- import com.lowagie.text.Element;
- import com.lowagie.text.Image;
- import com.lowagie.text.PageSize;
- import com.lowagie.text.Rectangle;
- import com.lowagie.text.pdf.PdfContentByte;
- import com.lowagie.text.pdf.PdfPCell;
- import com.lowagie.text.pdf.PdfPCellEvent;
- import com.lowagie.text.pdf.PdfPTable;
- import com.lowagie.text.pdf.PdfWriter;
- public class PicBackGround {
- public static void main(String[] args) {
- picBg();
- }
- private static void picBg() {
- Document document = new Document(PageSize.A4, 36, 36, 36, 36);
- try {
- PdfWriter.getInstance(document, new FileOutputStream("d:\\testPicBG.pdf"));
- document.open();
- BGPic border = new BGPic();
- float wid = 80f;
- float hei = 100f;
- float[] widArr = { wid, wid };
- PdfPTable table = new PdfPTable(2);
- table.setTotalWidth(widArr);
- table.setLockedWidth(true);
- PdfPCell cell = null;
- for (int i = 1; i <= 4; i++) {
- Image img = Image.getInstance("d:/ma.jpg");
- cell = new PdfPCell(img, true);
- cell.setFixedHeight(hei);
- cell.setHorizontalAlignment(Element.ALIGN_CENTER);
- cell.setPadding(8);
- cell.setCellEvent(border); // 加入背景图片
- table.addCell(cell);
- }
- document.add(table);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- document.close();
- }
- }
- }
- class BGPic implements PdfPCellEvent {
- public void cellLayout(PdfPCell cell, Rectangle rect, PdfContentByte[] canvas) {
- PdfContentByte cb = canvas[PdfPTable.BACKGROUNDCANVAS];
- Image img = null;
- try {
- img = Image.getInstance("d:/hai.jpg");
- img.scaleAbsolute(80, 100); // 设置背景图片的大小
- img.setAbsolutePosition(298, 606); // 设置第一个背景图片的绝对位置
- cb.addImage(img);
- img.setAbsolutePosition(217, 706); // 设置第二个背景图片的绝对位置
- cb.addImage(img);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
//---------------------------
百度里有搜到一种背景图设置法
Image jpgBack = Image.getInstance(xxxxx);
jpgBack.setAlignment(Image.UNDERLYING);
不过这种方法对表格不起作用
表格里加上图片后,文字怎么都写不到图片上去.
请有过经验的同志们,告知如何给表格加背景图