java操作pdf添加页眉条码添加水印图片
Post updated by `MetaWeblog API` on Mon Nov 26 2018 23:47:52 GMT+0800 (中国标准时间)
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添加页眉条码添加水印图片的更多相关文章
- 用什么方法给PDF添加页眉页脚
我们所看到的书本中都会设置好有页眉页脚,那么电子书想要添加页眉页脚要怎么操作呢,用什么方法可以在PDF中添加页眉页脚呢,今天就为大家分享一下,如何在电子文件中添加页眉页脚,想知道的小伙伴们就一起来看看 ...
- C# 给现有PDF文档添加页眉、页脚
概述 页眉页脚是一篇完整.精致的文档的重要组成部分.在页眉页脚处,可以呈现的内容很多,如公司名称.页码.工作表名.日期.图片,如LOGO.标记等.在之前的文章中介绍了如何通过新建一页空白PDF页来添加 ...
- ABBYY FineReader 15 如何为PDF文档添加页眉页脚
页眉.页脚是文档页面顶部或底部重复出现的文本信息.很多用户会习惯在文档页面的顶部与底部区域添加页眉.页脚来展现页码.文档标题.作者姓名.品牌名称等附加信息.而ABBYY FineReader 15(W ...
- C#word(2007)操作类--新建文档、添加页眉页脚、设置格式、添加文本和超链接、添加图片、表格处理、文档格式转化
转:http://www.cnblogs.com/lantionzy/archive/2009/10/23/1588511.html 1.新建Word文档 #region 新建Word文档/// &l ...
- ★itext-为pdf文件添加页眉页脚 | 3步完成 |
由于上一篇自定义生成pdf的功能需求又增加了,需要加上页码.所以本博客诞生了~ 1. 通过继承PdfPageEventHelper类,实现需要实现的方法 import com.lowagie.text ...
- 转 Java操作PDF之iText详细入门
转 Java操作PDF之iText详细入门 2016年08月08日 11:06:00 阅读数:19490 iText是著名的开放项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成 ...
- openxml(二) 添加页眉,页脚
openxml 中 word 文档的结构是如下图: 其中,页眉是 header,属于headerpart 部件,页脚是footer,属于footerpart 部件,图上还有其他的东西,之后会一一介绍. ...
- Java 操作pdf与excel
java 操作pdf组件 itextpdf <dependency> <groupId>com.itextpdf</groupId> <artifactId ...
- itext 生成pdf文件添加页眉页脚
原文来自:https://www.cnblogs.com/joann/p/5511905.html 我只是记录所有jar版本,由于版本冲突及不兼容很让人头疼的,一共需要5个jar, 其中itextpd ...
随机推荐
- Linux-ubuntu
在VMware安装ubuntu时磁盘分配如下: /boot 100MB / 2G swap 512MB /home 5G /var 1G /usr 5G+. 设置root权限:sudo passwd ...
- HTML5下通过response header解决跨域AJAX cookie的问题
ajax: 通过给Response Header添加Access-Control-Allow-Origin:* 来解决跨域请求,*代表允许所有的跨域请求,或者把*换成指定的域名 cookie: 服务 ...
- poj 3261 Milk Patterns(后缀数组)(k次的最长重复子串)
Milk Patterns Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7938 Accepted: 3598 Cas ...
- 静态页面参数传递&回调函数写法&快速排序的实现方法
相信很多人都有一种陋习,就是收藏的文章,几乎从来都没有回过头来仔细看过.这次借着这次活动的机会,在<无懈可击的web设计>一书的学习过程中,穿插着讲自己曾经收藏过的,现在觉得还有价值的文章 ...
- 卡牌手游源码《暗黑世界V1.3》数据库表说明文档!!!
原地址:http://blog.csdn.net/uxqclm/article/details/11970761 欢迎来到9秒:www.9miao.com 由于看到论坛中有人询问需求<暗黑世界V ...
- sql replace into 与 insert into
sql replace into用法详细说明 REPLACE的运行与INSERT很相似.只有一点例外,假如表中的一个旧记录与一个用于PRIMARY KEY或一个UNIQUE索引的新记录具有相同的值,则 ...
- ASP.net 的URL路由选择(System.Web.Routing.dll)
System.Web.Routing是.net 3.5sp1中新增的一个dll,用它提拱的类可以很方便的实现url的映射,在asp.net WebFrom的编程中可以使客户端请求的URL变得更加的&q ...
- android gridview按钮边框和定制点击颜色
<?xml version="1.0" encoding="utf-8"?> <GridView xmlns:android="ht ...
- Arrays.sort 与 Collections.sort
代码如下: package com.wangzhu.arrays; import java.util.Arrays; import java.util.Collections; public clas ...
- 排序算法-冒泡排序(Bubble Sort)
package com.wangzhu.sort; /** * 冒泡排序算法 * @ClassName: BubbleSort * @Description: TODO * @author wangz ...