ITEXTPDF使用示例

表格操作

段落操作

package com.eams.web.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.eams.app.business.check.constant.CheckConstant;
import com.eams.app.business.workflow.entity.CommentVO;
import com.eams.app.common.entity.CustomConfigProperties;
import com.eams.common.jdbctemplate.utils.id.UUIDDirective;
import com.eams.common.util.DateUtils;
import com.eams.web.business.CheckController;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.AcroFields.FieldPosition;

import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
public class PdfUtils {
    private static final Logger logger = Logger.getLogger(PdfUtils.class);
    
    
    
    public static int jiachu=3;
    public static int left=1;
    public static int right=2;
    public static String fontpath="";
    public static PdfPCell duetitle(String title,int ms) throws DocumentException, IOException{
        BaseFont bfChinese=BaseFont.createFont(PdfUtils.fontpath,BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
        Font textfont =new Font(bfChinese,24,Font.BOLD);
        PdfPCell cell = new PdfPCell(new Paragraph(title,textfont));
        cell.setMinimumHeight(25);
        cell.setBorder(0);
        cell.setColspan(ms);
        cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); 
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        return cell;
    }
    public static List<PdfPCell> dueHeard(String[] hs) throws DocumentException, IOException{
        List<PdfPCell> l=new ArrayList<PdfPCell>();
        BaseFont bfChinese=BaseFont.createFont(PdfUtils.fontpath,BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
        Font textfont =new Font(bfChinese,10.5f,Font.BOLD);
        for (int i=0;i<hs.length;i++) {
            String string = hs[i];
            PdfPCell cell = new PdfPCell(new Paragraph(string,textfont));
            cell.setMinimumHeight(25);
            cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); 
            cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            l.add(cell);
        }
        return l;
    }
    public static PdfPCell duecon(String con,Integer jiachual) throws DocumentException, IOException{
        BaseFont bfChinese=BaseFont.createFont(PdfUtils.fontpath,BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
        Font textfont =new Font(bfChinese,10.5f,Font.NORMAL);
        PdfPCell cell = new PdfPCell(new Paragraph(con,textfont));
        cell.setUseAscender(true);
        cell.setMinimumHeight(25);
        cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); 
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        return cell;
    }
    
    public static PdfPCell duecon(String con) throws DocumentException, IOException{
        return duecon(con, null);
    }
    public static String strnull(Object s){
        if(s==null){
            return "";
        }else{
            return String.valueOf(s);
        }
    }
    public static String getpdftempPath() {
        String re=CustomConfigProperties.getConfigProperty("efile.tmppath");
        File f=new File(re);
        if(!f.exists()){
            f.mkdirs();
        }
        if(!re.endsWith("/")&&!re.endsWith("\\")){
            re=re+"/";
        }
        return re;
    }
    
    public static void main(String[] args) throws FileNotFoundException {
        fontpath="e:/test/simsun.ttf";
        
        List<CommentVO> comments =new ArrayList<CommentVO>();
        for (int i = 0; i <13; i++) {
            CommentVO c=new CommentVO();
            comments.add(c);
        }
        String temppdffile="e:/test/"+DateUtils.generateLongTime()+".pdf";
        Document document =new Document(PageSize.A4,50,50,30,40);
        PdfWriter writer =null;
        OutputStream out=null;
        try{
            out=new FileOutputStream(new File(temppdffile));
            writer =PdfWriter.getInstance(document,out);
            //writer.setPageEvent(new HeadFootInfoPdfPageEvent());
            document.open();
            
            BaseFont bfChinese=BaseFont.createFont(PdfUtils.fontpath,BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
            Font textfont =new Font(bfChinese,10.5f,Font.NORMAL);
            Paragraph p=new Paragraph("擦撒开绿豆腐",textfont);
            //首行缩进
            p.setFirstLineIndent(75);
            //左缩进
            p.setIndentationLeft(40);
            //右缩进
            p.setIndentationRight(40);
            //居左右中
            p.setAlignment(Paragraph.ALIGN_RIGHT);
            
            document.add(p);
//            document.add(table);
            document.close();
            writer.flush();
            writer.close();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            IOUtils.closeQuietly(out);
            try{
                document.close();
            }catch(Exception e1){
                e1.printStackTrace();
            }
            try{
                writer.close();
            }catch(Exception e1){
                e1.printStackTrace();
            }
        }
    }
    public static void printpdf(OutputStream printout,List<CommentVO> comments ,String title){
        String temppath= PdfUtils.getpdftempPath();
        String temppdffile=temppath+UUIDDirective.gen()+".pdf";
        Document document =new Document(PageSize.A4,50,50,30,40);
        PdfWriter writer =null;
        OutputStream out=null;
        try{
            out=new FileOutputStream(new File(temppdffile));
            writer =PdfWriter.getInstance(document,out);
            document.open();
            BaseFont bfChinese=BaseFont.createFont(PdfUtils.fontpath,BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
            
            //标题
            Font titletextfont =new Font(bfChinese,10.5f,Font.BOLD);
            Paragraph ptitle=new Paragraph(strnull(title),titletextfont);
            ptitle.setAlignment(Paragraph.ALIGN_CENTER);
            document.add(ptitle);
            
            //空一行
            Paragraph pempty=new Paragraph(" ",titletextfont);
            document.add(pempty);
            
            Font textfont =new Font(bfChinese,10.5f,Font.NORMAL);
            for(int i=0;i<comments.size();i++) {
                CommentVO map=comments.get(i);
                if(!CheckConstant.tg.equals(map.getOperation())){
                    //只记录同意的审核
                    continue;
                }
                //任务名
                Paragraph prwm=new Paragraph(strnull(map.getTaskName()+":"),textfont);
                prwm.setAlignment(Paragraph.ALIGN_LEFT);
                //办理意见
                Paragraph pblyj=new Paragraph(strnull(map.getMessage()),textfont);
                //首行缩进
                pblyj.setFirstLineIndent(75);
                //左缩进
                pblyj.setIndentationLeft(54);
                //右缩进
                pblyj.setIndentationRight(54);
                //办理人及时间
                Paragraph pblr=new Paragraph(strnull(map.getUserName()+"  "+map.getTimeStr()),textfont);
                pblr.setAlignment(Paragraph.ALIGN_RIGHT);
                pblr.setIndentationRight(54);
                document.add(prwm);
                document.add(pblyj);
                document.add(pblr);
            }
            document.close();
            writer.flush();
            writer.close();
            
        }catch(Exception e){
            logger.error("生成pdf失败",e);
        }finally{
            IOUtils.closeQuietly(out);
            try{
                document.close();
            }catch(Exception e1){
                e1.printStackTrace();
            }
            try{
                writer.close();
            }catch(Exception e1){
                e1.printStackTrace();
            }
        }
        File f=new File(temppdffile);
        if(f.exists()){
            FileInputStream fin=null;
            try {
                fin=new FileInputStream(new File(temppdffile));
                IOUtils.copy(fin, printout);
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                IOUtils.closeQuietly(fin);
                f.delete();
            }
        }
    }
    public static void printpdf_BACK(OutputStream printout,List<CommentVO> comments ){
        String temppath= PdfUtils.getpdftempPath();
        String temppdffile=temppath+UUIDDirective.gen()+".pdf";
        Document document =new Document(PageSize.A4,50,50,30,40);
        PdfWriter writer =null;
        OutputStream out=null;
        try{
            out=new FileOutputStream(new File(temppdffile));
            int column=7;
            int width[] = {16,8,16,12,16,24,8};//设置每列宽度比例   
            writer =PdfWriter.getInstance(document,out);
            //writer.setPageEvent(new HeadFootInfoPdfPageEvent());
            document.open();
            PdfPTable table = new PdfPTable(column);//括号参数表示列
            table.setWidthPercentage(100);
            table.setWidths(width); 
            table.setHorizontalAlignment(Element.ALIGN_CENTER);//居中
            
            table.setHeaderRows(1);
            table.getDefaultCell().setVerticalAlignment(Element.ALIGN_CENTER);
            table.getDefaultCell().setMinimumHeight(50);

            List<PdfPCell> hs= PdfUtils.dueHeard(new String[]{"接收时间","办理人","办理人单位","流程环节","提交时间","办理意见","操作"});
            for (PdfPCell pdfPCell : hs) {
                table.addCell(pdfPCell);
            }
            //处理开始时间
            String bgtime=null;
            for(int i=0;i<comments.size();i++) {
                CommentVO map=comments.get(i);
                String tbgtime=null;
                if(bgtime==null){
                    tbgtime=map.getTimeStr();
                }else{
                    tbgtime=bgtime;
                }
                bgtime=map.getTimeStr();
                
                table.addCell(PdfUtils.duecon(PdfUtils.strnull(tbgtime),PdfUtils.left));
                table.addCell(PdfUtils.duecon(PdfUtils.strnull(map.getUserName())));
                table.addCell(PdfUtils.duecon(PdfUtils.strnull(map.getUnitName())));
                table.addCell(PdfUtils.duecon(PdfUtils.strnull(map.getTaskName())));
                table.addCell(PdfUtils.duecon(PdfUtils.strnull(map.getTimeStr())));
                table.addCell(PdfUtils.duecon(PdfUtils.strnull(map.getMessage())));
                table.addCell(PdfUtils.duecon(PdfUtils.strnull(map.getOperation()),PdfUtils.right));
            }
            document.add(table);
            document.close();
            writer.flush();
            writer.close();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            IOUtils.closeQuietly(out);
            try{
                document.close();
            }catch(Exception e1){
                e1.printStackTrace();
            }
            try{
                writer.close();
            }catch(Exception e1){
                e1.printStackTrace();
            }
        }
        File f=new File(temppdffile);
        if(f.exists()){
            FileInputStream fin=null;
            try {
                fin=new FileInputStream(new File(temppdffile));
                IOUtils.copy(fin, printout);
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                IOUtils.closeQuietly(fin);
                f.delete();
            }
        }
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值