- 浏览: 502216 次
- 性别:
- 来自: 惠州
文章分类
- 全部博客 (255)
- ant (1)
- springMVC (2)
- ajax (4)
- oracle (12)
- SSH (13)
- struts1 (2)
- Hibernate (14)
- spring (5)
- jstl (1)
- 连接池 (1)
- acegi (4)
- java (17)
- jquery (11)
- div+css (4)
- drupal (1)
- php (8)
- freemaker调模板生成静态页面 (1)
- xml (1)
- json (2)
- javascript (9)
- 正则表达式 (4)
- Ext (8)
- jdbc (1)
- sql server (2)
- perl (5)
- db4o (1)
- webservice (4)
- flex (13)
- it资讯 (1)
- joomla (0)
- 设计模式 (1)
- struts2 (4)
- s2sh (8)
- linux (3)
- ejb (2)
- android旅途 (24)
- android (36)
- C/C++ (16)
- mysql (1)
最新评论
-
fengyuxing168:
IBelyService bs = IBelyService. ...
为 Android 添加 Java 层服务也就是添加自定义的aidl服务到serviceManager 通过ServiceManager.getService取 -
dengzhangtao:
"由于ActivityManagerService是 ...
binder理解 -
yzyspy:
ActivityManagerService:startHom ...
Android的Launcher成为系统中第一个启动的,也是唯一的 -
Matchstick:
使用SELECT DISTINCT alias FROM Po ...
hibernate 一对多表查询时fetchMode.join 生成left outer join 出来数据重复问题 -
dlheart:
没看懂你什么意思啊,我遇到的问题是一对多,设了fetch = ...
hibernate 一对多表查询时fetchMode.join 生成left outer join 出来数据重复问题
最近用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中。有点拗口,看代码吧:
- package cn.chenkun.iText;
- import java.io.FileOutputStream;
- import com.lowagie.text.Document;
- import com.lowagie.text.PageSize;
- import com.lowagie.text.Paragraph;
- import com.lowagie.text.pdf.PdfPCell;
- import com.lowagie.text.pdf.PdfPTable;
- import com.lowagie.text.pdf.PdfWriter;
- public class Colspan {
- public static void main(String[] args) {
- colspan();
- }
- private static void colspan(){
- Document document = new Document(PageSize.A4, 36, 36, 36, 36);
- try {
- PdfWriter.getInstance(document, new FileOutputStream("d:\\coslpan.pdf"));
- document.open();
- PdfPTable table = new PdfPTable(4); // 最外层table
- float[] wid = {80f, 100f, 80f, 60f};
- table.setTotalWidth(wid);
- table.setLockedWidth(true);
- PdfPCell cell = null; // 最外层table的cell
- PdfPTable iTable = null; // 嵌套的table
- PdfPCell iCell = null; // 嵌套的table的cell
- iTable = new PdfPTable(3);
- float[] iWid = {80f, 100f, 80f};
- iTable.setTotalWidth(iWid);
- iTable.setLockedWidth(true);
- iCell = new PdfPCell(new Paragraph("column 1"));
- iCell.setFixedHeight(30);
- iTable.addCell(iCell);
- iCell.setColspan(2);
- iTable.addCell(iCell);
- iCell = new PdfPCell(new Paragraph("column 2"));
- iCell.setFixedHeight(30);
- iTable.addCell(iCell);
- iTable.addCell(iCell);
- iTable.addCell(iCell);
- iCell = new PdfPCell(new Paragraph("column 3"));
- iCell.setFixedHeight(30);
- iTable.addCell(iCell);
- iTable.addCell(iCell);
- iTable.addCell(iCell);
- cell = new PdfPCell(iTable); // 用这个table初始外层table的cell
- cell.setColspan(3); // 设置它跨3列
- cell.setFixedHeight(3*30); // 设置它的高度
- table.addCell(cell); // 将这个cell加入table中
- iTable = new PdfPTable(1);
- float[] iWid2 = {60f};
- iTable.setTotalWidth(iWid2);
- iTable.setLockedWidth(true);
- iCell = new PdfPCell(new Paragraph("i am here"));
- iTable.addCell(iCell);
- cell = new PdfPCell(iTable);
- cell.setFixedHeight(3*30); // 跨3列了
- table.addCell(cell);
- document.add(table);
- } catch (Exception de) {
- de.printStackTrace();
- }
- document.close();
- }
- }
2. iText的背景图片
iText中的PdfPCell都是有自己默认的布局的,要实现自己的布局,必须实现PdfPCellEvent接口,在方法cellLayout中定义自己的布局。更多信息见<<iText in Action>>中10.2 Working with iText’s direct content。Figure 10.6实际上已经实现了背景图片,这里将代码改写如下:
- package cn.chenkun.iText;
- import java.io.FileOutputStream;
- import com.lowagie.text.Document;
- import com.lowagie.text.Element;
- import com.lowagie.text.Image;
- import com.lowagie.text.PageSize;
- import com.lowagie.text.Rectangle;
- import com.lowagie.text.pdf.PdfContentByte;
- import com.lowagie.text.pdf.PdfPCell;
- import com.lowagie.text.pdf.PdfPCellEvent;
- import com.lowagie.text.pdf.PdfPTable;
- import com.lowagie.text.pdf.PdfWriter;
- public class PicBackGround {
- public static void main(String[] args) {
- picBg();
- }
- private static void picBg() {
- Document document = new Document(PageSize.A4, 36, 36, 36, 36);
- try {
- PdfWriter.getInstance(document, new FileOutputStream("d:\\testPicBG.pdf"));
- document.open();
- BGPic border = new BGPic();
- float wid = 80f;
- float hei = 100f;
- float[] widArr = { wid, wid };
- PdfPTable table = new PdfPTable(2);
- table.setTotalWidth(widArr);
- table.setLockedWidth(true);
- PdfPCell cell = null;
- for (int i = 1; i <= 4; i++) {
- Image img = Image.getInstance("d:/ma.jpg");
- cell = new PdfPCell(img, true);
- cell.setFixedHeight(hei);
- cell.setHorizontalAlignment(Element.ALIGN_CENTER);
- cell.setPadding(8);
- cell.setCellEvent(border); // 加入背景图片
- table.addCell(cell);
- }
- document.add(table);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- document.close();
- }
- }
- }
- class BGPic implements PdfPCellEvent {
- public void cellLayout(PdfPCell cell, Rectangle rect, PdfContentByte[] canvas) {
- PdfContentByte cb = canvas[PdfPTable.BACKGROUNDCANVAS];
- Image img = null;
- try {
- img = Image.getInstance("d:/hai.jpg");
- img.scaleAbsolute(80, 100); // 设置背景图片的大小
- img.setAbsolutePosition(298, 606); // 设置第一个背景图片的绝对位置
- cb.addImage(img);
- img.setAbsolutePosition(217, 706); // 设置第二个背景图片的绝对位置
- cb.addImage(img);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
//---------------------------
百度里有搜到一种背景图设置法
Image jpgBack = Image.getInstance(xxxxx);
jpgBack.setAlignment(Image.UNDERLYING);
不过这种方法对表格不起作用
表格里加上图片后,文字怎么都写不到图片上去.
请有过经验的同志们,告知如何给表格加背景图
发表评论
-
关于JAVA多线程同步
2011-09-29 10:39 783因为需要,最近关注了一下JAVA多线程同步问题。JAVA多线程 ... -
java多线程之 wait(),notify(),notifyAll()
2011-05-27 17:25 1326wait(),notify(),notifyAll()不属 ... -
JAVA反射机制的学习
2010-11-15 22:54 851JAVA反射机制的学习2009-07-06 11:33JAVA ... -
Java线程:线程的交互
2010-07-21 09:26 906Java线程:线程的交互 ---------------- ... -
JSP数据导出到EXCEL超简便方法
2010-05-24 09:31 1632JSP数据导出到EXCEL简便方法(2008-11-19 15 ... -
iText经验总结
2010-05-04 21:49 2382iText经验总结 因为前些 ... -
org.apache.commons.lang.StringUtil 常用方法使用例子
2010-03-26 11:00 10331.空字符串检查使用函数: StringUtils.isBla ... -
java 多线程 实现
2010-03-10 15:20 952多线程是这样一种机制 ... -
java反射机制
2010-01-22 10:19 966Reflection 是Java被视为动态(或准动态)语 ... -
利用iText写PDF心得
2009-08-11 09:56 1458利用iText写PDF心得 & ... -
使用itext生成pdf
2009-08-10 11:20 1413一、前言 在企业的信息系统中,报表处理一直占比较重要的作 ... -
java 生成excel
2009-05-15 11:24 15701.类文件makexls package com; impor ... -
jsp分页
2009-03-06 10:43 842public class PageHelper { //to ... -
JSP环境配置使用fckeditor
2009-02-18 09:59 1206JSP环境配置使用fckeditor 录入时间 2008-12 ... -
使用J2SE API读取Properties文件的六种方法
2008-10-07 19:27 755使用J2SE API读取Properties文件的六种方法 ... -
native2ascii转码
2008-10-07 17:22 13751,原理 Property 文件中 ...
相关推荐
用Itext操作word,可以在word里画表格,也可以往word里插入图片
利用itext将html转为pdf,支持中文,和图片,代码地址在https://www.cnblogs.com/bert227/p/9724254.html
利用itext读取PDF后使用pdfbox将PDF转为图片。压缩包内附有相应JAR文件
使用Itext简单生产了PDF文件,添加图片页眉的简单例子
itext5生成PDF(含水印文字及图片)的源代码,操作说明详细,代码完整可用。
使用iText-2.0.8将Html转换为pdf,带图片功能
java 使用IText导出PDF(包含图片、表格、文字等等Demo和依赖包),最全IText使用Demo,下载即可用
生成pdf的中文问题解决办法、导出pdf时,设置段落行间距方法,表单填充,使用itext方法填充相应表单域。
itext生成PDF图片文档
itext将图片生成PDF(高清版),可按比例缩放,三种可选的缩放方式,导出高清版的PDF,直接可导入使用的demo文件,内含jar包,无坑版!
itext工具jar包实现图片转pdf,可以根据不同的方案来等比伸缩图片的大小,保证正常情况下图片不失真。
在已有pdf文件中,指定页及绝对位置添加图片(包含itextpdf.jar及实例)
itext pdf word 中文 表格 图片,看了这个你就能用java生成word,pdf并插入图片,中文,表格。里面还有itext的API包括中文英文。
Itext生成pdf,图片水印,图片背景,字体设置,页眉页脚
由于工作需要,小研究了下itext,在此和大家分享下,互相探讨,也希望能帮得上哪位朋友。 功能:java生成pdf,支持中文,可以插入图片,可以添加超链 接连到其他网址。 内附代码及详细说明和需要的jar包
Itext5文本转PDF,含图片,代码实例,是5版本的,含亚洲文字支持,有文本,图片转pdf的实例,支持自定义编码,整理好的,直接能用,乱码是因为编码不对,设置编码就好了,具体百度怎样设置编码,里面有设置编码的接口...
IText 和 ITextAsian 的开发包
itext生成带图片的,带表格的复杂样式的word文档范例代码,代码中包含需要的jar包,现在侯可以直接使用。
iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。 iText的安装非常方便,下载iText.jar文件后,...
因为项目需要用到pdf模板生成pdf,其中模板中有图片,找了好久没有资料,无奈之下翻看api和源码,给大家分享下