Post updated by `MetaWeblog API` on Mon Nov 26 2018 23:47:52 GMT+0800 (中国标准时间)

添加条码页眉以及图片水印
1. 引入jar包
    1. itext-4.2.1.jar
    2. itext-asian-5.2.0.jar
    3. jbarcode-0.2.8.jar
2. 代码
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import org.apache.commons.io.IOUtils;
import org.jbarcode.JBarcode;
import org.jbarcode.encode.Code39Encoder;
import org.jbarcode.encode.InvalidAtributeException;
import org.jbarcode.paint.BaseLineTextPainter;
import org.jbarcode.paint.WideRatioCodedPainter;import com.lowagie.text.BadElementException;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Image;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfGState;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper; /**
* pdf工具
*
* @author 宋杰
* @date 2016-07-11 13:19:33
*/
public class PdfUtils {
/**
* 添加条码
* @param str 条码内容
* @param filepath pdf 文件绝对路径
* @param l_height 水平位置
* @param l_weight 垂直位置
*/public static void addString(String str,String filepath,int l_height,int l_weight){
BufferedImage localBufferedImage=null;
JBarcode jbcode = null;
try {
//1.创建条码图像
jbcode = new org.jbarcode.JBarcode(Code39Encoder.getInstance(), WideRatioCodedPainter.getInstance(),BaseLineTextPainter.getInstance());
localBufferedImage = jbcode.createBarcode(str);
ByteArrayOutputStream bao= new ByteArrayOutputStream();
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 = new FileInputStream(filepath);
PdfReader reader = new PdfReader(is);
OutputStream os = new FileOutputStream(filepath);
PdfStamper stamp = new PdfStamper(reader, os);
PdfContentByte contentByte = null;
int n = reader.getNumberOfPages();
//3. 设置透明度
PdfGState gs = new PdfGState();
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();
}
} /**
* 添加图片水印 居中
* @param imagepath 图片文件绝对路径
* @param filepath pdf 文件绝对路径
*/public static void addWaterImage(String imagepath,String filepath){
InputStream is = null;
PdfReader reader = null;
OutputStream os = null;
PdfStamper stamp = null;
try {
//1.创建pdf输入输出流
is = new FileInputStream(filepath);
reader = new PdfReader(is);
os = new FileOutputStream(filepath);
stamp = new PdfStamper(reader, os); PdfContentByte contentByte = null;
int n = reader.getNumberOfPages();
//2. 设置透明度
PdfGState gs = new PdfGState();
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 void main(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添加页眉条码添加水印图片的更多相关文章

  1. 用什么方法给PDF添加页眉页脚

    我们所看到的书本中都会设置好有页眉页脚,那么电子书想要添加页眉页脚要怎么操作呢,用什么方法可以在PDF中添加页眉页脚呢,今天就为大家分享一下,如何在电子文件中添加页眉页脚,想知道的小伙伴们就一起来看看 ...

  2. C# 给现有PDF文档添加页眉、页脚

    概述 页眉页脚是一篇完整.精致的文档的重要组成部分.在页眉页脚处,可以呈现的内容很多,如公司名称.页码.工作表名.日期.图片,如LOGO.标记等.在之前的文章中介绍了如何通过新建一页空白PDF页来添加 ...

  3. ABBYY FineReader 15 如何为PDF文档添加页眉页脚

    页眉.页脚是文档页面顶部或底部重复出现的文本信息.很多用户会习惯在文档页面的顶部与底部区域添加页眉.页脚来展现页码.文档标题.作者姓名.品牌名称等附加信息.而ABBYY FineReader 15(W ...

  4. C#word(2007)操作类--新建文档、添加页眉页脚、设置格式、添加文本和超链接、添加图片、表格处理、文档格式转化

    转:http://www.cnblogs.com/lantionzy/archive/2009/10/23/1588511.html 1.新建Word文档 #region 新建Word文档/// &l ...

  5. ★itext-为pdf文件添加页眉页脚 | 3步完成 |

    由于上一篇自定义生成pdf的功能需求又增加了,需要加上页码.所以本博客诞生了~ 1. 通过继承PdfPageEventHelper类,实现需要实现的方法 import com.lowagie.text ...

  6. 转 Java操作PDF之iText详细入门

    转 Java操作PDF之iText详细入门 2016年08月08日 11:06:00 阅读数:19490 iText是著名的开放项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成 ...

  7. openxml(二) 添加页眉,页脚

    openxml 中 word 文档的结构是如下图: 其中,页眉是 header,属于headerpart 部件,页脚是footer,属于footerpart 部件,图上还有其他的东西,之后会一一介绍. ...

  8. Java 操作pdf与excel

    java 操作pdf组件  itextpdf <dependency> <groupId>com.itextpdf</groupId> <artifactId ...

  9. itext 生成pdf文件添加页眉页脚

    原文来自:https://www.cnblogs.com/joann/p/5511905.html 我只是记录所有jar版本,由于版本冲突及不兼容很让人头疼的,一共需要5个jar, 其中itextpd ...

随机推荐

  1. 工作流(Workflow)学习---基础知识整理

    工作流定义: 工作流是将一组任务组织起来以完成某个经营过程:定义了任务的触发顺序和触发条件,每个任务可以由一个或多个软件系统完成,也可以由一个或一组人完成,还可以由一个或多个人与软件系统协作完成. 工 ...

  2. 简单的powershell 批量生成身份证复印件

    用Powshell 脚本批量完成,代码比较简单,出生日期,图片我都没有改,图片用于测试,附件. cls; $path = Split-Path -Parent $MyInvocation.MyComm ...

  3. Python用format格式化字符串

    format是是python2.6新增的一个格式化字符串的方法,相对于老版的%格式方法,它有很多优点. 1.不需要理会数据类型的问题,在%方法中%s只能替代字符串类型 2.单个参数可以多次输出,参数顺 ...

  4. Python设计模式——建造者模式

    需求,画人物,要求画一个人的头,左手,右手,左脚,右脚和身体,画一个瘦子,一个胖子 不使用设计模式 #encoding=utf-8 __author__ = 'kevinlu1010@qq.com' ...

  5. 微信获取用户的openid和详细信息

    获取用户的信息的原理,首先用户会点击一个url,这个url会包含一个参数redirect_uri,这个url是指向微信那边的服务器的,然后微信会把这个http请求重定向到redirect_uri,即我 ...

  6. Python 守护进程

    import os import sys from time import sleep try: pid = os.fork() if pid > 0: sys.exit(0) # Exit p ...

  7. CentOS 6.6x64下编译gcc-4.7.4

    最近使用老版本的gcc发现一些问题,于是想尝试升级. 看了一些教程之后进行尝试,发现各类教程均会有一些小问题,于是在此记录一下本人的过程. 编译过程中参考的文章有如下几篇,在此表示感谢: http:/ ...

  8. CMOS (1)–PMOS与NMOS

    1,名称来源 p,n指示的是生成的沟道类型 2,驱动逻辑0与逻辑1 一般用NMOS驱动逻辑0,用PMOS驱动逻辑1.

  9. c++ 联合体

    联合体分配的内存大小是成员变量中最大变量的大小 联合体的成员变量共享内存 小段模式(X86就是) 低位数据存在低地址单元 大端模式                     高位字节存在低地址单元

  10. [unity菜鸟] 笔记1 —— 函数篇

    SendMessage() 调用其他物体中的指令,先在脚本中编写一个自定义的函数,然后使用SendMessage()命令来调用那个物体上的命令 //①将以下函数附给target对象 void Rena ...