- 浏览: 72567 次
- 性别:
- 来自: 惠州
文章分类
最新评论
-
woyaonuli:
各位大侠,请教下,怎么让生成的pdf每页都有背景图片, ...
iText 跨行and背景图片 -
lixia0417:
恩,LZ,谢谢了,那我还是练习把SSh的例子改成S2sh的吧, ...
struts2+hibernate3+spring2读书笔记4(数据校验器) -
hhr_michael:
哥们,你好,这本书着重s2sh的各个部分开发,而整合的例子不是 ...
struts2+hibernate3+spring2读书笔记4(数据校验器) -
lixia0417:
对,哥们,问一下,这本书中关于S2SH整合开发的例子多吗,就是 ...
struts2+hibernate3+spring2读书笔记4(数据校验器) -
hhr_michael:
谢谢提醒,由于只是着重校验的过程,所以这代码没有在struts ...
struts2+hibernate3+spring2读书笔记4(数据校验器)
iText 跨行and背景图片(轉)
最近用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);
不过这种方法对表格不起作用
表格里加上图片后,文字怎么都写不到图片上去.
请有过经验的同志们,告知如何给表格加背景图
最近用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);
不过这种方法对表格不起作用
表格里加上图片后,文字怎么都写不到图片上去.
请有过经验的同志们,告知如何给表格加背景图
评论
1 楼
woyaonuli
2011-12-20
各位大侠,请教下,怎么让生成的pdf每页都有背景图片,
Image img = null;
img = Image.getInstance(backimg);
img.setAbsolutePosition(0, 0);
img.setAlignment(Image.UNDERLYING);
document.add(img);
这种方式只能在第一页或最后一页插入背景图片,哪位大侠知道,还望不吝赐教
Image img = null;
img = Image.getInstance(backimg);
img.setAbsolutePosition(0, 0);
img.setAlignment(Image.UNDERLYING);
document.add(img);
这种方式只能在第一页或最后一页插入背景图片,哪位大侠知道,还望不吝赐教
发表评论
-
itext 带表头
2010-08-12 16:19 1430itext 带表头有两种方法以: 第一种是Table的,第二 ... -
extremeTable 介绍
2010-08-10 16:12 1085何为 extremeTable,又一个开源taglib ext ... -
Java线程:创建与启动 (转)
2010-07-21 08:54 1917版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 ... -
用Java简单的读取pdf文件中的数据(转)
2010-05-20 09:44 1269用Java简单的读取pdf文件中的数据: 第一步:下载PDFB ... -
非java.util.zip,使用ant.jar制作zip压缩文件,以及相关中文解决方法(轉)
2010-02-10 10:52 885用java.util.zip制作zip压缩文件时,如果制作的压 ... -
JSP环境配置使用fckeditor
2010-02-09 09:45 839JSP环境配置使用fckeditor 关键字: jsp环境配置 ... -
jsp分页
2010-02-09 09:32 923jsp分页 (轉) 关键字: jsp分页 public cl ... -
java 生成excel
2010-02-09 09:18 949java 生成excel (轉) 关键字: java 生成 ... -
input只能输入数字
2010-01-07 15:35 1516input只能输入数字 方法一: <input t ... -
关于DIV被SELECT遮挡问题
2009-12-14 09:00 1014<html> <head> ... -
iText加入页码
2009-11-30 09:18 2852在網上找了點資料,如果單單實現頁碼的顯示,隻需在Documen ... -
struts标签使用举例--logic篇
2009-11-09 15:59 600来源:http://hi.baidu.com/lissnet/ ... -
Eclipse快捷键大全
2009-11-09 12:20 700Ctrl+1 快速修复(最经典 ... -
class.forName().newInstance()的作用
2009-11-02 09:49 1949在Java开发特别是数据库开发中,经常会用到Class.for ... -
[SQLServer]传入的表格格式数据流(TDS)远程过程调用(RPC)协议流不正确2008-05-28 17:57关键字: sqlserver 2000 d
2009-10-27 15:49 1179[SQLServer]传入的表格格式数据流(TDS)远程过程调 ... -
itext實現水印功能
2009-09-07 12:04 2566近日要用itext來做水印功能,在網上找了好多資料,但都是把原 ... -
pdf成生方法精品
2009-09-07 11:50 785最近由于项目需要,开始使用iText写PDF文件,从网上搜索到 ...
相关推荐
用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和源码,给大家分享下