- 浏览: 210817 次
- 性别:
- 来自: 广州
文章分类
最新评论
一个最基本的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
no arguments needed
throws IOException
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();
}
}
发表评论
-
PDF文件加密和读取
2009-04-12 16:01 1530import java.io.FileInputStream; ... -
使用itext操作PDF文档
2009-04-11 19:21 2491准备工作:到http://www.lo ... -
从Java应用程序动态生成PDF文件
2009-04-11 19:10 1173如果应用程序需要动态 ... -
itext学习
2009-04-11 18:51 1550package com.itext.test; imp ... -
iText 实现 PDF 报表下载
2009-04-11 15:09 2948很久没更新 blog 了,工作和一些事情占用了大部分精力,实在 ... -
使用iText输出PDF报表
2009-04-11 13:35 2288最近,工作中,用到iText输出PDF格式的报表, ... -
用JAVA实现PDF报表
2009-04-11 12:14 967一、前言 在企业的信息系统中,报表处理一直占比较 ...
相关推荐
一段经典的处理iText打印pdf表格代码。
iText7 原生方法制作带斜线表格
NULL 博文链接:https://ilove2009.iteye.com/blog/655973
包含 itext-asian-5.2.0.jar itextpdf-5.5.5.jar
itext生成带图片的,带表格的复杂样式的word文档范例代码,代码中包含需要的jar包,现在侯可以直接使用。
用iText生成比较简单的表格形式Word
NULL 博文链接:https://x125858805.iteye.com/blog/2239773
iText页码、页眉、页脚,itext 的复杂表格实现、保留表头、表尾、根据需求生成不同的iText表格
资源包含iTextSharp7(net40及netstandard1.6下的库文件),iText.kernel源码和读取表格数据源码,运行TableExtractionFromPDF项目可查看效果。 iText.kernel版本7.1.3.0 iText.io版本7.1.3.0 原文网址:...
iText创建表格pdf的教程,使用pdfptable创建表单!
用Itext操作word,可以在word里画表格,也可以往word里插入图片
资源包含iText.kernel源码和读取表格数据源码,运行TableExtractionFromPDF项目可查看效果。 iText.kernel版本7.1.3.0 iText.io版本7.1.3.0 原文网址:...
itextpdf包及复杂结构实现,几个例子运行一下,看看,基本上你就会了
NULL 博文链接:https://lucifer-li6.iteye.com/blog/1637687
java 使用iText生成word文档,包中带有源码与所用jar包,生成文档格式标准。可分页,可带页眉,可生成表格、图片和文字等word元素内容,文字带字号,字体,颜色等。
itext pdf word 中文 表格 图片,看了这个你就能用java生成word,pdf并插入图片,中文,表格。里面还有itext的API包括中文英文。
新需求,隔行换色,itext in action 是个很好的说明书,照着英文读下来,很简单的进行了实现,思路如下: 1.先创建PdfPTable对象,生成PDF表格cell之后,添加隔行换色的事件,将此事件在PdfPTable加入Document对象...
IText相关的学习文档整理,包括操作PDF和word, 供其他开发人员使用和学习
主要介绍了Java使用itext5实现PDF表格文档导出,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
itext对pdf进行编辑.使用Adobe对pdf创建表单,itext对表单填充,下拉选,单选,文本框.复选框.itext对pdf进行编辑.使用Adobe对pdf创建表单,itext对表单填充,下拉选,单选,文本框.复选框