`
小杨学JAVA
  • 浏览: 885349 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

iText学习 ---表格

 
阅读更多

iText学习 ---表格

iText学习第二天---表格

 

一个最基本的PdfPTable的例子


package com.itext.test;


import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;


import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;


/**
 * 一个非常简单的PdfPTable的例子.
 */
public class MyFirstTable {


 /**
  A very simple PdfPTable example.
  

  @param args
  
            no arguments needed
  */
 public static void main(String[] args) {


  System.out.println("My First PdfPTable");


  // 步骤 1: 创建一个document对象
  Document document = new Document();


  try {
   // 步骤 2:
   // 我们为document创建一个监听,并把PDF流写到文件中
   PdfWriter.getInstance(document, new FileOutputStream("c:\\MyFirstTable.pdf"));


   // 步骤 3:打开文档
   document.open();
   //创建一个有3列的表格
   PdfPTable table = new PdfPTable(3);
   //定义一个表格单元
   PdfPCell cell = new PdfPCell(new Paragraph("header with colspan 3"));
   //定义一个表格单元的跨度
   cell.setColspan(3);
   //把单元加到表格中
   table.addCell(cell);
   //把下面这9项顺次的加入到表格中,当一行充满时候自动折行到下一行
   table.addCell("1.1");
   table.addCell("2.1");
   table.addCell("3.1");
   table.addCell("1.2");
   table.addCell("2.2");
   table.addCell("3.2");
   table.addCell("1.3");
   table.addCell("2.3");
   table.addCell("3.3");
   //重新定义单元格
   cell = new PdfPCell(new Paragraph("cell test1"));
   //定义单元格的框颜色
   cell.setBorderColor(new Color(255, 0, 0));
   //把单元格加到表格上,默认为一个单元
   table.addCell(cell);
   //重新定义单元格
   cell = new PdfPCell(new Paragraph("cell test2"));
   //定义单元格的跨度
   cell.setColspan(2);
   //定义单元格的背景颜色
   cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
   //增加到表格上
   table.addCell(cell);
   //增加到文档中
   document.add(table);
  } catch (DocumentException de) {
   System.err.println(de.getMessage());
  } catch (IOException ioe) {
   System.err.println(ioe.getMessage());
  }


  // 步骤 5:关闭文档
  document.close();
 }
}


看完这个例子,又看过我第一天记录的朋友一定会问为什么不用Table,我在这里解释一下。


PdfPTable is a very powerful and flexible object, but for some specific needs, you can also use one of the alternatives for PdfPTable. If you have a Swing application with JTables, you can look at the JTable2Pdf section. PdfPTable only works for generating PDF. If you need to generate HTML or RTF, you need the (no longer supported) Table object.


上面这句话来之---iText的 tutorial,你应该明白了吧。


 


If you add a PdfPTable with Document.add(), the default width of the table is 80 percent of the available space and the table is aligned in the center. You can change these defaults with setWidthPercentage and setHorizontalAlignment.


 


下面就讲一个可以自己定义表格宽度和对齐方式的例子:


package com.itext.test;


import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;


import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;


/**
 * 改变复杂表格的宽度和对齐方式.
 */
public class TableWidthAlignment {


 /**
  Changing the width and alignment of the complete table.
  

  param args
  
            no arguments needed
  
throws IOException
  
            no arguments needed
  
throws IOException
  
@throws DocumentException
  */
 public static void main(String[] args) throws DocumentException,
   IOException {
  // 定义中文字体
  BaseFont bfChinese = BaseFont.createFont("STSong-Light",
    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
  com.lowagie.text.Font fontCN = new com.lowagie.text.Font(bfChinese, 12,
    com.lowagie.text.Font.NORMAL);
  System.out.println("table width and alignment");
  // 步骤1:创建一个大小为A4的文档
  Document document = new Document(PageSize.A4);
  try {
   // 步骤 2:
   // 我们为document创建一个监听,并把PDF流写到文件中
   PdfWriter.getInstance(document, new FileOutputStream(
     "c:\\TableWidthAlignment.pdf"));
   // 步骤 3:打开文档
   document.open();
   // 创建一个有3列的表格
   PdfPTable table = new PdfPTable(3);
   // 定义一个表格单元
   PdfPCell cell = new PdfPCell(new Paragraph("header with colspan 3"));
   // 定义一个表格单元的跨度
   cell.setColspan(3);
   // 把单元加到表格中
   table.addCell(cell);
   // 把下面这9项顺次的加入到表格中,当一行充满时候自动折行到下一行
   table.addCell("1.1");
   table.addCell("2.1");
   table.addCell("3.1");
   table.addCell("1.2");
   table.addCell("2.2");
   table.addCell("3.2");
   table.addCell("1.3");
   table.addCell("2.3");
   table.addCell("3.3");
   // 重新定义单元格
   cell = new PdfPCell(new Paragraph("cell test1"));
   // 定义单元格的框颜色
   cell.setBorderColor(new Color(255, 0, 0));
   // 把单元格加到表格上,默认为一个单元
   table.addCell(cell);
   // 重新定义单元格
   cell = new PdfPCell(new Paragraph("cell test2"));
   // 定义单元格的跨度
   cell.setColspan(2);
   // 定义单元格的背景颜色
   cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
   // 增加到表格上
   table.addCell(cell);
   document.add(new Paragraph("默认情况下的大小---居中 80%", fontCN));
   // 增加到文档中
   document.add(table);
   document.add(new Paragraph("居中 100%", fontCN));
   // 设置表格大小为可用空白区域的100%
   table.setWidthPercentage(100);
   // 增加到文档中2
   document.add(table);
   document.add(new Paragraph("居右 50%", fontCN));
   // 设置表格大小为可用空白区域的50%
   table.setWidthPercentage(50);
   // 设置水平对齐方式为 居右
   table.setHorizontalAlignment(Element.ALIGN_RIGHT);
   document.add(new Paragraph("居左 50%", fontCN));
   // 增加到文档中3
   document.add(table);
   // 设置水平对齐方式为 居左
   table.setHorizontalAlignment(Element.ALIGN_LEFT);
   document.add(table);
  } catch (Exception de) {
   de.printStackTrace();
  }
  // 步骤 5:关闭文档
  document.close();
 }
}



在上面的例子里,我们自己定义了一个3列的表格,而且对它进行了宽度设置和对齐方式的设置,但是细心的朋友会看到,所有的单元宽度都是相同的,因为iText为我们做了一些计算,在默认情况下,各单元格之间就是相同大小的,下面我们就讲一下,如何定义自己的单元格宽度。


想要做到这一点,我们需要PdfPTable(float[] relativeWidths)构造函数,它接受的是一个float数组,比喻说你定义一个有3列的表格,第一列的宽度为单位1,第二列也为单位1,第3列为单位2,那你就可以组织这样一个数组{1f,1f,2f},这个相关数组提供给这个构造函数以后,iText会为你自动计算,每一列到底应该多大。


一旦上面的这些操作完成,你还想改变表格的单元宽度,你可以使用setWidth()方法,我们也会在下面的例子里讲到。


更高级的部分请看:


If you want to work with absolute widths for the columns. You have to let iText calculate a widthpercentage for the table. In this case you should use: setWidthPercentage(float[] columnWidth, Rectangle pageSize). As you can see in the example, you need to do some calculations first to get the right pagesize. 
It even easier to use absolute widths if you lock the width of the table to a 'total width'. You need the methods setTotalWidth and setLockedWidth for this. In the example the relation between the different cells will remain 10%, 10%, 5%, 75%, so you'll have 2 columns with a width of 30pt, one with a width of 15pt and one that's 225pt wide. 
package com.itext.test;


import java.io.FileOutputStream;


import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;


/**
 * 定义PdfPTable的列宽.
 */
public class CellWidths {


 /**
  Width manipulations of cells.
  

  @param args
  
            no arguments needed
  */
 public static void main(String[] args) {


  System.out.println("Width");
  // 步骤 1: 创建一个document对象,大小为A4,上下左右边距都为36
  Document document = new Document(PageSize.A4, 36, 36, 36, 36);
  try {
   // 步骤 2:
   // 我们为document创建一个监听,并把PDF流写到文件中
   PdfWriter.getInstance(document, new FileOutputStream(
     "c:\\CellWidths.pdf"));
   // 步骤 3:打开文档
   document.open();
   // 创建一个有4列的表格,它们之间的相关比率为 10%,10%,5%,75%
   document.add(new Paragraph("We use 10%,10%,5%,75%:\n\n"));
   float[] widths = { 0.1f, 0.1f, 0.05f, 0.75f };
   PdfPTable table = new PdfPTable(widths);
   table.addCell("10%");
   table.addCell("10%");
   table.addCell("5%");
   table.addCell("75%");
   table.addCell("aa");
   table.addCell("aa");
   table.addCell("a");
   table.addCell("aaaaaaaaaaaaaaa");
   table.addCell("bb");
   table.addCell("bb");
   table.addCell("b");
   table.addCell("bbbbbbbbbbbbbbb");
   table.addCell("cc");
   table.addCell("cc");
   table.addCell("c");
   table.addCell("ccccccccccccccc");
   // 把定义好的表格增加到文档中
   document.add(table);
   document.add(new Paragraph("We change the percentages,20%,20%,10%,50%:\n\n"));
   // 修改表格列关联比 ,现在为20%,20%,10%,50%
   widths[0] = 20f;
   widths[1] = 20f;
   widths[2] = 10f;
   widths[3] = 50f;
   // 这句完成了表格列宽的修改
   table.setWidths(widths);
   document.add(table);
   // 再改变,使用绝对宽度
   widths[0] = 40f;
   widths[1] = 40f;
   widths[2] = 20f;
   widths[3] = 300f;
   // 定义右边距和上边距
   Rectangle r = new Rectangle(PageSize.A4.right(72), PageSize.A4
     .top(72));
   table.setWidthPercentage(widths, r);
   document.add(new Paragraph(
     "We change the percentage using absolute widths,40,40,20,300:\n\n"));
   document.add(table);
   // 使用一个固定的大小
   document.add(new Paragraph("We use a locked width,300:\n\n"));
   // 设置表格宽度
   table.setTotalWidth(300);
   table.setLockedWidth(true);
   document.add(table);
  } catch (Exception de) {
   de.printStackTrace();
  }
  // 步骤 5:关闭文档
  document.close();
 }
}

 

 

转载自:http://hi.baidu.com/lion98/blog/item/aca07bec343720d12e2e211a.html

分享到:
评论

相关推荐

    Java使用itext5实现PDF表格文档导出

    主要介绍了Java使用itext5实现PDF表格文档导出,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

    java源码包---java 源码 大量 实例

     [TablePanel.java] 日历表格面板  [ConfigLine.java] 控制条类  [RoundBox.java] 限定选择控件  [MonthMaker.java] 月份表算法类  [Pallet.java] 调色板,统一配色类 Java扫雷源码 Java生成自定义控件...

    程序员考试刷题-Quiz-Generator:该应用程序的目的是简化学术环境中的知识测试活动。目标群体包括学习编程语言的人,需要具有一定语言程

    申请涉及用户填写表格,指定测试将包含的问题数量、问题级别、问题类别(编程语言)、语言技能(程序员的语言技能、Example、JavaFX、Swing)、问题类型(网格、文本,布尔值)。 所有这些问题都存储在一个基本的...

    JAVA上百实例源码以及开源项目源代码

     [TablePanel.java] 日历表格面板  [ConfigLine.java] 控制条类  [RoundBox.java] 限定选择控件  [MonthMaker.java] 月份表算法类  [Pallet.java] 调色板,统一配色类 Java扫雷源码 Java生成自定义控件...

    成百上千个Java 源码DEMO 4(1-4是独立压缩包)

    日历表格面板 [ConfigLine.java] 控制条类 [RoundBox.java] 限定选择控件 [MonthMaker.java] 月份表算法类 [Pallet.java] 调色板,统一配色类 Java扫雷源码 Java生成自定义控件源代码 2个目标文件 Java实现HTTP连接...

    成百上千个Java 源码DEMO 3(1-4是独立压缩包)

    日历表格面板 [ConfigLine.java] 控制条类 [RoundBox.java] 限定选择控件 [MonthMaker.java] 月份表算法类 [Pallet.java] 调色板,统一配色类 Java扫雷源码 Java生成自定义控件源代码 2个目标文件 Java实现HTTP连接...

    javascript下用ActiveXObject控件替换word书签,将内容导出到word后打印第1/2页

    由于时间比较紧,没多的时候去学习研究上述工具包,现在用javascript操作ActiveXObject控件,用替换word模板中的书签方式解决。 前提条件: 1.浏览器安全级别降低,可以使用ActiveXObject控件。 2.装有office word...

    JAVA上百实例源码以及开源项目

     [TablePanel.java] 日历表格面板  [ConfigLine.java] 控制条类  [RoundBox.java] 限定选择控件  [MonthMaker.java] 月份表算法类  [Pallet.java] 调色板,统一配色类 Java扫雷源码 Java生成自定义控件...

    Java开发实战1200例(第1卷).(清华出版.李钟尉.陈丹丹).part3

     本书非常适合Java的初学者,如高校学生、求职人员作为练习、速查、学习使用,也适合Java程序员参考、查阅。 目 录 第1篇 Java语法与面向对象技术 第1章 开发环境的应用 2 1.1 Java环境 3 实例001 下载JDK开发...

    java源码包2

     [TablePanel.java] 日历表格面板  [ConfigLine.java] 控制条类  [RoundBox.java] 限定选择控件  [MonthMaker.java] 月份表算法类  [Pallet.java] 调色板,统一配色类 Java扫雷源码 Java生成自定义控件源...

    java源码包3

     [TablePanel.java] 日历表格面板  [ConfigLine.java] 控制条类  [RoundBox.java] 限定选择控件  [MonthMaker.java] 月份表算法类  [Pallet.java] 调色板,统一配色类 Java扫雷源码 Java生成自定义控件源...

    java源码包4

     [TablePanel.java] 日历表格面板  [ConfigLine.java] 控制条类  [RoundBox.java] 限定选择控件  [MonthMaker.java] 月份表算法类  [Pallet.java] 调色板,统一配色类 Java扫雷源码 Java生成自定义控件源...

Global site tag (gtag.js) - Google Analytics