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. 代码
  1. import java.awt.image.BufferedImage;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import javax.imageio.ImageIO;
  9. import org.apache.commons.io.IOUtils;
  10. import org.jbarcode.JBarcode;
  11. import org.jbarcode.encode.Code39Encoder;
  12. import org.jbarcode.encode.InvalidAtributeException;
  13. import org.jbarcode.paint.BaseLineTextPainter;
  14. import org.jbarcode.paint.WideRatioCodedPainter;import com.lowagie.text.BadElementException;
  15. import com.lowagie.text.DocumentException;
  16. import com.lowagie.text.Image;
  17. import com.lowagie.text.Rectangle;
  18. import com.lowagie.text.pdf.PdfContentByte;
  19. import com.lowagie.text.pdf.PdfGState;
  20. import com.lowagie.text.pdf.PdfReader;
  21. import com.lowagie.text.pdf.PdfStamper;
  22.  
  23. /**
  24. * pdf工具
  25. *
  26. * @author 宋杰
  27. * @date 2016-07-11 13:19:33
  28. */
  29. public class PdfUtils {
  30. /**
  31. * 添加条码
  32. * @param str 条码内容
  33. * @param filepath pdf 文件绝对路径
  34. * @param l_height 水平位置
  35. * @param l_weight 垂直位置
  36. */public static void addString(String str,String filepath,int l_height,int l_weight){
  37. BufferedImage localBufferedImage=null;
  38. JBarcode jbcode = null;
  39. try {
  40. //1.创建条码图像
  41. jbcode = new org.jbarcode.JBarcode(Code39Encoder.getInstance(), WideRatioCodedPainter.getInstance(),BaseLineTextPainter.getInstance());
  42. localBufferedImage = jbcode.createBarcode(str);
  43. ByteArrayOutputStream bao= new ByteArrayOutputStream();
  44. ImageIO.write(localBufferedImage, "png", bao);
  45. Image img = Image.getInstance(bao.toByteArray());
  46. img.setAlignment(1); //居中显示
  47. img.setAbsolutePosition(l_height, l_weight);//显示位置,根据需要调整
  48. img.scalePercent(60); //显示为原条形码图片大小的比例,百分比
  49. //2.创建pdf输入输出流
  50. InputStream is = new FileInputStream(filepath);
  51. PdfReader reader = new PdfReader(is);
  52. OutputStream os = new FileOutputStream(filepath);
  53. PdfStamper stamp = new PdfStamper(reader, os);
  54. PdfContentByte contentByte = null;
  55. int n = reader.getNumberOfPages();
  56. //3. 设置透明度
  57. PdfGState gs = new PdfGState();
  58. gs.setFillOpacity(0.7f);
  59. gs.setStrokeOpacity(0.7f);
  60.  
  61. //4.在pdf每页右上角添加条码
  62. for (int i = 1; i <= n; i++){
  63. contentByte = stamp.getOverContent(i); // getOverContent 水印会把正文盖住 getUnderContent 水印会被正文的图片盖住
  64. contentByte.setGState(gs);
  65. contentByte.addImage(img);
  66. //contentByte.addImage(Image.getInstance("D:/primeton/yunda/ide/eclipse/workspace/ydsoa/com.yd.soa.budget/src/webcontent/comm/logo.jpg"));
  67. }
  68. //5.关闭所有输入输出
  69. reader.close();
  70. stamp.close();
  71. IOUtils.closeQuietly(bao);
  72. IOUtils.closeQuietly(is);
  73. IOUtils.closeQuietly(os);
  74. } catch (InvalidAtributeException e) {
  75. e.printStackTrace();
  76. } catch (IOException e) {
  77. e.printStackTrace();
  78. } catch (BadElementException e) {
  79. e.printStackTrace();
  80. } catch (DocumentException e) {
  81. e.printStackTrace();
  82. }
  83. }
  84.  
  85. /**
  86. * 添加图片水印 居中
  87. * @param imagepath 图片文件绝对路径
  88. * @param filepath pdf 文件绝对路径
  89. */public static void addWaterImage(String imagepath,String filepath){
  90. InputStream is = null;
  91. PdfReader reader = null;
  92. OutputStream os = null;
  93. PdfStamper stamp = null;
  94. try {
  95. //1.创建pdf输入输出流
  96. is = new FileInputStream(filepath);
  97. reader = new PdfReader(is);
  98. os = new FileOutputStream(filepath);
  99. stamp = new PdfStamper(reader, os);
  100.  
  101. PdfContentByte contentByte = null;
  102. int n = reader.getNumberOfPages();
  103. //2. 设置透明度
  104. PdfGState gs = new PdfGState();
  105. gs.setFillOpacity(0.5f);
  106. gs.setStrokeOpacity(0.5f);
  107. //3. 读取图片
  108. Image logo = Image.getInstance(imagepath);
  109. //4.在pdf每页右上角添加条码
  110. for (int i = 1; i <= n; i++){
  111. contentByte = stamp.getUnderContent(i); // getOverContent 水印会把正文盖住 getUnderContent 水印会被正文的图片盖住
  112. contentByte.setGState(gs);
  113. Rectangle rectangle = reader.getPageSize(i);
  114. float width = rectangle.getWidth();
  115. float height = rectangle.getHeight();
  116. logo.setAbsolutePosition(width/2-logo.getWidth()/2, height/2);
  117. contentByte.addImage(logo);
  118. }
  119. } catch (IOException e) {
  120. e.printStackTrace();
  121. } catch (BadElementException e) {
  122. e.printStackTrace();
  123. } catch (DocumentException e) {
  124. e.printStackTrace();
  125. }finally{
  126. //5.关闭所有输入输出
  127. reader.close();
  128. try {
  129. stamp.close();
  130. } catch (DocumentException e) {
  131. } catch (IOException e) {
  132. }
  133. IOUtils.closeQuietly(is);
  134. IOUtils.closeQuietly(os);
  135. }
  136. }
  137.  
  138. public static void main(String[] args){
  139. addString("YDHT-CG-20160525-001", "D:/111.pdf", 400, 795);
  140. addWaterImage("com.yd.soa.budget/src/webcontent/comm/logo.jpg", "D:/111.pdf");
  141.  
  142. }
  143. }

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. Oracle OEM重建

    参考博客:http://blog.csdn.net/tianlesoftware/article/details/4702978 测试环境: C:\Users\Administrator>sql ...

  2. Javascript 5种方法实现过滤删除前后所有空格

    第一种:循环检查替换 //供使用者调用 function trim(s){ return trimRight(trimLeft(s)); } //去掉左边的空白 function trimLeft(s ...

  3. iOS 7隐藏statusbar

    - (BOOL)prefersStatusBarHidden { return YES; } [[UIApplication sharedApplication] setStatusBarHidden ...

  4. responsive web design

    http://d.alistapart.com/responsive-web-design/ex/ex-site-flexible.html http://alistapart.com/article ...

  5. const变量的存储区及修改权限

    转自const变量的存储区及修改权限 [cpp] view plaincopy const int a = 1; int *p = const_cast<int*>(&a); *p ...

  6. Percona Xtrabackup备份mysql全库及指定数据库(完整备份与增量备份)

    原文地址:http://www.tuicool.com/articles/RZRnq2 Xtrabackup简介 Percona XtraBackup是开源免费的MySQL数据库热备份软件,它能对In ...

  7. HDU 5008 Boring String Problem

    题意:给定一个串长度<=1e5,将其所有的不同的字串按照字典序排序,然后q个询问,每次询问字典序第k小的的起始坐标,并且起始坐标尽量小. 分析: 一开始看错题意,没有意识到是求不同的字串中第k小 ...

  8. Java集合类之HashMap

    package com.test; import java.util.*; public class Demo7_3 { public static void main(String[] args) ...

  9. vs2012布局问题

    问题背景: 北大青鸟ASP.Net视频中,老师提到可以通过更改属性PageLayout的值,来实现页面布局由默认的FlowLayout(流式布局)而成为GridLayout(网格布局),即系统控件安排 ...

  10. C++文件读写总结

    在C++中如何实现文件的读写? 作者: infobillows 发表日期: 2007-04-03 21:33 点击数: 465 一.ASCII 输出 为了使用下面的方法, 你必须包含头文件<fs ...