一、前言 二、iText简介 iText的安装非常方便,在http://www./iText/download.html网站上下载iText.jar文件后,只需要在系统的CLASSPATH中加入iText.jar的路径,在程序中就可以使用iText类库了。 三、建立第一个PDF文档 ①建立com.lowagie.text.Document对象的实例。 Document document = new Document(); ②建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。 PDFWriter.getInstance(document, new FileOutputStream("Helloworld.PDF")); ③打开文档。 document.open(); ④向文档中添加内容。 document.add(new Paragraph("Hello World")); ⑤关闭文档。 document.close(); 通过上面的5个步骤,就能产生一个Helloworld.PDF的文件,文件内容为"Hello World"。 建立com.lowagie.text.Document对象的实例 通过参数pageSize可以设定页面大小、面背景色、以及页面横向/纵向等属性。iText定义了A0-A10、AL、LETTER、HALFLETTER、_11x17、LEDGER、NOTE、B0-B5、ARCH_A-ARCH_E、FLSA和FLSE等纸张类型,也可以通过Rectangle pageSize = new Rectangle(144, 720);自定义纸张。通过Rectangle方法rotate()可以将页面设置成横向。 书写器(Writer)对象 设定文档属性 public boolean addTitle(String title) public boolean addSubject(String subject) public boolean addKeywords(String keywords) public boolean addAuthor(String author) public boolean addCreator(String creator) public boolean addProducer() public boolean addCreationDate() public boolean addHeader(String name, String content) 其中方法addHeader对于PDF文档无效,addHeader仅对html文档有效,用于添加文档的头信息。 public boolean setPageSize(Rectangle pageSize) public boolean add(Watermark watermark) public void removeWatermark() public void setHeader(HeaderFooter header) public void resetHeader() public void setFooter(HeaderFooter footer) public void resetFooter() public void resetPageCount() public void setPageCount(int pageN) 如果要设定第一页的页面属性,这些方法必须在文档打开之前调用。 对于PDF文档,iText还提供了文档的显示属性,通过调用书写器的 setViewerPreferences方法可以控制文档打开时Acrobat Reader的显示属性,如是否单页显示、是否全屏显示、是否隐藏状态条等属性。 另外,iText也提供了对PDF文件的安全保护,通过书写器(Writer)的setEncryption方法,可以设定文档的用户口令、只读、可打印等属性。 添加文档内容 四、文本处理 Chunk chunk1 = new Chunk("This text is underlined",
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为左对齐,默认为左对齐。 五、表格处理 类com.lowagie.text.Table的构造函数有三个: Table(int columns) Table(int columns, int rows) Table(Properties attributes) 参数columns、rows、attributes分别为表格的列数、行数、表格属性。创建表格时必须指定表格的列数,而对于行数可以不用指定。 建立表格之后,可以设定表格的属性,如:边框宽度、边框颜色、衬距(padding space 即单元格之间的间距)大小等属性。 六、图像处理 Image gif = Image.getInstance("vonnegut.gif"); Image jpeg = Image.getInstance("myKids.jpg"); Image png = Image.getInstance("hitchcock.png"); 图像的位置 图像的尺寸和旋转 public void scaleAbsolute(int newWidth, int newHeight) public void scalePercent(int percent) public void scalePercent(int percentX, int percentY) 函数public void scaleAbsolute(int newWidth, int newHeight)直接设定显示尺寸; 七、中文处理 BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); com.lowagie.text.Font FontChinese = new com.lowagie.text.Font(bfChinese, 12, com.lowagie.text.Font.NORMAL); Paragraph pragraph=new Paragraph("你好", FontChinese); 八、后计 九、案例-导出pdf、word、图片 a、iText所需jar iText-2.1.7.jar iTextAsian.jar 支持中文字体必需的包,是itext-2.1.7的扩展包 itext-rtf-2.1.7.jar itext导出word必需的包,是itext-2.1.7的扩展包 b、例子 package junit.test; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.net.MalformedURLException; import org.junit.Test; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Font; import com.lowagie.text.Image; import com.lowagie.text.Paragraph; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.PdfWriter; import com.lowagie.text.rtf.RtfWriter2; /** * 导出图片、word、pdf * * @author 林计钦 * @version 1.0 Feb 7, 2014 9:02:38 AM */ public class PdfAndWordTest { /** * 导出pdf */ @Test public void exportPdf() { Document document=null; try { BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 设置中文字体 Font headFont = new Font(bfChinese, 10, Font.NORMAL);// 设置字体大小 //第一步:创建一个document对象。 document = new Document(); //第二步:创建一个PdfWriter实例,将文件输出流指向一个文件。 PdfWriter.getInstance(document, new FileOutputStream("D:/test/123.pdf")); //第三步:打开文档。 document.open(); Paragraph title = new Paragraph("你好,Pdf!", headFont); //第四步:在文档中增加一个段落。 document.add(title); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ if(document!=null){ //第五步:关闭文档。 document.close(); } } } /** * 导出word */ @Test public void exportWord() { Document document=null; try { document = new Document(); RtfWriter2.getInstance(document, new FileOutputStream("D:/test/word.doc")); document.open(); Paragraph title = new Paragraph("你好,Word!"); document.add(title); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); }finally{ if(document!=null){ document.close(); } } } /** * 导出图片 */ @Test public void exportImg() { Document document=null; try { BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 设置中文字体 Font headFont = new Font(bfChinese, 10, Font.NORMAL);// 设置字体大小 document = new Document(); PdfWriter.getInstance(document, new FileOutputStream("D:/test/img.pdf")); //设定文档的作者 document.addAuthor("林计钦"); //测试无效 document.open(); document.add(new Paragraph("你好,Img!", headFont)); //读取一个图片 Image image = Image.getInstance("D:/test/1.gif"); //插入一个图片 document.add(image); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ if(document!=null){ document.close(); } } } }
|
|