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/wpiwp125355123/blog/item/dd4fda0bac774a34b0351dee.html

猜你喜欢

转载自fuchangle.iteye.com/blog/1567779