java pdf 页眉_java操作pdf添加页眉条码添加水印图片

importjava.awt.image.BufferedImage;importjava.io.ByteArrayOutputStream;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjavax.imageio.ImageIO;importorg.apache.commons.io.IOUtils;importorg.jbarcode.JBarcode;importorg.jbarcode.encode.Code39Encoder;importorg.jbarcode.encode.InvalidAtributeException;importorg.jbarcode.paint.BaseLineTextPainter;importorg.jbarcode.paint.WideRatioCodedPainter;importcom.lowagie.text.BadElementException;importcom.lowagie.text.DocumentException;importcom.lowagie.text.Image;importcom.lowagie.text.Rectangle;importcom.lowagie.text.pdf.PdfContentByte;importcom.lowagie.text.pdf.PdfGState;importcom.lowagie.text.pdf.PdfReader;importcom.lowagie.text.pdf.PdfStamper;/*** pdf工具

*

*@author宋杰

* @date 2016-07-11 13:19:33*/

public classPdfUtils {/*** 添加条码

*@paramstr 条码内容

*@paramfilepath pdf 文件绝对路径

*@paraml_height 水平位置

*@paraml_weight 垂直位置*/public static void addString(String str,String filepath,int l_height,intl_weight){

BufferedImage localBufferedImage=null;

JBarcode jbcode= null;try{//1.创建条码图像

jbcode = neworg.jbarcode.JBarcode(Code39Encoder.getInstance(), WideRatioCodedPainter.getInstance(),BaseLineTextPainter.getInstance());

localBufferedImage=jbcode.createBarcode(str);

ByteArrayOutputStream bao= newByteArrayOutputStream();

ImageIO.write(localBufferedImage,"png", bao);

Image img=Image.getInstance(bao.toByteArray());

img.setAlignment(1); //居中显示

img.setAbsolutePosition(l_height, l_weight);//显示位置,根据需要调整

img.scalePercent(60); //显示为原条形码图片大小的比例,百分比//2.创建pdf输入输出流

InputStream is = newFileInputStream(filepath);

PdfReader reader= newPdfReader(is);

OutputStream os= newFileOutputStream(filepath);

PdfStamper stamp= newPdfStamper(reader, os);

PdfContentByte contentByte= null;int n =reader.getNumberOfPages();//3. 设置透明度

PdfGState gs = newPdfGState();

gs.setFillOpacity(0.7f);

gs.setStrokeOpacity(0.7f);//4.在pdf每页右上角添加条码

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

contentByte= stamp.getOverContent(i); //getOverContent 水印会把正文盖住 getUnderContent 水印会被正文的图片盖住

contentByte.setGState(gs);

contentByte.addImage(img);//contentByte.addImage(Image.getInstance("D:/primeton/yunda/ide/eclipse/workspace/ydsoa/com.yd.soa.budget/src/webcontent/comm/logo.jpg"));

}//5.关闭所有输入输出

reader.close();

stamp.close();

IOUtils.closeQuietly(bao);

IOUtils.closeQuietly(is);

IOUtils.closeQuietly(os);

}catch(InvalidAtributeException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}catch(BadElementException e) {

e.printStackTrace();

}catch(DocumentException e) {

e.printStackTrace();

}

}/*** 添加图片水印 居中

*@paramimagepath 图片文件绝对路径

*@paramfilepath pdf 文件绝对路径*/public static voidaddWaterImage(String imagepath,String filepath){

InputStream is= null;

PdfReader reader= null;

OutputStream os= null;

PdfStamper stamp= null;try{//1.创建pdf输入输出流

is = newFileInputStream(filepath);

reader= newPdfReader(is);

os= newFileOutputStream(filepath);

stamp= newPdfStamper(reader, os);

PdfContentByte contentByte= null;int n =reader.getNumberOfPages();//2. 设置透明度

PdfGState gs = newPdfGState();

gs.setFillOpacity(0.5f);

gs.setStrokeOpacity(0.5f);//3. 读取图片

Image logo =Image.getInstance(imagepath);//4.在pdf每页右上角添加条码

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

contentByte= stamp.getUnderContent(i); //getOverContent 水印会把正文盖住 getUnderContent 水印会被正文的图片盖住

contentByte.setGState(gs);

Rectangle rectangle=reader.getPageSize(i);float width =rectangle.getWidth();float height =rectangle.getHeight();

logo.setAbsolutePosition(width/2-logo.getWidth()/2, height/2);

contentByte.addImage(logo);

}

}catch(IOException e) {

e.printStackTrace();

}catch(BadElementException e) {

e.printStackTrace();

}catch(DocumentException e) {

e.printStackTrace();

}finally{//5.关闭所有输入输出

reader.close();try{

stamp.close();

}catch(DocumentException e) {

}catch(IOException e) {

}

IOUtils.closeQuietly(is);

IOUtils.closeQuietly(os);

}

}public static voidmain(String[] args){

addString("YDHT-CG-20160525-001", "D:/111.pdf", 400, 795);

addWaterImage("com.yd.soa.budget/src/webcontent/comm/logo.jpg", "D:/111.pdf");

}

}

你可能感兴趣的:(java,pdf,页眉)