iText生成PDF

iText生成PDF【转载】

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

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

 ① 建立com.itextpdf.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"。 

import java.io.FileNotFoundException;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.io.OutputStream;  
import java.util.HashMap;  
import java.util.Map;  
  
import com.itextpdf.text.Document;  
import com.itextpdf.text.DocumentException;  
import com.itextpdf.text.Element;  
import com.itextpdf.text.Font;  
import com.itextpdf.text.Image;  
import com.itextpdf.text.PageSize;  
import com.itextpdf.text.Paragraph;  
import com.itextpdf.text.pdf.BaseFont;  
import com.itextpdf.text.pdf.PdfPCell;  
import com.itextpdf.text.pdf.PdfPTable;  
import com.itextpdf.text.pdf.PdfWriter;  
  
/** 
 * iText 学习 
 * Paragraph 缩进的段落 
 * Chapter PDF文档中的章节 
 * Font 字体规范 
 * List 一个列表,顺序包含许多ListItems 
 * Table 单元格的表 
 *  
 * @author Bing 
 * 
 */  
public class PDF4iText {  
    /** 
     *  
     * @param os  PDF输出的目录 
     *  
     * 以下参数是params所对应的Key-value 
     * @param imgPath Logo图片地址 
     *  
     * @param userName 领用人 
     * @param userDept 领用部门 
     * @param borrowTime 领用时间 
     * @param borrowRemark 领用事由 
     * @param borrowDate 领用日期 
     *  
     * @param assetName 固定资产名称 
     * @param assetModel 固定资产型号 
     * @param assetConfig 固定资产配置 
     * @param assetSequence 固定资产序列号 
     *  
     */  
    public static void createPDF(OutputStream os, Map<String, String> params) {  
  
        Document document = new Document(PageSize.A4, 50, 50, 100, 10);// 页面大小,左右上下页边距  
          
        try {  
            PdfWriter.getInstance(document, os);  
            // 添加PDF文档的一些信息  
            document.addTitle("Hello World Example"); // 标题  
            document.addSubject("This example explains how to add metadata."); // 主题  
            document.addAuthor("xuxb"); // 作者  
            document.addCreator("My program using iText"); // 创建者  
            document.addKeywords("iText");  // 关键字  
              
            BaseFont baseFont = BaseFont.createFont("C:\\Windows\\Fonts\\SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);  
//          BaseFont baseFont = BaseFont.createFont("C:\\Windows\\Fonts\\msyh.ttf",BaseFont.IDENTITY_H,BaseFont.EMBEDDED);  
//          BaseFont baseFont1 = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);  
            BaseFont baseFont1 = BaseFont.createFont("C:\\Windows\\Fonts\\SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);  
              
            Font simsun = new Font(baseFont, 12, Font.NORMAL);  
            Font italic = new Font(baseFont, 10, Font.ITALIC);  
            Font STSong = new Font(baseFont1, 10, Font.NORMAL);  
            Font bold = new Font(baseFont1, 12, Font.BOLD);  
              
            // 打开文档,将要写入内容  
            document.open();  
              
            float[] f = {40,60};  
            PdfPTable header = new PdfPTable(f);   
            header.setSpacingBefore(5f);  
            header.setWidthPercentage(100);  
            header.getDefaultCell().setBorder(0);  
//          LOGO KJLink  
            PdfPCell h1 = new PdfPCell(Image.getInstance(params.get("imgPath")));  
            h1.setBorder(0);  
            h1.setBorderWidthBottom(0.2f);  
            h1.setPaddingTop(25f);  
//          h1.setFixedHeight(100);  
            header.addCell(h1);  
            PdfPCell h2 = new PdfPCell(new Paragraph("固定资产领用单", italic));  
            h2.setBorder(0);  
            h2.setBorderWidthBottom(0.2f);  
            h2.setPaddingTop(20f);  
            h2.setHorizontalAlignment(Element.ALIGN_RIGHT);  
            header.addCell(h2);  
              
            document.add(header);  
              
            PdfPTable table = new PdfPTable(6); // Code 1  
            table.setWidthPercentage(100);  
            table.setSpacingBefore(10f);  
//          table.getDefaultCell().setBorder(0);  
            table.setHorizontalAlignment(Element.ALIGN_CENTER);// 水平居中  
              
//          第一行 row1  
            PdfPCell row1 = new PdfPCell(new Paragraph("KJLINK International Co. 固定资产领用单", bold));  
            row1.setColspan(6);  
//          cell0.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置内容水平居中  
            row1.setFixedHeight(30);  
            row1.setPadding(5);  
            row1.setHorizontalAlignment(Element.ALIGN_CENTER); // 垂直居中  
            table.addCell(row1);  
              
//           第二行 row2  
            PdfPCell hCell1 = new PdfPCell(new Paragraph("领用人", simsun));  
            hCell1.setHorizontalAlignment(Element.ALIGN_CENTER);  
            hCell1.setFixedHeight(25);  
            table.addCell(hCell1);  
            table.addCell(new Paragraph(params.get("userName"), STSong));  
  
            PdfPCell hCell2 = new PdfPCell(new Paragraph("领用部门", simsun));  
            hCell2.setHorizontalAlignment(Element.ALIGN_CENTER);  
            table.addCell(hCell2);  
            table.addCell(new Paragraph(params.get("userDept"), STSong));  
            PdfPCell hCell3 = new PdfPCell(new Paragraph("领用时间", simsun));  
            hCell3.setHorizontalAlignment(Element.ALIGN_CENTER);  
            table.addCell(hCell3);  
            table.addCell(new Paragraph(params.get("borrowTime"), STSong));  
              
//           第三行 row3  
            PdfPCell row3 = new PdfPCell(new Paragraph("领用事由:", STSong));  
            row3.setColspan(6);  
            row3.setFixedHeight(20);  
            row3.setPadding(5);  
            row3.setBorder(0);  
            row3.setBorderWidthLeft(0.2f);  
            row3.setBorderWidthRight(0.2f);  
//          row3.setBorderColorBottom(BaseColor.WHITE);  
            row3.setVerticalAlignment(Element.ALIGN_CENTER);  
            table.addCell(row3);  
              
//           第三行 row3  
            PdfPCell row3_2 = new PdfPCell(new Paragraph(params.get("borrowRemark"), STSong));  
            row3_2.setColspan(6);  
            row3_2.setBorder(0);  
            row3_2.setFixedHeight(80);  
            row3_2.setPaddingRight(10f);  
            row3_2.setBorderWidthLeft(0.2f);  
            row3_2.setBorderWidthRight(0.2f);  
//          row3_1.setBorderColorTop(BaseColor.WHITE);//ColorTop(BaseColor.RED);  
            row3_2.setHorizontalAlignment(Element.ALIGN_LEFT);  
            row3_2.setPaddingLeft(20f);  
            table.addCell(row3_2);  
              
//           第三行 row3  
            PdfPCell row3_1 = new PdfPCell(new Paragraph("签名:                        " + params.get("borrowDate"), simsun));  
            row3_1.setColspan(6);  
            row3_1.setBorder(0);  
            row3_1.setPaddingRight(10f);  
            row3_1.setBorderWidthLeft(0.2f);  
            row3_1.setBorderWidthRight(0.2f);  
//          row3_1.setBorderColorTop(BaseColor.WHITE);//ColorTop(BaseColor.RED);  
            row3_1.setFixedHeight(20);  
            row3_2.setPaddingBottom(6f);  
            row3_1.setHorizontalAlignment(Element.ALIGN_RIGHT);  
            table.addCell(row3_1);  
          
              
//           第四行 row4  
            PdfPCell row5 = new PdfPCell(new Paragraph("领用资产情况", bold));  
            row5.setColspan(6);  
            row5.setFixedHeight(25);  
            row5.setPadding(5);  
            row5.setHorizontalAlignment(Element.ALIGN_CENTER); // 垂直居中  
            table.addCell(row5);  
              
//           第五行 row5  
            PdfPCell cell1 = new PdfPCell(new Paragraph("固定资产名称", simsun));  
            cell1.setHorizontalAlignment(Element.ALIGN_CENTER);  
            cell1.setFixedHeight(25);  
            table.addCell(cell1);  
            PdfPCell cell2 = new PdfPCell(new Paragraph("型号", simsun));  
            cell2.setHorizontalAlignment(Element.ALIGN_CENTER);  
            table.addCell(cell2);  
            PdfPCell cell3 = new PdfPCell(new Paragraph("配置", simsun));  
            cell3.setColspan(2);  
            cell3.setHorizontalAlignment(Element.ALIGN_CENTER);  
            table.addCell(cell3);  
            PdfPCell cell4 = new PdfPCell(new Paragraph("序列号", simsun));  
            cell4.setColspan(2);  
            cell4.setHorizontalAlignment(Element.ALIGN_CENTER);  
            table.addCell(cell4);  
              
//           第五行 动态行  
            PdfPCell cell21 = new PdfPCell(new Paragraph(params.get("assetName"), STSong));  
            cell21.setHorizontalAlignment(Element.ALIGN_CENTER);  
            cell21.setFixedHeight(25);  
            table.addCell(cell21);  
            PdfPCell cell22 = new PdfPCell(new Paragraph(params.get("assetModel"), STSong));  
            cell22.setHorizontalAlignment(Element.ALIGN_CENTER);  
            table.addCell(cell22);  
            PdfPCell cell23 = new PdfPCell(new Paragraph(params.get("assetConfig"), STSong));  
            cell23.setColspan(2);  
            cell23.setHorizontalAlignment(Element.ALIGN_CENTER);  
            table.addCell(cell23);  
            PdfPCell cell24 = new PdfPCell(new Paragraph(params.get("assetSequence"), STSong));  
            cell24.setColspan(2);  
            cell24.setHorizontalAlignment(Element.ALIGN_CENTER);  
            table.addCell(cell24);  
              
              
//          空白行1  
            table.addCell(" ");  
            table.addCell(" ");  
            PdfPCell blank11 = new PdfPCell(new Paragraph(" "));  
            blank11.setFixedHeight(25);  
            blank11.setColspan(2);  
            table.addCell(blank11);  
            PdfPCell blank12 = new PdfPCell(new Paragraph(" "));  
            blank12.setColspan(2);  
            table.addCell(blank12);  
              
//          空白行2  
            table.addCell(" ");  
            table.addCell(" ");  
            PdfPCell blank21 = new PdfPCell(new Paragraph(" "));  
            blank21.setFixedHeight(25);  
            blank21.setColspan(2);  
            table.addCell(blank21);  
            PdfPCell blank22 = new PdfPCell(new Paragraph(" "));  
            blank22.setColspan(2);  
            table.addCell(blank22);  
//          空白行3  
            table.addCell(" ");  
            table.addCell(" ");  
            PdfPCell blank31 = new PdfPCell(new Paragraph(" "));  
            blank31.setColspan(2);  
            blank31.setFixedHeight(25);  
            table.addCell(blank31);  
            PdfPCell blank32 = new PdfPCell(new Paragraph(" "));  
            blank32.setColspan(2);  
            table.addCell(blank32);  
              
//           第六行 row6  
            PdfPCell row6 = new PdfPCell(new Paragraph("分管领导意见:", simsun));  
            row6.setColspan(6);  
            row6.setFixedHeight(120);  
            row6.setPadding(5);  
            row6.setVerticalAlignment(Element.ALIGN_CENTER);  
            table.addCell(row6);  
              
//           第七行 row7  
            PdfPCell row7 = new PdfPCell(new Paragraph("总裁意见:", simsun));  
            row7.setColspan(6);  
            row7.setFixedHeight(120);  
            row7.setPadding(5);  
            row7.setVerticalAlignment(Element.ALIGN_CENTER);  
            table.addCell(row7);  
              
//           将table添加到document  
            document.add(table);  
              
            document.add(new Paragraph("KJLink International, Inc", STSong));  
            document.add(new Paragraph("凯捷技术有限公司 (c)Copyright 2005", STSong)); //   
              
            System.out.println("生成PDF成功!");  // tips message  
              
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (DocumentException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            if (document.isOpen()) {  
                document.close();  
            }  
        }  
    }  
      
    public static void main(String[] args) throws FileNotFoundException {  
          
        OutputStream os = new FileOutputStream("D:\\固定资产领用单.pdf"); // 输出PDF到指定位置  
          
        Map<String, String> params = new HashMap<String, String>(); // 参数列表  
        params.put("imgPath", "C:\\Users\\Bing\\Pictures\\KJLink.png"); // Logo图片地址  
        params.put("userName", "iteye.com");  
        params.put("userDept", "java频道");  
        params.put("borrowTime", "11-07-24 10:00");  
        params.put("borrowRemark", "去深圳出差一个月。");  
        params.put("borrowDate", "2011年07月24日");  
        params.put("assetName", "KJ-AA-0001");  
        params.put("assetModel", "ThinkPad T61");  
        params.put("assetConfig", "酷睿i5双核,2GDDR3内存,80G硬盘");  
        params.put("assetSequence", "1111-2222-3333");  
          
        PDF4iText.createPDF(os, params);  
    }  
} 
 

 

你可能感兴趣的:(itext)