iText工具类

 

package com.bi.wms.fpn.util;

 

import java.awt.Color;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import com.lowagie.text.Document;

import com.lowagie.text.DocumentException;

import com.lowagie.text.Element;

import com.lowagie.text.Font;

import com.lowagie.text.Image;

import com.lowagie.text.PageSize;

import com.lowagie.text.Paragraph;

import com.lowagie.text.pdf.BaseFont;

import com.lowagie.text.pdf.PdfContentByte;

import com.lowagie.text.pdf.PdfPCell;

import com.lowagie.text.pdf.PdfPTable;

import com.lowagie.text.pdf.PdfReader;

import com.lowagie.text.pdf.PdfStamper;

 

public class PdfUtil {

 

       /**

        *添加空行

        */

       public static void newBlankLine(Document document){

          document.add(Chunk.NEWLINE);

       }

 

/**

* 创建文字

* @param content内容

* @param font字体

* @param align对齐方式

* @return

*/

         public static Paragraph newParagraph(String content,Font font, int align){

Paragraph paragraph=new Paragraph(content, font);

paragraph.setAlignment(align);

return paragraph;

 

 

/**

* 创建单元格(内容为图片)

* @param bgColor 背景

* @param border 边框

* @param align 对齐方式

* @param colspan 所占列数

* @param obj  内容(文字或图片对象)

* @return

*/

         public static PdfPCell newPdfPCell(Color bgColor,int border,int align,int colspan,Image image){

  PdfPCell cell=new PdfPCell();

cell.setBackgroundColor(bgColor);

cell.setBorder(border);

cell.setHorizontalAlignment(align);

cell.setColspan(colspan);

cell.addElement(image);

return cell;

}

/**

* 创建单元格(内容为文字)

* @param bgColor

* @param border

* @param align

* @param colspan

* @param paragraph

* @return

*/

        public static PdfPCell newPdfPCell(Color bgColor,int border,int align,int colspan,Paragraph paragraph){

PdfPCell cell=new PdfPCell();

cell.setBackgroundColor(bgColor);

cell.setBorder(border);

cell.setHorizontalAlignment(align);

cell.setColspan(colspan);

cell.addElement(paragraph);

return cell;

}

/**

* 创建字体

* @param size 大小

* @param font 字体

* @return

* @throws Exception

*/

        public static Font newFont(int size, int font){

BaseFont bfChinese=null;

try {

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

} catch (DocumentException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return new Font(bfChinese,size,font);

}

/**

* 创建字体

* @param size大小

* @param font字体

* @param color字体颜色

* @return

* @throws Exception

*/

         public static Font newFont(int size, int font,Color color){

Font f= newFont(size,font);

f.setColor(color);

return f;

}

/**

* 创建图片

* @param imgPath 图片路径

* @param width 宽

* @param height 高

* @param align 对齐方式

* @return

*/

         public static Image newImage(String imgPath, int width, int height,int align){

  Image img=null;

  try{

 img= Image.getInstance(imgPath);

 img.scaleAbsolute(width, height);

                  img.setAlignment(align);

  }catch (Exception e) {

 

 e.printStackTrace(); 

  }

  return img;

}

 

 

/**

 * 创建一个跨多行的单元格

 * @param rows 所占行数

 * @param bgColor 背景色

 * @param paragraph 单元格内容文字

 * @param align 对齐方式 

 */

 public static  PdfPCell newPdfPCellByRows(int rows,Color bgColor,Paragraph paragraph,int align){

 PdfPTable iTable=new PdfPTable(1);

 PdfPCell iCell=new PdfPCell();

 iCell.setFixedHeight(iCell.getFixedHeight()*rows);

 iTable.addCell(iCell);

 iCell.setBackgroundColor(bgColor);

 iCell.addElement(paragraph);

 iCell.setHorizontalAlignment(align);

 PdfPCell cell=new PdfPCell(iTable);

 return cell;

 }

 

/**

 * 创建一个跨多列的单元格

 * @param colspan 所占列数

 * @param bgColor 背景色

 * @param paragraph 单元格内容文字

 * @param align 对齐方式 

 */

 public static  PdfPCell newPdfPCellByColspan(int colspan,Color bgColor,Paragraph paragraph,int align){

 PdfPTable iTable=new PdfPTable(1);

 PdfPCell iCell=new PdfPCell();

 iCell.setColspan(colspan);

 iCell.setBackgroundColor(bgColor);

 iCell.setBorder(0);

 iCell.addElement(paragraph);

 iCell.setHorizontalAlignment(align);

 iTable.addCell(iCell);

 PdfPCell cell=new PdfPCell(iTable);

 return cell;

 }

 

 

       /**

        * 添加背景图片

        * @param document

                 * @param imgPath 图片路径

                 * @param width  图片宽度

                 * @param height 图片高度

                 * @param x 图片位置x值 (文档左下角为坐标原点)

                 * @param y 图片位置y值

        */

       private static void addBackGroundImg(Document document,String imgPath,float width,float height, int x, int y)throws Exception{


        Image bakimage = Image.getInstance( imgPath );

    bakimage.setAlignment(Image.UNDERLYING);

    bakimage.setAbsolutePosition(x, y);

    bakimage.setAlignment(Element.ALIGN_TOP);

    bakimage.scaleAbsoluteWidth(width);

    bakimage.scaleAbsoluteHeight(height);

    document.add(bakimage);

       }

       

       /**

        * 添加水印(图片+文字)

        * @param inputFile 原始文件

        * @param outputFile 水印输出文件

        * @param waterMark 水印名字

        * @param waterMarkImgPath 水印图片路径

        */

       //private static void addWaterMark(String inputFile,String outputFile,String waterMark,String waterMarkImgPath)throws Exception{

       public static void addWaterMark(String inputFile,String outputFile,String waterMark)throws Exception{

        PdfReader reader = new PdfReader(inputFile);  

        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile)); 

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

        int total = reader.getNumberOfPages() + 1;

        //String waterMarkImgPath="http://127.0.0.1:8080/ceb/WebContent/images/report_background.jpg";

        String waterMarkImgPath="D:/_My_Work_Space/ceb/WebContent/images/report_background.jpg";

        Image image=Image.getInstance(waterMarkImgPath);

        image.scaleAbsolute(PageSize.A4.width(), PageSize.A4.height());

        image.setAbsolutePosition(0, 0);

        PdfContentByte under;  

           int j = waterMark.length();  

           char c = 0;  

           int rise = 0;  

           for (int i = 1; i < total; i++) {  

               rise = 500;  

               under = stamper.getUnderContent(i);  

               // 添加图片  

               under.addImage(image);  

               under.beginText();  

               under.setColorFill(Color.LIGHT_GRAY);  

               under.setFontAndSize(base, 40);  

               // 设置水印文字字体倾斜 开始  

               if (j >= 15) {  

                   under.setTextMatrix(200, 50);  

                   for (int k = 0; k < j; k++) {  

                       under.setTextRise(rise);  

                       c = waterMark.charAt(k);  

                       under.showText(c + "");  

                       rise -= 35;  

                   }  

               } else {  

                   under.setTextMatrix(180, 40);  

                   for (int k = 0; k < j; k++) {  

                       under.setTextRise(rise);  

                       c = waterMark.charAt(k);  

                       under.showText(c + "");  

                       rise -= 25;  

                   }  

               }  

               // 字体设置结束  

               under.endText(); 

               

           }

           stamper.close();  

           //删除源文件,保留加水印后的文件

           File file=new File(inputFile);

           file.delete();

       }

 

 

 

}


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值