使用iText生成带底图的PDF

package com.wzh.itext;



import com.itextpdf.text.Chunk;

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;

import java.io.ByteArrayOutputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.util.logging.Level;

import java.util.logging.Logger;



/**

 * @date 2015-3-17 12:48:24

 * @author y

 * @desc 

 */

public class ItextUtil {

    

    /**

     * 生成带底图的PDF字节流

     * @param swdjz

     * @return 

     */

    public static byte[] createPdfBytes(Swdjz swdjz){

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        byte []content = null;

        

        

        Document doc = new Document(PageSize.A4.rotate(), 50, 50, 50, 50); //创建文档 ,A4纸,横屏 

        

         try {

            PdfWriter pw = PdfWriter.getInstance(doc, baos);

            doc.open();

            

            //解决中文不显示问题

            BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

            //定义字体

            Font fontChinaTitle14 = new Font(bfChinese, 14, Font.BOLD);

            Font fontChina16 = new Font(bfChinese, 16);

            Font fontChina14 = new Font(bfChinese, 14);

            

            //加载电子税务登记证背景图片

            Image imgBg = Image.getInstance(swdjz.getBgPath());

            imgBg.setAbsolutePosition(0, 0); //设置图片位置

            imgBg.scaleAbsolute(842, 595);//设置图片大小

            doc.add(imgBg);

            

            // 空行

            Paragraph blank = new Paragraph(" ");

            doc.add(blank);

            doc.add(blank);

            doc.add(blank);

            doc.add(blank);

            doc.add(blank);

            

            /*右上角信息*/

            //档案号

            Chunk c01 = new Chunk(swdjz.getDanh(), fontChina16);

            Paragraph snoParagraph01 = new Paragraph();

            snoParagraph01.add(c01);

            snoParagraph01.setAlignment(Element.ALIGN_RIGHT);

            snoParagraph01.setIndentationRight(30);

            snoParagraph01.setSpacingBefore(10);

            doc.add(snoParagraph01);

            //纳税人识别号

            Chunk c02 = new Chunk(swdjz.getNsrsbh(), fontChina16);

            Paragraph snoParagraph02 = new Paragraph();

            snoParagraph02.add(c02);

            snoParagraph02.setAlignment(Element.ALIGN_RIGHT);

            snoParagraph02.setIndentationRight(28);

            doc.add(snoParagraph02);

            

            doc.add(blank);

            doc.add(blank);

            

            /*正文*/

            //纳税人名称

            Chunk c03 = new Chunk(swdjz.getNsrmc(), fontChina16);

            Paragraph snoParagraph03 = new Paragraph();

            snoParagraph03.add(c03);

            snoParagraph03.setIndentationLeft(220);

            doc.add(snoParagraph03);

            

            doc.add(blank);

            

            //法定代表人

            Chunk c04 = new Chunk(swdjz.getFddbr(), fontChina16);

            Paragraph snoParagraph04 = new Paragraph();

            snoParagraph04.add(c04);

            snoParagraph04.setIndentationLeft(220);

            doc.add(snoParagraph04);

            

            doc.add(blank);

            

            //地址

            Chunk c05 = new Chunk(swdjz.getDz(), fontChina16);

            Paragraph snoParagraph05 = new Paragraph();

            snoParagraph05.add(c05);

            snoParagraph05.setIndentationLeft(220);

            snoParagraph05.setSpacingBefore(3);

            doc.add(snoParagraph05);

            

            doc.add(blank);

            

            //登记注册类型

            Chunk c06 = new Chunk(swdjz.getDjzclx(), fontChina16);

            Paragraph snoParagraph06 = new Paragraph();

            snoParagraph06.add(c06);

            snoParagraph06.setIndentationLeft(220);

            snoParagraph06.setSpacingBefore(4);

            doc.add(snoParagraph06);

            

            doc.add(blank);

            

            //经营范围

            Chunk c07 = new Chunk(swdjz.getJyfw(), fontChina14);

            Paragraph snoParagraph07 = new Paragraph();

            snoParagraph07.add(c07);

            snoParagraph07.setIndentationLeft(220);

            doc.add(snoParagraph07);

            

            doc.add(blank);

            

            //批准设立机关

            Chunk c08 = new Chunk(swdjz.getPzsljg(), fontChina16);

            Paragraph snoParagraph08 = new Paragraph();

            snoParagraph08.add(c08);

            if(swdjz.getJyfw().length()>37){

                snoParagraph08.setSpacingBefore(-10);

            }else{

                snoParagraph08.setSpacingBefore(4);

            }

            snoParagraph08.setIndentationLeft(220);

            doc.add(snoParagraph08);

            

            doc.add(blank);

            

            //扣缴义务

            Chunk c09 = new Chunk(swdjz.getKjyw(), fontChina16);

            Paragraph snoParagraph09 = new Paragraph();

            snoParagraph09.add(c09);

            if(swdjz.getJyfw().length()<38){

                snoParagraph09.setSpacingBefore(4);

            }

            snoParagraph09.setIndentationLeft(220);

            doc.add(snoParagraph09);

            

            doc.add(blank);

            doc.add(blank);

            doc.add(blank);

            

            /*右下角*/

            //发证日期

            Chunk cYear = new Chunk("一三     ", fontChina16);

            Chunk cMonth = new Chunk("   八   ", fontChina16);

            Chunk cDay = new Chunk("   十二   ", fontChina16);

            

            Paragraph snoParagraph10 = new Paragraph();

            snoParagraph10.add(cYear);

            snoParagraph10.add(cMonth);

            snoParagraph10.add(cDay);

            snoParagraph10.setSpacingBefore(4);

            snoParagraph10.setIndentationRight(45);

            snoParagraph10.setAlignment(Element.ALIGN_RIGHT);

            doc.add(snoParagraph10);

            

            

            doc.close();

            

            content = baos.toByteArray();

            

        } catch (FileNotFoundException ex) {

            Logger.getLogger(ItextUtil.class.getName()).log(Level.SEVERE, null, ex);

        } catch (DocumentException ex) {

            Logger.getLogger(ItextUtil.class.getName()).log(Level.SEVERE, null, ex);

        } catch (IOException ex) {

            Logger.getLogger(ItextUtil.class.getName()).log(Level.SEVERE, null, ex);

        }finally{

            try {

                baos.close();

            } catch (IOException ex) {

                Logger.getLogger(ItextUtil.class.getName()).log(Level.SEVERE, null, ex);

            }

        }

        

        return content;

    }

    

}

Servlet 中:

 response.setContentType("application/pdf");

        

        ServletOutputStream sos = response.getOutputStream();

        

        response.setHeader("Content-Disposition", "attachment;filename="

                        + new String(swdjz.getNsrmc().getBytes(), "iso-8859-1"));



        

        byte [] content = ItextUtil.createPdfBytes(swdjz);

        

        BufferedOutputStream bos = new BufferedOutputStream(sos);

        bos.write(content);

        

        bos.close();

 

你可能感兴趣的:(itext)