itext表格居中.docx

上传人:b****1 文档编号:14614553 上传时间:2023-06-25 格式:DOCX 页数:11 大小:20.88KB
下载 相关 举报
itext表格居中.docx_第1页
第1页 / 共11页
itext表格居中.docx_第2页
第2页 / 共11页
itext表格居中.docx_第3页
第3页 / 共11页
itext表格居中.docx_第4页
第4页 / 共11页
itext表格居中.docx_第5页
第5页 / 共11页
itext表格居中.docx_第6页
第6页 / 共11页
itext表格居中.docx_第7页
第7页 / 共11页
itext表格居中.docx_第8页
第8页 / 共11页
itext表格居中.docx_第9页
第9页 / 共11页
itext表格居中.docx_第10页
第10页 / 共11页
itext表格居中.docx_第11页
第11页 / 共11页
亲,该文档总共11页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

itext表格居中.docx

《itext表格居中.docx》由会员分享,可在线阅读,更多相关《itext表格居中.docx(11页珍藏版)》请在冰点文库上搜索。

itext表格居中.docx

itext表格居中

竭诚为您提供优质文档/双击可除

itext,表格居中

  篇一:

itext使用简单介绍

  pdFitext使用简单介绍

  发布日期:

20xx-06-19

  一、前言

  在企业的信息系统中,报表处理一直占比较重要的作用,本文将介绍一种生成pdF报表的java组件--itext。

通过在服务

  器端使用jsp或javabean生成pdF报表,客户端采用超级连接显示或下载得到生成的报表,这样就很好的解决了b/s系统的报表处理问题。

  二、itext简介

  itext是著名的开放源码的站点sourceforge一个项目,是用于生成pdF文档的一个java类库。

通过itext不仅可以生成pdF或

  rtf的文档,而且可以将xml、html文件转化为pdF文件。

  itext的安装非常方便,在/itext/download.html-download网站上下载itext.jar文件后,只需

  要在系统的classpath中加入itext.jar的路径,在程序中就可以使用itext类库了。

  三、建立第一个pdF文档

  用itext生成pdF文档需要5个步骤:

  ①建立com.lowagie.text.document对象的实例。

  documentdocument=newdocument();

  ②建立一个书写器(writer)与document对象关联,通过书写器(writer)可以将文档写入到磁盘中。

  pdFwriter.getinstance(document,newFileoutputstream("helloworld.pdF"));

  ③打开文档。

  document.open();

  ④向文档中添加内容。

  document.add(newparagraph("helloworld"));

  ⑤关闭文档。

  document.close();

  通过上面的5个步骤,就能产生一个helloworld.pdF的文件,文件内容为"helloworld"。

  建立com.lowagie.text.document对象的实例

  com.lowagie.text.document对象的构建函数有三个,分别是:

  publicdocument();

  publicdocument(Rectanglepagesize);

  publicdocument(Rectanglepagesize,intmarginleft,intmarginRight,intmargintop,int

  marginbottom);

  构建函数的参数pagesize是文档页面的大小,对于第一个构建函数,页面的大小为a4,同document(pagesize.a4)的效果一样;

  对于第三个构建函数,参数marginleft、marginRight、margintop、marginbottom分别为左、右、上、下的页边距。

  通过参数pagesize可以设定页面大小、面背景色、以及页面横向/纵向等属性。

itext定义了a0-a10、al、letteR、halFletteR、_

  11x17、ledgeR、note、b0-b5、aRch_a-aRch_e、Flsa和Flse等纸张类型,也可以通过Rectanglepagesize=newRectangle

  (144,720);自定义纸张。

通过Rectangle方法rotate()可以将页面设置成横向。

  书写器(writer)对象

  一旦文档(document)对象建立好之后,需要建立一个或多个书写器(writer)对象与之关联。

通过书写器(writer)对象可以将具体

  文档存盘成需要的格式,如com.lowagie.text.pdF.pdFwriter可以将文档存成pdF文件,com.lowagie.text.html.htmlwriter可

  以将文档存成html文件。

  设定文档属性

  在文档打开之前,可以设定文档的标题、主题、作者、关键字、装订方式、创建者、生产者、创建日期等属性,调用的方法分别是:

  publicbooleanaddtitle(stringtitle)

  publicbooleanaddsubject(stringsubject)

  publicbooleanaddkeywords(stringkeywords)

  publicbooleanaddauthor(stringauthor)

  publicbooleanaddcreator(stringcreator)

  publicbooleanaddproducer()

  publicbooleanaddcreationdate()

  publicbooleanaddheader(stringname,stringcontent)

  其中方法addheader对于pdF文档无效,addheader仅对html文档有效,用于添加文档的头信息。

  当新的页面产生之前,可以设定页面的大小、书签、脚注(headerFooter)等信息,调用的方法是:

  publicbooleansetpagesize(Rectanglepagesize)

  publicbooleanadd(watermarkwatermark)

  publicvoidremovewatermark()

  publicvoidsetheader(headerFooterheader)publicvoidresetheader()publicvoidsetFooter(headerFooterfooter)publicvoidresetFooter()publicvoidresetpagecount()publicvoidsetpagecount(intpagen)如果要设定第一页的页面属性,这些方法必须在文档打开之前调用。

  对于pdF文档,itext还提供了文档的显示属性,通过调用书写器的setViewerpreferences方法可以控制文档打开时acrobatReader

  的显示属性,如是否单页显示、是否全屏显示、是否隐藏状态条等属性。

  另外,itext也提供了对pdF文件的安全保护,通过书写器(writer)的setencryption方法,可以设定文档的用户口令、只读、可打

  印等属性。

  添加文档内容

  所有向文档添加的内容都是以对象为单位的,如phrase、paragraph、table、graphic对象等。

比较常用的是段落(paragraph)对象,

  用于向文档中添加一段文字。

  四、文本处理

  itext中用文本块(chunk)、短语(phrase)和段落(paragraph)处理文本。

  文本块(chunk)是处理文本的最小单位,有一串带格式(包括字体、颜色、大小)的字符串组成。

如以下代码就是产生一个字体为helVetica、

  大小为10、带下划线的字符串:

  chunkchunk1=newchunk("thistextisunderlined",FontFactory.getFont(FontFactory.helVetica,12,Font.undeRline));

  短语(phrase)由一个或多个文本块(chunk)组成,短语(phrase)也可以设定字体,但对于其中以设定过字体的文本块(chunk)无效。

通过

  短语(phrase)成员函数add可以将一个文本块(chunk)加到短语(phrase)中,如:

phrase6.add(chunk);

  段落(paragraph)由一个或多个文本块(chunk)或短语(phrase)组成,相当于woRd文档中的段落概念,同样可以设定段落的字体大小、颜

  色等属性。

另外也可以设定段落的首行缩进、对齐方式(左对齐、右对齐、居中对齐)。

通过函数setalignment可以设定段落的对

  齐方式,setalignment的参数1为居中对齐、2为右对齐、3为左对齐,默认为左对齐。

  五、表格处理

  itext中处理表格的类为:

com.lowagie.text.table和com.lowagie.text.pdF.pdFptable,对于比较简单的表格处理可以用

  com.lowagie.text.table,但是如果要处理复杂的表格,这就需要com.lowagie.text.pdF.pdFptable进行处理。

这里就  

  com.lowagie.text.table进行说明。

  类com.lowagie.text.table的构造函数有三个:

  ①table(intcolumns)

  ②table(intcolumns,introws)

  ③table(propertiesattributes)

  参数columns、rows、attributes分别为表格的列数、行数、表格属性。

创建表格时必须指定表格的列数,而对于行数可以不用指定。

  建立表格之后,可以设定表格的属性,如:

边框宽度、边框颜色、衬距(paddingspace即单元格之间的间距)大小等属性。

下面通

  过一个简单的例子说明如何使用表格,代码如下:

  1:

tabletable=newtable(3);

  2:

table.setborderwidth

(1);

  3:

table.setbordercolor(newcolor(0,0,255));

  4:

table.setpadding(5);

  5:

table.setspacing(5);

  6:

cellcell=newcell("header");

  7:

cell.setheader(true);

  8:

cell.setcolspan(3);

  9:

table.addcell(cell);

  10:

table.endheaders();

  11:

cell=newcell("examplecellwithcolspan1androwspan2");

  12:

cell.setRowspan

(2);

  13:

cell.setbordercolor(newcolor(255,0,0));

  14:

table.addcell(cell);

  15:

table.addcell("1.1");

  16:

table.addcell("2.1");

  17:

table.addcell("1.2");

  18:

table.addcell("2.2");

  19:

table.addcell("celltest1");

  20:

cell=newcell("bigcell");

  21:

cell.setRowspan

(2);

  22:

cell.setcolspan

(2);

  23:

table.addcell(cell);

  24:

table.addcell("celltest2");

  运行结果如下:

  header

  examplecellwithcolspan1androwspan21.12.1

  1.22.2

  celltest1bigcell

  celltest2

  代码1-5行用于新建一个表格,如代码所示,建立了一个列数为3的表格,并将边框宽度设为1,颜色为蓝色,衬距为5。

  代码6-10行用于设定表格的表头,第7行cell.setheader(true);是将该单元格作为表头信息显示;第8行cell.setcolspan(3);

  指定了该单元格占3列;为表格添加表头信息时,要注意的是一旦表头信息添加完了之后,必须调用endheaders()方法,如

  第10行,否则当表格跨页后,表头信息不会再显示。

  代码11-14行是向表格中添加一个宽度占一列,长度占二行的单元格。

  往表格中添加单元格(cell)时,按自左向右、从上而下的次序添加。

如执行完11行代码后,表格的右下方出现2行2列的空白,这是

  再往表格添加单元格时,先填满这个空白,然后再另起一行,15-24行代码说明了这种添加顺序。

  六、图像处理

  itext中处理表格的类为com.lowagie.text.image,目前itext支持的图像格式有:

giF,jpeg,png,wmf等格式,对于不同的图像

  格式,itext用同样的构造函数自动识别图像格式。

通过下面的代码分别获得gif、jpg、png图像的实例。

  imagegif=image.getinstance("vonnegut.gif");

  imagejpeg=image.getinstance("mykids.jpg");

  imagepng=image.getinstance("hitchcock.png");

  篇二:

利用itext生成复杂pdf

  利用itext生成复杂pdf

  内容包括:

复杂表格的生成(单元格合并,边框,位置布局,背景色)

  页脚输出,中文字体输出

  划直线

  下载包itext-5.0.3.jar

  importjava.io.Fileoutputstream;

  importjava.io.ioexception;

  .malformeduRlexception;

  importcom.itextpdf.text.*;

  importcom.itextpdf.text.pdf.baseFont;

  importcom.itextpdf.text.pdf.cmykcolor;

  importcom.itextpdf.text.pdf.columntext;

  importcom.itextpdf.text.pdf.pdfpcell;

  importcom.itextpdf.text.pdf.pdfptable;

  importcom.itextpdf.text.pdf.pdfwriter;

  importcom.itextpdf.text.pdf.draw.lineseparator;

  publicclassitexttest{

  //publicvoidonendpage(pdfwriterpdfwriter,documentdocument){

  //columntext.showtextaligned(pdfwriter.getdirectcontent(),

  //element.align_centeR,newphrase(string.format("page%d",//设置页码

  //pdfwriter.getpagenumber())),300,50,0);

  //}

  publicstaticvoidmain(string[]args)throwsdocumentexception,

  malformeduRlexception,ioexception{

  //1、实例化文档对象

  documentdocument=newdocument(pagesize.a4,37,30,50,40);

  //2、创建pdfwriter对象(其他编写器包括htmlwriter、Rtfwriter、xmlwriter)pdfwriterwriter=pdfwriter.getinstance(document,newFileoutputstream("d:

\\new.pdf"));document.open();//生成的pdf存放位置

  //中文字体格式

  //baseFontbfchinese=baseFont.createFont("simyou.ttF",//simyou.ttF在本地目录

  下去拷贝c:

/windows/Fonts/simyou.ttF

  //baseFont.identity_h,baseFont.not_embedded);

  //FontFontchinese=newFont(bfchinese,12,Font.noRmal);

  Fontfont1=FontFactory.getFont(FontFactory.couRieR,8,Font.bold,basecolor.black);

  Fontfont2=FontFactory.getFont(FontFactory.couRieR,8,

  Font.noRmal,basecolor.black);

  Fontfont3=FontFactory.getFont(FontFactory.couRieR,9,

  Font.noRmal,basecolor.gRay);

  Fontfont5=FontFactory.getFont(FontFactory.couRieR,8,

  Font.noRmal,basecolor.black);

  //设置页脚

  lineseparatorline=new

  lineseparator(0.5f,1.3f,basecolor.cyan,element.align_centeR,0);//直线

  chunkc=newchunk(line);//line不能直接放入paragraph或phrase中

  columntext.showtextaligned(writer.getdirectcontent(),

  置

  1,newparagraph(string.format("notes"),font3),55,55,0);//通过坐标控制显示位columntext.showtextaligned(writer.getdirectcontent(),element.align_centeR,newparagraph(c),300,50,0);

  //设置页码

  //columntext.showtextaligned(writer.getdirectcontent(),

  //element.align_centeR,newphrase(string.format("page%d",

  //writer.getpagenumber())),300,50,0);

  //document.newpage();

  pdfptablet=newpdfptable

(2);//创建表格

  t.setwidthpercentage(100);//百分比

  t.setspacingbefore(20);

  //表格元素1

  imageimage1=image.getinstance("1.jpg");

  image1.scaleabsolute(170f,100f);//宽、高

  pdfpcellc1=newpdfpcell();

  c1.addelement(image1);

  paragraph[]parr=newparagraph[16];

  parr[0]=newparagraph("zycooeu",font1);

  parr[1]=newparagraph("ls21armstronghouse,Firstavenue",font2);

  parr[2]=newparagraph("doncastersouthyorkshiredn93ga",font2);

  parr[3]=newparagraph("unitedkingdom",font2);

  parr[4]=newparagraph("Vatgb2065852514",font2);

  for(inti=0;i  c1.addelement(parr[i]);

  }

  c1.setborder(0);//设置单元格边框的无颜色

  t.addcell(c1);

  //表格元素2

  imageimage2=image.getinstance("2.jpg");

  image2.scaleabsolute(110f,110f);//宽、高,值为像素pdfpcellc2=newpdfpcell();c2.addelement(image2);//设置图像对齐方式c2.setborder(0);//t.addcell(c2);//表格元素3parr[5]=newparagraph("billto",font2);parr[6]=newparagraph("atime2changet/aVoip.pt",font2);parr[7]=newparagraph("Ruaconsiglieripedroso,123(iberfar)",font2);parr[8]=newparagraph("edificioh",font2);pdfpcellc3=newpdfpcell();c3.addelement(newparagraph(""));for(inti=5;i  t.addcell(c7);//元素8pdfpcellc8=newpdfpcell();

  pdfptablett=newpdfptable

(2);//向表格单项中镶嵌表格tt.getdefaultcell().setborder(0);

  tt.addcell("");tt.addcell("");

  tt.addcell("");tt.addcell("");

  paragraph[]pars=newparagraph[10];

  pdfpcell[]cells=newpdfpcell[10];

  pars[0]=newparagraph("invoicedate:

",font2);pars[1]=newparagraph("09/03/20xx",font2);

  pars[2]=newparagraph("terms:

",font2);

  pars[3]=newparagraph("dueonReceipt",font2);pars[4]=newparagraph("duedate:

",font2);

  pars[5]=newparagraph("09/03/20xx",font2);

  pars[6]=newparagraph("p.o.#:

",font2);

  pars[7]=newparagraph("euVat-pt507441338",font2);pars[8]=newparagraph("euVatno.:

",font2);

  pars[9]=newparagraph("pt507441338",font2);

  for(inti=0;i  }pars[i].setalignment

(2);//设置该段落为靠右显示cells[i]=newpdfpcell();cells[i].setborder(0);//cell必须有,再addelement添加才能设置对齐方式//cell1.setphrase(para);是错的cells[i].addelement(pars[i]);tt.addcell(cells[i]);tt.setwidthpercentage(100);//百分比c8.addelement(tt);c8.setVerticalalignment(element.align_Right);c8.setRowspan

(2);//向下合并单元格c8.setborder(0);t.addcell(c8);/

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 总结汇报 > 学习总结

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2