pdf打印

package aacmm.common.pdf;


import org.apache.commons.logging.Log;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;


/**
 * 设置打印页脚信息
 * 
 * @霍春华
 * 
 */
public class FootInfoPdfPageEvent extends PdfPageEventHelper {
protected Log logger = org.apache.commons.logging.LogFactory.getLog(this
.getClass());

 private String cont;//页脚内容  
   public FootInfoPdfPageEvent() {  
 
   }  
   public FootInfoPdfPageEvent(String cont) {  
       this.cont = cont;  
   }  

 
   @Override  
   public void onEndPage(PdfWriter writer, Document document) {  
   
      
       //添加页脚
       try {  
        BaseFont bfChinese = BaseFont.createFont("STSong-Light",
                    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
     
       Font font = new Font(bfChinese,8);  
           PdfPTable table = new PdfPTable(1);  
           //设置表格的宽度
           table.setTotalWidth(new float[]{540});  
           Paragraph button=new Paragraph(cont,font);  


           PdfPCell cell = new PdfPCell(button);  
           cell.setHorizontalAlignment(Element.ALIGN_LEFT); 
           cell.setBorder(Rectangle.NO_BORDER);  
           table.addCell(cell);  
           
           //设置答应的页脚位置
           table.writeSelectedRows(-10,-10,85,52,writer.getDirectContent()); 
 
       } catch (Exception e) {  
        //打印错误的信息
        logger.error(e);  
       }  
 
   }  
 
  

}




package aacmm.common.pdf;


import java.io.IOException;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.GrayColor;
import com.itextpdf.text.pdf.PdfPCell;


/**
 * 
 * @author 霍春华
 *
 */
public class PdfUtil {
private static Font pdf20Font = null;
//获取指定内容与字体的单元格  
public static PdfPCell getPDFCell(String string, Font font)   
{  
   //创建单元格对象,将内容与字体放入段落中作为单元格内容  
   PdfPCell cell=new PdfPCell(new Paragraph(string,font));  
   cell.setHorizontalAlignment(Element.ALIGN_CENTER);  
   cell.setVerticalAlignment(Element.ALIGN_MIDDLE); 
   //设置最小单元格高度  
   cell.setMinimumHeight(25);  
   return cell;  


//合并行的静态函数  
   public static PdfPCell mergeRow(String str,Font font,int i) {  
         
       //创建单元格对象,将内容及字体传入  
       PdfPCell cell=new PdfPCell(new Paragraph(str,font));  
       //设置单元格内容居中  
       cell.setHorizontalAlignment(Element.ALIGN_CENTER);  
       cell.setVerticalAlignment(Element.ALIGN_MIDDLE);  
       //将该单元格所在列包括该单元格在内的i行单元格合并为一个单元格  
       cell.setRowspan(i);  
         
       return cell;  
   }  
   
   //合并列的静态函数  
   public static PdfPCell mergeCol(String str,Font font,int i) {  
         
       PdfPCell cell=new PdfPCell(new Paragraph(str,font));  
       cell.setMinimumHeight(25);  
       cell.setHorizontalAlignment(Element.ALIGN_CENTER);  
       cell.setVerticalAlignment(Element.ALIGN_MIDDLE);  
       //将该单元格所在行包括该单元格在内的i列单元格合并为一个单元格  
       cell.setColspan(i);     
       return cell;  
   }  
   
   
  public static Font getChinese20Font() throws DocumentException, IOException {
   
     
    if (pdf20Font == null) {


            // 设置中文字体和字体样式
            BaseFont bfChinese = BaseFont.createFont("STSong-Light",
                    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            pdf20Font = new Font(bfChinese);
            pdf20Font.setSize(20);
            pdf20Font.setColor(new GrayColor(0.9f));
            
        }
       return pdf20Font;
   }

}





package aacmm.common.pdf;
import org.apache.commons.logging.Log;


import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.GrayColor;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;


/**
 * 
 * @author 霍春华
 * pdf打印添加水印
 *
 */


public class Watermark extends PdfPageEventHelper {

protected Log logger = org.apache.commons.logging.LogFactory.getLog(this
.getClass());

Font FONT = new Font(FontFamily.HELVETICA, 30, Font.BOLD, new GrayColor(0.9f)); 
     
   private String waterCont;//水印内容  
   private String time;//水印内容  
   public Watermark() {  
 
   }  
   public Watermark(String waterCont,String time) {  
       this.waterCont = waterCont;  
       this.time = time; 
   }  
 
 @Override  
 public void onEndPage(PdfWriter writer, Document document) {  
   try {
      for(int i=0 ; i<5; i++) {  
           for(int j=0; j<5; j++) {  
           
ColumnText.showTextAligned(writer.getDirectContentUnder(),  
                          Element.ALIGN_CENTER,  
                          new Phrase(this.waterCont == null ? "" : this.waterCont, PdfUtil.getChinese20Font()),  
                          (50.5f+i*350),  
                          (40.0f+j*150),  
                          writer.getPageNumber() % 2 == 1 ? 45 : -45);

ColumnText.showTextAligned(writer.getDirectContentUnder(),  
                          Element.ALIGN_CENTER,  
                          new Phrase(this.time == null ? "" : this.time, PdfUtil.getChinese20Font()),  
                          (50.5f+i*350),  
                          (40.0f+j*150+50),  
                          writer.getPageNumber() % 2 == 1 ? 45 : -45);

           }  
       }
      } 
     catch(Exception e){
     logger.error(e);
     }
 }
 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值