`
anson_xu
  • 浏览: 502216 次
  • 性别: Icon_minigender_1
  • 来自: 惠州
社区版块
存档分类

iText 跨行and背景图片

    博客分类:
  • java
阅读更多

最近用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中。有点拗口,看代码吧:

  1. package cn.chenkun.iText;
  2. import java.io.FileOutputStream;
  3. import com.lowagie.text.Document;
  4. import com.lowagie.text.PageSize;
  5. import com.lowagie.text.Paragraph;
  6. import com.lowagie.text.pdf.PdfPCell;
  7. import com.lowagie.text.pdf.PdfPTable;
  8. import com.lowagie.text.pdf.PdfWriter;
  9. public class Colspan {
  10.     public static void main(String[] args) {
  11.         colspan();
  12.     }
  13.     
  14.     private static void colspan(){
  15.         Document document = new Document(PageSize.A4, 36363636);
  16.         try {
  17.             PdfWriter.getInstance(document, new FileOutputStream("d:\\coslpan.pdf"));
  18.             document.open();  
  19.             
  20.             PdfPTable table = new PdfPTable(4);  // 最外层table
  21.             float[] wid = {80f, 100f, 80f, 60f};
  22.             table.setTotalWidth(wid);
  23.             table.setLockedWidth(true);
  24.             
  25.             PdfPCell cell = null;  // 最外层table的cell
  26.             
  27.             PdfPTable iTable = null// 嵌套的table
  28.             PdfPCell iCell = null;  // 嵌套的table的cell
  29.             
  30.             iTable = new PdfPTable(3);
  31.             float[] iWid = {80f, 100f, 80f};
  32.             iTable.setTotalWidth(iWid);
  33.             iTable.setLockedWidth(true);
  34.             iCell = new PdfPCell(new Paragraph("column 1"));
  35.             iCell.setFixedHeight(30);
  36.             iTable.addCell(iCell);
  37.             iCell.setColspan(2);
  38.             iTable.addCell(iCell);
  39.             iCell = new PdfPCell(new Paragraph("column 2"));
  40.             iCell.setFixedHeight(30);
  41.             iTable.addCell(iCell);
  42.             iTable.addCell(iCell);
  43.             iTable.addCell(iCell);
  44.             
  45.             iCell = new PdfPCell(new Paragraph("column 3"));
  46.             iCell.setFixedHeight(30);
  47.             iTable.addCell(iCell);
  48.             iTable.addCell(iCell);
  49.             iTable.addCell(iCell);
  50.             
  51.             cell = new PdfPCell(iTable);  // 用这个table初始外层table的cell
  52.             cell.setColspan(3);  // 设置它跨3列
  53.             cell.setFixedHeight(3*30);  // 设置它的高度
  54.             table.addCell(cell);  // 将这个cell加入table中
  55.             
  56.             iTable = new PdfPTable(1);  
  57.             float[] iWid2 = {60f};
  58.             iTable.setTotalWidth(iWid2);
  59.             iTable.setLockedWidth(true);
  60.             iCell = new PdfPCell(new Paragraph("i am here"));
  61.             iTable.addCell(iCell);
  62.             
  63.             cell = new PdfPCell(iTable);
  64.             cell.setFixedHeight(3*30);  // 跨3列了
  65.             table.addCell(cell);
  66.             
  67.             document.add(table);
  68.         } catch (Exception de) {
  69.             de.printStackTrace();
  70.         }
  71.         document.close();
  72.     }
  73. }

 

2. iText的背景图片

        iText中的PdfPCell都是有自己默认的布局的,要实现自己的布局,必须实现PdfPCellEvent接口,在方法cellLayout中定义自己的布局。更多信息见<<iText in Action>>中10.2 Working with iText’s direct content。Figure 10.6实际上已经实现了背景图片,这里将代码改写如下:

  1. package cn.chenkun.iText;
  2. import java.io.FileOutputStream;
  3. import com.lowagie.text.Document;
  4. import com.lowagie.text.Element;
  5. import com.lowagie.text.Image;
  6. import com.lowagie.text.PageSize;
  7. import com.lowagie.text.Rectangle;
  8. import com.lowagie.text.pdf.PdfContentByte;
  9. import com.lowagie.text.pdf.PdfPCell;
  10. import com.lowagie.text.pdf.PdfPCellEvent;
  11. import com.lowagie.text.pdf.PdfPTable;
  12. import com.lowagie.text.pdf.PdfWriter;
  13. public class PicBackGround {
  14.     public static void main(String[] args) {
  15.         picBg();
  16.     }
  17.     private static void picBg() {
  18.         Document document = new Document(PageSize.A4, 36363636);
  19.         try {
  20.             PdfWriter.getInstance(document, new FileOutputStream("d:\\testPicBG.pdf"));
  21.             document.open();
  22.             BGPic border = new BGPic();
  23.             float wid = 80f;
  24.             float hei = 100f;
  25.             float[] widArr = { wid, wid };
  26.             PdfPTable table = new PdfPTable(2);
  27.             table.setTotalWidth(widArr);
  28.             table.setLockedWidth(true);
  29.             PdfPCell cell = null;
  30.             for (int i = 1; i <= 4; i++) {
  31.                 Image img = Image.getInstance("d:/ma.jpg");
  32.                 cell = new PdfPCell(img, true);
  33.                 cell.setFixedHeight(hei);
  34.                 cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  35.                 cell.setPadding(8);
  36.                 cell.setCellEvent(border);  // 加入背景图片
  37.                 table.addCell(cell);
  38.             }
  39.             document.add(table);
  40.         } catch (Exception e) {
  41.             e.printStackTrace();
  42.         } finally {
  43.             document.close();
  44.         }
  45.     }
  46. }
  47. class BGPic implements PdfPCellEvent {
  48.     public void cellLayout(PdfPCell cell, Rectangle rect, PdfContentByte[] canvas) {
  49.         PdfContentByte cb = canvas[PdfPTable.BACKGROUNDCANVAS];
  50.         Image img = null;
  51.         try {
  52.             img = Image.getInstance("d:/hai.jpg");
  53.             img.scaleAbsolute(80100);  // 设置背景图片的大小
  54.             img.setAbsolutePosition(298606);  // 设置第一个背景图片的绝对位置
  55.             cb.addImage(img);
  56.             img.setAbsolutePosition(217706);  // 设置第二个背景图片的绝对位置
  57.             cb.addImage(img);
  58.         } catch (Exception e) {
  59.             e.printStackTrace();
  60.         }
  61.     }
  62. }

 

 

 

//---------------------------

百度里有搜到一种背景图设置法
Image jpgBack = Image.getInstance(xxxxx);
jpgBack.setAlignment(Image.UNDERLYING); 

不过这种方法对表格不起作用
表格里加上图片后,文字怎么都写不到图片上去.
请有过经验的同志们,告知如何给表格加背景图

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics