- 浏览: 1209028 次
- 性别:
- 来自: 深圳
文章分类
- 全部博客 (718)
- HTML (13)
- JS基础 (23)
- JS应用 (40)
- AJAX (6)
- JSP相关 (12)
- JAVA基础 (52)
- JAVA应用 (74)
- APPLET (11)
- SWING\RCP (2)
- JAVA反射 (6)
- 设计模式 (26)
- 数据库设计 (20)
- Struts (35)
- Struts2 (12)
- Spring (22)
- Hibernate (45)
- Ibatis (18)
- mybatis (3)
- SSH (8)
- UML (5)
- WebService (3)
- XML (16)
- Log4j (7)
- WEB容器 (26)
- 数据结构 (36)
- Linux (34)
- Ruby on Rails (1)
- 其它技术 (27)
- IDE配置 (15)
- 项目实战 (2)
- Oracle (69)
- JAVA报表 (7)
- Android学习 (2)
- 博客链接 (1)
- 网络基础 (1)
- WEB集群 (1)
- .Net开发 (11)
- PB (4)
- 系统构建 (15)
最新评论
-
jnjeC:
牛逼啊哥们,讲得太好了
Maven仓库理解、如何引入本地包、Maven多种方式打可执行jar包 -
九尾狐的yi巴:
很好 感谢!
Itext中文处理(更新版) -
luweifeng1983:
有用的,重启一下嘛。
设置eclipse外部修改文件后自动刷新 -
Master-Gao:
设置了也不管用,怎么破呢?
设置eclipse外部修改文件后自动刷新 -
aigo_h:
锋子还有时间写博客,还是很闲哈!
Add directory entries问题
本文转自:http://hi.baidu.com/lion98/blog/item/aca07bec343720d12e2e211a.html
一个最基本的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();
}
}
发表评论
-
JAVA短信发送及JAVA发送http请求与处理
2016-06-22 14:34 522JAVA发送HTTP请求与处理参考: http://ww ... -
Java Zip应用
2016-06-22 14:30 548应用系统中使用jdk原生 ... -
模拟tomcat进行http请求及响应处理:BufferedReader类的readLine在socket网络编程应用时发生阻塞
2016-05-11 15:04 1667最近写一个简单的程序模拟tomcat进行http请求及响应 ... -
Castor-xml映射出现节点重复问题解决
2015-08-13 16:24 1189最近在使用Castor-xml生成XML时发现生成的格式不 ... -
Add directory entries问题
2014-08-05 10:24 1843用spring注解的项目,eclipse可以运行,打成jar ... -
BeanUtils.copyProperties与PropertyUtils.copyProperties用法及区别
2013-01-25 16:21 858http://www.cnblogs.com/fayf/ar ... -
java.net.BindException: Cannot assign requested address
2012-06-13 14:10 869转:http://wgkgood.blog.51cto ... -
Duplicate name in Manifest: Depends-On
2012-06-13 13:13 1616解决方法:http://ww2.sjc.edu/faculty ... -
java tree
2011-11-16 02:01 1266import java.util.ArrayList; im ... -
jacob操作word和excel
2011-07-28 17:00 873http://danadler.com/jacob/ -
获得PrepareStatement 的最终执行语句
2011-07-04 10:07 985方法一:http://www.blogjava.net/ ... -
Non-terminating decimal expansion; no exact representable decimal result
2011-06-24 16:03 2582由于需要处理精度比较高的浮点数,所以弃用double类型,改用 ... -
JXLS应用示例
2011-06-03 01:41 3426JXLS在循环的记录前加序号: <jx:f ... -
月份加减方法处理
2011-06-03 00:52 899private static String monthAdd ... -
JAVA 5.0 后 自带监控工具 jps、jinfo、jstat、jma
2011-05-12 16:25 1222最近有个客户要进行WebLogic性能调优,但因为他们没有性能 ... -
定时器的实现、java定时器介绍与Spring中定时器的配置
2011-04-28 15:05 25141定时器的作用 在实际的开发中,如果项目中需要定时执行或者需 ... -
ClientAbortException,Connection reset by peer: socket write error
2011-03-07 11:24 6037extremetable导出excel,弹出一个下载窗口,这 ... -
java 调ireport (javaBean数据源)保存服务器 生成word
2011-02-24 14:40 1523package com.ztesoft.bidding.con ... -
JAVA包生成EXE应用实例
2011-01-06 15:35 1007相关文章:http://blog.csdn.net/fresh ... -
RMI客户端调用远程服务器方法-远程方法调用
2010-12-27 16:45 2720RMI软件下载:http://www.genady.net/r ...
相关推荐
用Itext操作word,可以在word里画表格,也可以往word里插入图片
iText7 原生方法制作带斜线表格
一段经典的处理iText打印pdf表格代码。
iText页码、页眉、页脚,itext 的复杂表格实现、保留表头、表尾、根据需求生成不同的iText表格
iText创建表格pdf的教程,使用pdfptable创建表单!
用iText生成比较简单的表格形式Word
itext中文操作手册,java操作word pdf的利器
java 生成pdf itext 学习详解
NULL 博文链接:https://ilove2009.iteye.com/blog/655973
iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。
资源包含iText.kernel源码和读取表格数据源码,运行TableExtractionFromPDF项目可查看效果。 iText.kernel版本7.1.3.0 iText.io版本7.1.3.0 原文网址:...
itext生成带图片的,带表格的复杂样式的word文档范例代码,代码中包含需要的jar包,现在侯可以直接使用。
itext,是一个开发源代码的项目,你可以使用itext方便的实现pdf的输出,打印报表
NULL 博文链接:https://x125858805.iteye.com/blog/2239773
包含 itext-asian-5.2.0.jar itextpdf-5.5.5.jar
请多多支持,哥们也是千辛万苦总结的,为了大家就不要资源分,请自觉尊重原创,注明原创地址。
本工程为MyEclipse创建的java工程,用于演示使用iText jar包如何实现对word文件的操作,这些操作是多方面的,包括对表格的创建合并等。 本工程编码方式:GBK。 下载下来解压拷贝到MyEclipse中即可运行,欢迎下载。
NULL 博文链接:https://oneinit.iteye.com/blog/1529917
itext pdf word 中文 表格 图片,看了这个你就能用java生成word,pdf并插入图片,中文,表格。里面还有itext的API包括中文英文。
itext对pdf进行编辑.使用Adobe对pdf创建表单,itext对表单填充,下拉选,单选,文本框.复选框.itext对pdf进行编辑.使用Adobe对pdf创建表单,itext对表单填充,下拉选,单选,文本框.复选框