iText PDFServlet

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>
    This is my JSP page. <br>
    <a href="pdf">打开 PDF 文档</a><br>
    <a href="pdf" target="_blank">新窗口打开 PDF 文档</a><br>
  </body>
</html>

 

package com.helloweenvsfei.itext;

import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;

public class PDFServlet extends HttpServlet {

    private static final long serialVersionUID = -1770041214313726383L;

    public PDFServlet() {
        super();
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // 创建文档对象,A4纸大小
        Document document = new Document(PageSize.A4);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        try {
            PdfWriter writer = PdfWriter.getInstance(document, stream);
            //设置文档作者
            document.addAuthor("Helloween");
            //设置文档标题
            document.addTitle("This is itext pdf file");
            //设置主题
            document.addSubject("First pdf");
            //设置关键字
            document.addKeywords("iText");

            // 打开文档
            document.open();
            // 在pdf文件中写入文字
            document.add(new Paragraph("Hello World, Hello iText"));
            //设置字体的文字
            document.add(new Paragraph("Hello World, Hello iText",FontFactory.getFont(FontFactory.COURIER, 12, Font.BOLDITALIC)));
            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
            //设置为中文
            Paragraph pragraph=new Paragraph("你好,这是中文", FontChinese);  
            document.add(pragraph);
            
            // 创建2行3列的表
            Table table = new Table(3, 2);
            // 设置表格边框颜色
            table.setBorderColor(new Color(220, 100, 100));
            // 设置表格边距
            table.setPadding(5);
            // 设置表格间距
            table.setSpacing(3);
            // 设置表格线条宽度
            table.setBorderWidth(3);
            //创建单元格对象
            Cell cell = new Cell(" Header 1 ");
            //将单元格添加到表格中
            table.addCell(cell);
            cell = new Cell(" Header 2 ");
            //设置单元格占2列
            cell.setColspan(2);
            table.addCell(cell);
            //将普通文本添加到表格中
            table.addCell(" Cell 1 ");
            table.addCell(" Cell 2 ");
            table.addCell(" Cell 3 ");
            //将表格添加到文档中
            document.add(table);


            //创建图片对象,参数为图片的文件名
            Image bmp = Image.getInstance("C:\\Users\\girl.jpg");
            bmp.scalePercent(25f);
            document.add(bmp);
            // 关闭文档
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 设置响应文档类型为pdf
        response.setContentType("application/pdf");
        // 设置响应数据大小
        response.setContentLength(stream.size());
        // 获得响应数据流
        ServletOutputStream out = response.getOutputStream();
        // 将pdf数据流写入到响应数据流中
        stream.writeTo(out);
        out.flush();
        out.close();

    }

}

 

posted @ 2017-05-16 16:05  sky20080101  阅读(146)  评论(0)    收藏  举报