PDF整理

来源:互联网 发布:微信清理软件 编辑:程序博客网 时间:2024/05/04 17:37

1、设置中文字体

    private static Font headfont ;// 设置字体大小    
    private static Font keyfont;// 设置字体大小    
    private static Font textfont;// 设置字体大小   

static{    
        BaseFont bfChinese;    
        try {
            bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);    
            headfont = new Font(bfChinese, 10, Font.BOLD);// 设置字体大小    
            keyfont = new Font(bfChinese, 8, Font.BOLD);// 设置字体大小    
            textfont = new Font(bfChinese, 8, Font.NORMAL);// 设置字体大小    
        } catch (Exception e) {             
            e.printStackTrace();    
        }     
    }   


2、设置英文字体

HELVETICA:表示英文字体

1)

        private static Font headfont ;// 设置字体大小    
        private static Font keyfont;// 设置字体大小    
        private static Font textfont;// 设置字体大小
        
        static{   
            try {   
                headfont = new Font(Font.HELVETICA, 9, Font.BOLD);// 设置字体大小    
                keyfont = new Font(Font.HELVETICA, 8, Font.NORMAL);// 设置字体大小    
                textfont = new Font(Font.HELVETICA, 6, Font.NORMAL);// 设置字体大小    
            } catch (Exception e) {             
                e.printStackTrace();    
            }     
        }

2)

BaseFont bfEnglish = BaseFont.createFont(BaseFont.HELVETICA,BaseFont.WINANSI,BaseFont.NOT_EMBEDDED);

headfont = new Font(bfEnglish, 9, Font.BOLD);// 设置字体大小    
keyfont = new Font(bfEnglish, 8, Font.NORMAL);// 设置字体大小    
textfont = new Font(bfEnglish, 6, Font.NORMAL);// 设置字体大小   


3、在PDF添加表格,只需要添加列数

如:    public static PdfPTable createTable(int colNumber){    
        PdfPTable table = new PdfPTable(colNumber);    
        try{    
            table.setTotalWidth(520);//表格的总宽度   
            table.setWidths(new int[]{324,98,98});//设置每列的宽度
            table.setLockedWidth(true);//锁定列的宽度    
            table.setHorizontalAlignment(Element.ALIGN_CENTER); //设置水平样式居中
            table.getDefaultCell().setBorder(1);//设置表格的默认边框为1
        }catch(Exception e){    
            e.printStackTrace();    
        }    
        return table;    
    }

4、在PDF如果设置表格的背景色,由于表格本身没有提供方法设置背景色,只需要设置每列的背景色即可

如:   public static PdfPCell createCell1(String value,com.lowagie.text.Font font,int align){    
        PdfPCell cell = new PdfPCell();    
     
        cell.setHorizontalAlignment(align);        
        cell.setPhrase(new Phrase(value,font));    
        cell.setColspan(3);//合并三列

        cell.setBackgroundColor(new Color(153,51,102));//设置背景色
        cell.setBorder(0);//设置边框
        cell.setPadding(2);//设置内边距
        //cell.setPaddingBottom(4);
       return cell;    
   }


0 0
原创粉丝点击