itext临时文件|itext-217js2jar是什么文件

|

㈠ itext-2.1.7.js2.jar是什么文件

jasper report团队给itext打的一个补丁参考这里,第二条

Hi,

We have patched the iText 2.1.7 to solve a transparency bug. We've published the patched iText version on our public Maven repository at:http://jasperreports.sourceforge.net/maven2/com/lowagie/itext/2.1.7.js1/

If you look at the pom.xml in JR, you can see we refernce this repository there.

What did you try to do and did not work? What was the error?

Thanks,Teodor

可以看到后续还有 js2 js3 js5 等版本,都是jasper report 对itext打的补丁包

㈡ java 如何给pdf文件加水印

Java生成PDF 加密 水印1、iText简介iText是一个开放源码的Java类库,可以用来方便地生成PDF文件。大家通过访问http://sourceforge.net/project/showfiles.php?group_id=15255&release_id=167948 下载最新版本的类库,下载完成之后会得到一个.jar包,把这个包加入JDK的classpath即可使用。如果生成的PDF文件中需要出现中文、日文、韩文字符,则还需要通过访问http://itext.sourceforge.net/downloads/iTextAsian.jar 下载iTextAsian.jar包。关于iText类库的使用,http://www.lowagie.com/iText/tutorial/index.html 有比较详细的教程。该教程从入门开始,比较系统地介绍了在PDF文件中放入文字、图片、表格等的方法和技巧。读完这片教程,大致就可以做一些从简单到复杂的PDF文件了。不过,试图通过教程解决在生成PDF文件过程中遇到的所有困难无疑是一种奢望。所以,阅读iText的api文档显得非常重要。读者在下载类库的同时,也可以下载类库的文档。注:如果以上两个下载链接无法下载而且通过网络也找不到这个jar包的同志可以留下邮箱地址,我会在两个工作日之内发邮件过去。以下部分我是我调试通过的源代码,提供大家参考:import java.awt.*;import java.io.*;import com.lowagie.text.*;import com.lowagie.text.Font;import com.lowagie.text.Rectangle;import com.lowagie.text.pdf.*;/*** 最近的项目中使用Itext将txt文件转换为PDF文件, 并且实现对文件的一些权限控制。 现实对pdf文件加*密,添加水印等。 */public class PDFConvertBL{ // txt原始文件的路径 private static final String txtFilePath = "d:/11.txt"; // 生成的pdf文件路径 private static final String pdfFilePath = "d:/22.pdf"; // 添加水印图片路径 // private static final String imageFilePath = "D:/33.jpg"; // 生成临时文件前缀 private static final String prefix = "tempFile"; // 所有者密码 private static final String OWNERPASSword = "12345678"; /** * txt文件转换为pdf文件 * * @param txtFile txt文件路径 * @param pdfFile pdf文件路径 * @param userPassWord 用户密码 * @param waterMarkName 水印内容 * @param permission 操作权限 */ public static void generatePDFWithTxt(String txtFile, String pdfFile, String userPassWord, String waterMarkName, int permission) { try { // 生成临时文件 File file = File.createTempFile(prefix, ".pdf"); // 创建pdf文件到临时文件 if (createPDFFile(txtFile, file)) { // 增加水印和加密 waterMark(file.getPath(), pdfFile, userPassWord, OWNERPASSWORD, waterMarkName, permission); } } catch (Exception e) { e.printStackTrace(); } } /** * 创建PDF文档 * * @param txtFilePath txt文件路径(源文件) * @param pdfFilePath pdf文件路径(新文件) */ private static boolean createPDFFile(String txtFilePath, File file) { // 设置纸张 Rectangle rect = new Rectangle(PageSize.A4); // 设置页码 HeaderFooter footer = new HeaderFooter(new Phrase("页码:", setChineseFont()), true); footer.setBorder(Rectangle.NO_BORDER); // step1 Document doc = new Document(rect, 50, 50, 50, 50); doc.setFooter(footer); try { FileReader fileRead = new FileReader(txtFilePath); BufferedReader read = new BufferedReader(fileRead); // 设置pdf文件生成路径 step2 PdfWriter.getInstance(doc, new FileOutputStream(file)); // 打开pdf文件 step3 doc.open(); // 实例化Paragraph 获取写入pdf文件的内容,调用支持中文的方法. step4 while (read.ready()) { // 添加内容到pdf(这里将会按照txt文件的原始样式输出) doc.add(new Paragraph(read.readLine(), setChineseFont())); } // 关闭pdf文件 step5 doc.close(); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 在pdf文件中添加水印 * * @param inputFile 原始文件 * @param outputFile 水印输出文件 * @param waterMarkName 水印名字 */ private static void waterMark(String inputFile, String outputFile, String userPassWord, String ownerPassWord, String waterMarkName, int permission) { try { PdfReader reader = new PdfReader(inputFile); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile)); // 设置密码 stamper.setEncryption(userPassWord.getBytes(), ownerPassWord.getBytes(), permission, false); BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); int total = reader.getNumberOfPages() + 1; // Image image = Image.getInstance(imageFilePath); // image.setAbsolutePosition(200, 400); PdfContentByte under; int j = waterMarkName.length(); char c = 0; int rise = 0; for (int i = 1; i < total; i++) { rise = 500; under = stamper.getUnderContent(i); // 添加图片 // under.addImage(image); under.beginText(); under.setColorFill(Color.CYAN); under.setFontAndSize(base, 30); // 设置水印文字字体倾斜 开始 if (j >= 15) { under.setTextMatrix(200, 120); for (int k = 0; k < j; k++) { under.setTextRise(rise); c = waterMarkName.charAt(k); under.showText(c + ""); rise -= 20; } } else { under.setTextMatrix(180, 100); for (int k = 0; k < j; k++) { under.setTextRise(rise); c = waterMarkName.charAt(k); under.showText(c + ""); rise -= 18; } } // 字体设置结束 under.endText(); // 画一个圆 // under.ellipse(250, 450, 350, 550); // under.setLineWidth(1f); // under.stroke(); } stamper.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 设置中文 * * @return Font */ private static Font setChineseFont() { BaseFont base = null; Font fontChinese = null; try { base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED); fontChinese = new Font(base, 12, Font.NORMAL); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return fontChinese; } public static void main(String[] args) { generatePDFWithTxt(txtFilePath, pdfFilePath, "123", "水印文字", 16); }}文章参考一些网络博客稍加修改调试,特此申明http://hi..com/sx_python/item/15081531ad7d1bc21b96965e

㈢ itext 生成 PDF(一)

itext 生成 PDF(二) 官网: http://itextsupport.com/apidocs/itext5/latest/ 博文: https://blog.csdn.net/u010154380/article/details/78087663 博文: https://blog.csdn.net/u013129932/article/details/43889705 iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。 项目要使用iText,必须引入jar包。才能使用,maven依赖如下: <dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.10</version></dependency> 输出中文,还要引入下面itext-asian.jar包: <dependency><groupId>com.itextpdf</groupId><artifactId>itext-asian</artifactId><version>5.2.0</version></dependency> 设置pdf文件密码,还要引入下面bcprov-jdk15on.jar包: <dependency><groupId>org.bouncycastle</groupId><artifactId>bcprov-jdk15on</artifactId><version>1.54</version></dependency> iText常用类 com.itextpdf.text.Document:这是iText库中最常用的类,它代表了一个pdf实例。如果你需要从零开始生成一个PDF文件,你需要使用这个Document类。首先创建(new)该实例,然后打开(open)它,并添加(add)内容,最后关闭(close)该实例,即可生成一个pdf文件。 com.itextpdf.text.Paragraph:表示一个缩进的文本段落,在段落中,你可以设置对齐方式,缩进,段落前后间隔等 com.itextpdf.text.Chapter:表示PDF的一个章节,他通过一个Paragraph类型的标题和整形章数创建 com.itextpdf.text.Font:这个类包含了所有规范好的字体,包括family of font,大小,样式和颜色,所有这些字体都被声明为静态常量 com.itextpdf.text.List:表示一个列表; com.itextpdf.text.Anchor:表示一乱返磨个锚,类似于HTML页面的链接。 com.itextpdf.text.pdf.PdfWriter:当这个PdfWriter被添加到PdfDocument后,所有添哗斗加世铅到Document的内容将会写入到与文件或网络关联的输出流中。 com.itextpdf.text.pdf.PdfReader:用于读取PDF文件; iText使用 创建一个简单的pdf文件,如下: packagecom.hd.pdf;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importcom.itextpdf.text.Document;importcom.itextpdf.text.DocumentException;importcom.itextpdf.text.Paragraph;importcom.itextpdf.text.pdf.PdfWriter;publicclassTestPDFDemo1{publicstaticvoidmain(String[]args)throws FileNotFoundException,DocumentException{// 1.新建document对象Document document=newDocument();// 2.建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。// 创建 PdfWriter 对象 第一个参数是对文档对象的引用,第二个参数是文件的实际名称,在该名称中还会给出其输出路径。PdfWriter writer=PdfWriter.getInstance(document,newFileOutputStream("C:/Users/H__D/Desktop/test.pdf"));// 3.打开文档document.open();// 4.添加一个内容段落document.add(newParagraph("Hello World!"));// 5.关闭文档document.close();}} 打开文件 851491-20161209165247147-746087588.png PDF中创建表格 publicstaticvoidmain(String[]args)throws DocumentException,FileNotFoundException{//创建文件Document document=newDocument();//建立一个书写器PdfWriter writer=PdfWriter.getInstance(document,newFileOutputStream("C:/Users/H__D/Desktop/test4.pdf"));//打开文件document.open();//添加内容document.add(newParagraph("HD content here"));// 3列的表.PdfPTable table=newPdfPTable(3);table.setWidthPercentage(100);// 宽度100%填充table.setSpacingBefore(10f);// 前间距table.setSpacingAfter(10f);// 后间距List<PdfPRow>listRow=table.getRows();//设置列宽float[]columnWidths={1f,2f,3f};table.setWidths(columnWidths);//行1PdfPCell cells1[]=newPdfPCell[3];PdfPRow row1=newPdfPRow(cells1);//单元格cells1[0]=newPdfPCell(newParagraph("111"));//单元格内容cells1[0].setBorderColor(BaseColor.BLUE);//边框验证cells1[0].setPaddingLeft(20);//左填充20cells1[0].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中cells1[0].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中cells1[1]=newPdfPCell(newParagraph("222"));cells1[2]=newPdfPCell(newParagraph("333"));//行2PdfPCell cells2[]=newPdfPCell[3];PdfPRow row2=newPdfPRow(cells2);cells2[0]=newPdfPCell(newParagraph("444"));//把第一行添加到集合listRow.add(row1);listRow.add(row2);//把表格添加到文件中document.add(table);//关闭文档document.close();//关闭书写器writer.close();} 打开图片 851491-20161209165247147-746087588.png 给PDF文件设置文件属性,例如: publicstaticvoidmain(String[]args)throws FileNotFoundException,DocumentException{//创建文件Document document=newDocument();//建立一个书写器PdfWriter writer=PdfWriter.getInstance(document,newFileOutputStream("C:/Users/H__D/Desktop/test2.pdf"));//打开文件document.open();//添加内容document.add(newParagraph("Some content here"));//设置属性//标题document.addTitle("this is a title");//作者document.addAuthor("H__D");//主题document.addSubject("this is subject");//关键字document.addKeywords("Keywords");//创建时间document.addCreationDate();//应用程序document.addCreator("hd.com");//关闭文档document.close();//关闭书写器writer.close();} 打开文件 851491-20161209165247147-746087588.png PDF中添加图片 publicstaticvoidmain(String[]args)throws DocumentException,IOException{//创建文件Document document=newDocument();//建立一个书写器PdfWriter writer=PdfWriter.getInstance(document,newFileOutputStream("C:/Users/H__D/Desktop/test3.pdf"));//打开文件document.open();//添加内容document.add(newParagraph("HD content here"));//图片1Image image1=Image.getInstance("C:/Users/H__D/Desktop/IMG_0109.JPG");//设置图片位置的x轴和y周image1.setAbsolutePosition(100f,550f);//设置图片的宽度和高度image1.scaleAbsolute(200,200);//将图片1添加到pdf文件中document.add(image1);//图片2Image image2=Image.getInstance(newURL("http://static.cnblogs.com/images/adminlogo.gif"));//将图片2添加到pdf文件中document.add(image2);//关闭文档document.close();//关闭书写器writer.close();} 打开图片 851491-20161209165247147-746087588.png PDF中创建列表 publicstaticvoidmain(String[]args)throws DocumentException,FileNotFoundException{//创建文件Document document=newDocument();//建立一个书写器PdfWriter writer=PdfWriter.getInstance(document,newFileOutputStream("C:/Users/H__D/Desktop/test5.pdf"));//打开文件document.open();//添加内容document.add(newParagraph("HD content here"));//添加有序列表List orderedList=newList(List.ORDERED);orderedList.add(newListItem("Item one"));orderedList.add(newListItem("Item two"));orderedList.add(newListItem("Item three"));document.add(orderedList);//关闭文档document.close();//关闭书写器writer.close();} 打开文件 851491-20161209180029726-1168732515.png PDF中设置样式/格式化输出,输出中文内容,必须引入itext-asian.jar publicstaticvoidmain(String[]args)throws DocumentException,IOException{//创建文件Document document=newDocument();//建立一个书写器PdfWriter writer=PdfWriter.getInstance(document,newFileOutputStream("C:/Users/H__D/Desktop/test6.pdf"));//打开文件document.open();//中文字体,解决中文不能显示问题BaseFont bfChinese=BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);//蓝色字体Font blueFont=newFont(bfChinese);blueFont.setColor(BaseColor.BLUE);//段落文本Paragraph paragraphBlue=newParagraph("paragraphOne blue front",blueFont);document.add(paragraphBlue);//绿色字体Font greenFont=newFont(bfChinese);greenFont.setColor(BaseColor.GREEN);//创建章节Paragraph chapterTitle=newParagraph("段落标题xxxx",greenFont);Chapter chapter1=newChapter(chapterTitle,1);chapter1.setNumberDepth(0);Paragraph sectionTitle=newParagraph("部分标题",greenFont);Section section1=chapter1.addSection(sectionTitle);Paragraph sectionContent=newParagraph("部分内容",blueFont);section1.add(sectionContent);//将章节添加到文章中document.add(chapter1);//关闭文档document.close();//关闭书写器writer.close();} 打开图片 ![ 851491-20161209180029726-1168732515.png ] 851491-20161209165247147-746087588.png 给PDF文件设置密码,需要引入bcprov-jdk15on.jar包: publicstaticvoidmain(String[]args)throws DocumentException,IOException{// 创建文件Document document=newDocument();// 建立一个书写器PdfWriter writer=PdfWriter.getInstance(document,newFileOutputStream("C:/Users/H__D/Desktop/test8.pdf"));//用户密码String userPassword="123456";//拥有者密码String ownerPassword="hd";writer.setEncryption(userPassword.getBytes(),ownerPassword.getBytes(),PdfWriter.ALLOW_PRINTING,PdfWriter.ENCRYPTION_AES_128);// 打开文件document.open();//添加内容document.add(newParagraph("password !!!!"));// 关闭文档document.close();// 关闭书写器writer.close();} 打开图片 851491-20161209165247147-746087588.png 给PDF文件设置权限 publicstaticvoidmain(String[]args)throws DocumentException,IOException{// 创建文件Document document=newDocument();// 建立一个书写器PdfWriter writer=PdfWriter.getInstance(document,newFileOutputStream("C:/Users/H__D/Desktop/test9.pdf"));// 只读权限writer.setEncryption("".getBytes(),"".getBytes(),PdfWriter.ALLOW_PRINTING,PdfWriter.ENCRYPTION_AES_128);// 打开文件document.open();// 添加内容document.add(newParagraph("password !!!!"));// 关闭文档document.close();// 关闭书写器writer.close();} 读取/修改已有的PDF文件 publicstaticvoidmain(String[]args)throwsDocumentException,IOException{//读取pdf文件PdfReaderpdfReader=newPdfReader("C:/Users/H__D/Desktop/test1.pdf");//修改器PdfStamperpdfStamper=newPdfStamper(pdfReader,newFileOutputStream("C:/Users/H__D/Desktop/test10.pdf"));Imageimage=Image.getInstance("C:/Users/H__D/Desktop/IMG_0109.JPG");image.scaleAbsolute(50,50);image.setAbsolutePosition(0,700);for(inti=1;i<=pdfReader.getNumberOfPages();i++){PdfContentBytecontent=pdfStamper.getUnderContent(i);content.addImage(image);}pdfStamper.close();} itext  生成 PDF(二) 链接:https://www.jianshu.com/p/20d4905383b4

㈣ 怎样用iText读取pdf文件

用iText读取pdf文件举例:(1)在Eclipse中新建一个Java工程。(2)下载相应的iText-5.0.2.jar并放到对应的lib目录下。在工程坦宏枝中创建包并创建测试类,该类包含一个inspect方法用于从一个PDF中获取文本,它接受两个参数,分别是PDF文件路径和输出流,指定要提取的PDF文件的路径和读取PDF所用的输出流,比如:PDF路径为E://text.pdf。然后调用iText提供的PdfReader类和PdfTextExtractor类,将PDF格式的文本提取出来并写入txt文件中。部分代码如下:import java.io.FileOutputStream;import java.io.IOException;import java.io.PrintWriter;import com.itextpdf.text.DocumentException;import com.itextpdf.text.pdf.PdfReader;import com.itextpdf.text.pdf.parser.PdfTextExtractor;public class PDF { /** The resulting text file with info about a PDF. */ public static final String RESULT = "d:/ceshi.txt";//存放由pdf转换成txt文件的路径。 /** * Main method. * @param args no arguments needed * @throws DocumentException * @throws IOException */ public static void main(String[] args) throws DocumentException, IOException { PrintWriter writer = new PrintWriter(new FileOutputStream(RESULT));//txt文件写入流 String string = "E:/text.pdf";//pdf文件路径 inspect(writer,string); //调用读取方法 writer.close(); } /** * Inspect a PDF file and write the info to a txt file * @param writer Writer to a text file * @param filename Path to the PDF file * @throws IOException */ public static void inspect(PrintWriter writer, String filename) throws IOException { PdfReader reader = new PdfReader(filename); //读取pdf所使用的输让敏出流 int num = reader.getNumberOfPages();//获得页数 String content = ""; //存放读取出的绝携文档内容 for (int i = 1; i < num; i++) { content += PdfTextExtractor.getTextFromPage(reader, i); //读取第i页的文档内容 } writer.write(content);//写入文件内容 writer.flush(); }}

㈤ 怎样利用iText把生成出的PDF变成压缩包

一、iText介绍iText是着名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。iText的安装非常方便,在http://www.lowagie.com/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,intmarginBottom);构建函数的参数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)publicbooleanaddProcer()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("");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");运行结果如下:header1.12.11.22.2celltest1bigcellcelltest2代码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中通过函数publicvoidsetAlignment(intalignment)进行处理,参数alignment为Image.RIGHT、Image.MIDDLE、Image.LEFT分别指右对齐、居中、左对齐;当参数alignment为Image.TEXTWRAP、Image.UNDERLYING分别指文字绕图形显示、图形作为文字的背景显示。这两种参数可以结合以达到预期的效果,如setAlignment(Image.RIGHT|Image.TEXTWRAP)显示的效果为图像右对齐,文字围绕图像显示。图像的尺寸和旋转如果图像在文档中不按原尺寸显示,可以通过下面的函数进行设定:publicvoidscaleAbsolute(intnewWidth,intnewHeight)publicvoidscalePercent(intpercent)publicvoidscalePercent(intpercentX,intpercentY)函数publicvoidscaleAbsolute(intnewWidth,intnewHeight)直接设定显示尺寸;函数publicvoidscalePercent(intpercent)设定显示比例,如scalePercent(50)表示显示的大小为原尺寸的50%;而函数scalePercent(intpercentX,intpercentY)则图像高宽的显示比例。如果图像需要旋转一定角度之后在文档中显示,可以通过函数publicvoidsetRotation(doubler)设定,参数r为弧度,如果旋转角度为30度,则参数r=Math.PI/6。六、中文处理默认的iText字体设置不支持中文字体,需要下载远东字体包iTextAsian.jar,否则不能往PDF文档中输出中文字体。通过下面的代码就可以在文档中使用中文了:BaseFontbfChinese=BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);com.lowagie.text.FontFontChinese=newcom.lowagie.text.Font(bfChinese,12,com.lowagie.text.Font.NORMAL);Paragraphpragraph=newParagraph("你好",FontChinese);小结iText还有很多高级的功能,这里就不一一介绍了,具体开发时可参考发布的文档。总的来说,iText是一套java环境下不错的制作PDF的组件。因为iText支持jsp/javabean下的开发,这使得B/S应用中的报表问题能得到很好的解决。由于iText毕竟不是专门为制作报表设计,所有报表中的内容、格式都需要通过写代码实现,相对于那些专业的支持可视化设计的报表软件来说,编程的工作量就有一定程度的增加。


赞 (0)