zoukankan      html  css  js  c++  java
  • IText简介及示例

    一、iText简介

       iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。 

            使用iText非常方便,引入jar包,程序中就可以使用iText类库了。iText.jar包下载地址:http://www.itextpdf.com/download.php

            如果生成的PDF文件中需要出现中文、日文、韩文字符,则同样的地址,下载extrajars-2.3.zip扩展包,里面包括itext-asian.jar等扩展工具包。

    二、功能介绍

             在企业的信息系统中,报表处理一直占比较重要的作用,iText组件通过在服务器端使用Jsp 或JavaBean生成PDF报表,客户端采用超级连接显示或下载得到生成的报表,这样就很好的解决了B/S系统的报表处理问题。

    适合使用IText的需求:

            Typically, iText is used in projects that have one ofthe following requirements:

            The content isn't available in advance: it'scalculated based on user input or real-time database information.

            The PDF files can't be produced manually due to the massivevolume of content: a large number of pages or documents.

            Documents need to be created in unattended mode, in abatch process.

            The content needs to be customized or personalized;for instance, the name of the end user has to be stamped on a number of pages.

            Often you'll encounter these requirements in webapplications, where content needs to be served dynamically to a browser.Normally, you'd serve this information in the form of HTML, but for somedocuments, PDF is preferred over HTML for better printing quality, foridentical presentation on a variety of platforms, for security reasons, or toreduce the file size.

            通常,iText用于具有下列条件之一的项目:

            内容不固定,它是基于用户输入或实时数据库信息计算。

            由于页数多或者文件较大而造成的内容过多而使得PDF文件不能手动生成,。

            文件需要在无人值守模式下创建的,使用批处理过程。

            内容需要自定义或个性化;例如,最终用户的名字需要被印在某一页中。

            通常你会在Web应用程序中遇到的这些要求,其中的内容对于浏览者来说必须是动态。通常,你会以HTML的形式提供这些信息,但对于一些文档,PDF格式在印刷质量上是优于HTML的,同样,在各种平台上,出于安全原因,或减少文件大小的考虑,PDF都优于HTML。

    三、demo演示,一个最简单的使用IText转化为PDF的例子

           用iText生成PDF文档需要5个步骤: 

      ①建立com.itextpdf.text.Document对象的实例。

            Document document= new Document(); 

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

            PDFWriter.getInstance(document,new FileOutputStream("ITextTest.pdf")); 

      ③打开文档。

            document.open(); 

      ④向文档中添加内容。

            document.add(newParagraph("IText  Test")); 

      ⑤关闭文档。

            document.close(); 

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

            具体代码如下:

         

     1 package com.wh;  
     2    
     3 importjava.io.FileOutputStream;  
     4 importcom.itextpdf.text.BaseColor;  
     5 importcom.itextpdf.text.Document;  
     6 importcom.itextpdf.text.Element;  
     7 importcom.itextpdf.text.Font;  
     8 importcom.itextpdf.text.Paragraph;  
     9 importcom.itextpdf.text.Rectangle;  
    10 importcom.itextpdf.text.pdf.BaseFont;  
    11 importcom.itextpdf.text.pdf.PdfPTable;  
    12 importcom.itextpdf.text.pdf.PdfWriter;  
    13    
    14 public class ToPDF{  
    15        // 表头  
    16        public static final String[] tableHeader= { "姓名", "性别", "年龄",  
    17                      "学院", "专业", "年级"};  
    18    
    19        // 数据表字段数  
    20        private static final int colNumber = 6;  
    21    
    22        // 表格的设置  
    23        private static final int spacing = 2;  
    24    
    25        // 表格的设置  
    26        private static final int padding = 2;  
    27    
    28        // 导出Pdf文挡  
    29        public static void exportPdfDocument() {  
    30               // 创建文Pdf文挡50, 50, 50,50左右上下距离  
    31               Document document = newDocument(new Rectangle(1500, 2000), 50, 50, 50,  
    32                             50);  
    33               try {  
    34                      //使用PDFWriter进行写文件操作  
    35                      PdfWriter.getInstance(document,new FileOutputStream(  
    36                                    "d:\学生信息.pdf"));  
    37                      document.open();  
    38                      // 中文字体  
    39                      BaseFont bfChinese =BaseFont.createFont("STSong-Light",  
    40                                    "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);  
    41                      Font fontChinese = newFont(bfChinese, 12, Font.NORMAL);  
    42                      // 创建有colNumber(6)列的表格  
    43                      PdfPTable datatable = newPdfPTable(colNumber);  
    44                      //定义表格的宽度  
    45                      int[] cellsWidth = { 8, 2,2, 8, 5, 3 };  
    46                      datatable.setWidths(cellsWidth);  
    47                      // 表格的宽度百分比  
    48                      datatable.setWidthPercentage(100);  
    49                      datatable.getDefaultCell().setPadding(padding);  
    50                      datatable.getDefaultCell().setBorderWidth(spacing);  
    51                      //设置表格的底色  
    52                      datatable.getDefaultCell().setBackgroundColor(BaseColor.GREEN);  
    53                      datatable.getDefaultCell().setHorizontalAlignment(  
    54                                    Element.ALIGN_CENTER);  
    55                      // 添加表头元素  
    56                      for (int i = 0; i <colNumber; i++) {  
    57                             datatable.addCell(newParagraph(tableHeader[i], fontChinese));  
    58                      }  
    59                      // 添加子元素  
    60                      for (int i = 0; i <colNumber; i++) {  
    61                             datatable.addCell(newParagraph(tableHeader[i], fontChinese));  
    62                      }  
    63                      document.add(datatable);  
    64               } catch (Exception e) {  
    65                      e.printStackTrace();  
    66               }  
    67               document.close();  
    68        }  
    69    
    70        public static void main(String[] args)throws Exception {  
    71               exportPdfDocument();  
    72        }  
    73    
    74 }  

    下载示例代码及jar包请点击: 下载

  • 相关阅读:
    poj 2777 Count Color
    poj 3264 Balanced Lineup
    hdu 1754 I hate it
    poj 3468 A Simple Problem with Integers
    noip 2013 提高组 Day2 部分题解
    [c/c++]指针(2)
    [c/c++]指针(1)
    vijos 1360 八数码问题
    [复习]快速幂算法
    noip 2013 提高组 day1
  • 原文地址:https://www.cnblogs.com/jiliunyongjin/p/7530522.html
Copyright © 2011-2022 走看看