java导出pdf格式的文件.docx
《java导出pdf格式的文件.docx》由会员分享,可在线阅读,更多相关《java导出pdf格式的文件.docx(18页珍藏版)》请在三一办公上搜索。
1、java导出pdf格式的文件public static void main(String args) Document doc = new Document; PdfWriter.getInstance(doc, new FileOutputStream(E:/HelloWorld.pdf); doc.open; doc.add(new Paragraph(Hello World); doc.close; Document doc = new Document (PageSize.A4); PdfWriter.getInstance (doc, new FileOutputStream (c:
2、/test/pdf/test.pdf); doc.open ; /标题字体 BaseFont bfTitle = BaseFont.createFont(STSong-Light, UniGB-UCS2-H, BaseFont.NOT_EMBEDDED); Font titleFont = new Font(bfTitle, 18, Font.NORMAL); /内容字体 BaseFont bfComic = BaseFont.createFont(STSong-Light, UniGB-UCS2-H, BaseFont.NOT_EMBEDDED); Font font = new Font(
3、bfComic, 9, Font.NORMAL); Paragraph titleP=new Paragraph(儿童信息 Child Informationnn,titleFont); titleP.setAlignment(titleP.ALIGN_CENTER); doc.add(titleP); /生成4列的表格 PdfPTable table = new PdfPTable (4); table.setWidthPercentage(100); table.setWidthPercentage(100); table.addCell (new Paragraph (Children-
4、id,font); PdfPCell cell = new PdfPCell (new Paragraph (0914*,font); cell.setColspan (3); table.addCell (cell); / 添加第一行 table.addCell (new Paragraph (Name(CN),font); table.addCell (new Paragraph (党宁生,font); table.addCell (new Paragraph (Name(EN),font); table.addCell (new Paragraph (DANG NING SHENG,fo
5、nt); /添加第二行 table.addCell (new Paragraph (Sex(CN),font); table.addCell (new Paragraph (男,font); table.addCell (new Paragraph (Sex(EN),font); table.addCell (new Paragraph (MALE,font); /添加第8行 table.addCell (new Paragraph (Note,font); cell = new PdfPCell (new Paragraph (儿童资料,font); cell.setColspan (3);
6、 table.addCell (cell); /添加第9行 table.addCell (new Paragraph (Pictures,font); Image photo=Image.getInstance(c:/test/pdf/1246588678828.jpg); cell = new PdfPCell (photo); cell.setColspan (3); table.addCell (cell); for(PdfPRow row:(ArrayList<PdfPRow>)table.getRows) for(PdfPCell cells:row.getCells) if
7、(cells!=null) cells.setPadding(10.0f); doc.add (table); doc.newPage; /插入图片 doc.newPage; Image image1 = Image.getInstance (c:/test/pdf/1246588315281.jpg); image1.setAlignment(image1.ALIGN_CENTER); image1.scaleToFit( PageSize.A4.getHeight,PageSize.A4.getWidth); doc.add (image1); doc.close ; iText是一个能够
8、快速产生PDF文件的java类库。iText的java类对于那些要产生包含文本,表格,图形的只读文档是很有用的。它的类库尤其与java Servlet有很好的给合。使用iText与PDF能够使你正确的控制Servlet的输出。 一、前言在企业的信息系统中,报表处理一直占比较重要的作用,Java组件iText可以非常快速的生成PDF报表。iText是着名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、 Html文件转化为PDF文件。通过在服务器端使用Jsp或 JavaBean生成PDF报表,
9、客户端采用超级连接显示或下载得到生成的报表,这样就很好的解决了B/S系统的报表处理问题了。二、iText简介iText是着名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。iText的安装非常方便,在 - download 网站上下载iText.jar文件后,只需要在系统的CLASSPATH中加入iText.jar的路径,在程序中就可以使用iText类库了。三、建立第一个PDF文档用iText生成PDF文档需要5个步骤:建立com.lowagie.text.D
10、ocument对象的实例。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的文件,文件内容为Hell
11、o World。建立com.lowagie.text.Document对象的实例com.lowagie.text.Document对象的构建函数有三个,分别是:public Document;public Document(Rectangle pageSize);public Document(Rectangle pageSize,int marginLeft,int marginRight,int marginTop,int marginBottom);构建函数的参数pageSize是文档页面的大小,对于第一个构建函数,页面的大小为A4,同Document(PageSize.A4)的效 果一
12、样;对于第三个构建函数,参数marginLeft、marginRight、marginTop、marginBottom分别为左、右、上、下的 页边距。通过参数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可以将页面设置成横向。书写器对象一旦文
13、档(document)对象建立好之后,需要建立一个或多个书写器(Writer)对象与之关联。通过书写器(Writer)对象可以将 具体文档存盘成需要的格式,如com.lowagie.text.PDF.PDFWriter可以将文档存成PDF文件, com.lowagie.text.html.HtmlWriter可以将文档存成html文件。设定文档属性在文档打开之前,可以设定文档的标题、主题、作者、关键字、装订方式、创建者、生产者、创建日期等属性,调用的方法分别是:public boolean addTitle(String title)public boolean addSubject(Stri
14、ng subject)public boolean addKeywords(String keywords)public boolean addAuthor(String author)public boolean addCreator(String creator)public boolean addProducerpublic boolean addCreationDatepublic boolean addHeader(String name, String content)其中方法addHeader对于PDF文档无效,addHeader仅对html文档有效,用于添加文档的头信息。当新的
15、页面产生之前,可以设定页面的大小、书签、脚注等信息,调用的方法是:public boolean setPageSize(Rectangle pageSize)public boolean add(Watermark watermark)public void removeWatermarkpublic void setHeader(HeaderFooter header)public void resetHeaderpublic void setFooter(HeaderFooter footer)public void resetFooterpublic void resetPageCoun
16、tpublic void setPageCount(int pageN)如果要设定第一页的页面属性,这些方法必须在文档打开之前调用。对于PDF文档,iText还提供了文档的显示属性,通过调用书写器的setViewerPreferences方法可以控制文档打开时Acrobat Reader的显示属性,如是否单页显示、是否全屏显示、是否隐藏状态条等属性。另外,iText也提供了对PDF文件的安全保护,通过书写器的setEncryption方法,可以设定文档的用户口令、只读、可打印等属性。添加文档内容所有向文档添加的内容都是以对象为单位的,如Phrase、Paragraph、Table、Graphi
17、c对象等。比较常用的是段落(Paragraph)对象,用于向文档中添加一段文字。四、文本处理iText中用文本块(Chunk)、短语(Phrase)和段落(paragraph)处理文本。文本块(Chunk)是处理文本的最小单位,有一串带格式的字符串组成。如以下代码就是产生一个字体为HELVETICA、大小为10、带下划线的字符串:Chunk chunk1 = new Chunk(This text is underlined, FontFactory.getFont(FontFactory.HELVETICA, 12, Font.UNDERLINE);短语(Phrase)由一个或多个文本块(C
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 导出 pdf 格式 文件

链接地址:https://www.31ppt.com/p-3159576.html