使用lowagie给pdf添加文字和图片水印

2019-02-12 17:06:56
package com.xian.util;

import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;

/**
 * 给PDF 加水印功能(文字水印和图片水印)
 * 
 * @author yangxp
 */
public class PdfUtil {

    /**
     * 添加图片和文字水印
     * 
     * @param srcFile 待加水印文件
     * @param destFile 加水印后存放地址
     * @param text 加水印的文本内容
     * @param textWidth 文字横坐标
     * @param textHeight 文字纵坐标
     * @param imgFile 加水印图片文件
     * @param imgWidth 图片横坐标
     * @param imgHeight 图片纵坐标
     * @throws IOException
     * @throws DocumentException
     */
    public static void addWaterMark(String srcFile, String destFile, String text, int textWidth, int textHeight,
            String imgFile, int imgWidth, int imgHeight) throws IOException, DocumentException {

        // 待加水印的文件
        PdfReader reader = new PdfReader(srcFile);

        // 加完水印的文件
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(destFile));

        // 设置字体
        BaseFont font = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);

        // PDF总页数
        int total = reader.getNumberOfPages() + 1;

        // 循环对每页插入水印
        PdfContentByte content;
        for (int i = 1; i < total; i++) {

            // 水印在之前文本之上
            content = stamper.getOverContent(i);

            // 图片水印
            if (imgFile != null) {

                Image image = null;
                if (imgFile != null) {

                    image = Image.getInstance(imgFile);
                    image.setAbsolutePosition(imgWidth, imgHeight);

                    // 设置图片的显示大小
                    image.scaleToFit(100, 125);
                }

                content.addImage(image);
            }

            // 文字水印
            if (text != null) {

                content.beginText();

                // 设置颜色 默认为蓝色
                content.setColorFill(Color.BLUE);

                // 设置字体及字号
                content.setFontAndSize(font, 38);

                // 设置起始位置
                content.setTextMatrix(textWidth, textHeight);

                // 中间水印
                content.showTextAligned(Element.ALIGN_LEFT, text, textWidth, textHeight, 45);

                // 底部水印
                for (int k = 0; k < text.length(); k++) {

                    // 距离底边的距离
                    content.setTextRise(10);

                    // 将char转成字符串
                    content.showText(String.valueOf(text.charAt(k)));
                }

                content.endText();
            }
        }

        stamper.close();
    }

    public static void main(String[] args) throws DocumentException, IOException {

        String iconPath = "d:/test/icon/icon.png";
        String srcImgPath = "d:/test/upload/temp/test.pdf";
        String targerPath = "d:/test/upload/file/test.pdf";

        PdfUtil.addWaterMark(srcImgPath, targerPath, "得瑟的ERP", 200, 300, iconPath, 400, 100);
    }

}
  • 2018-03-04 10:15:33

    HTTP代理协议 HTTP/1.1的CONNECT方法

    我们平时使用HTTP协议无非就是GET、POST这些方法,但是HTTP的内容远不止那些。今天就来说说HTTP代理使用的CONNECT。这个不是在网页开发上用的,如果没兴趣就跳过吧。

  • 2018-03-05 11:30:04

    iOS wkwebkit 播放HTML5 视频 全屏问题解决

    使用html5 的video标签播放视频的时候,限制视频的尺寸,在android上是没有问题的,但是在ios上发现,视频没有开始播放的时候还是好的,但是一旦播放开是,就会全屏,非常奇怪。

  • 2018-03-07 14:35:32

    centos7下yum安装ffmpeg

    安装EPEL Release,因为安装需要使用其他的repo源,所以需要EPEL支持 yum install -y epel-release

  • 2018-03-08 09:44:12

    前端性能监控:window.performance

    Web Performance API允许网页访问某些函数来测量网页和Web应用程序的性能,包括 Navigation Timing API和高分辨率时间数据。

  • 2018-03-08 09:44:15

    前端性能监控:window.performance

    Web Performance API允许网页访问某些函数来测量网页和Web应用程序的性能,包括 Navigation Timing API和高分辨率时间数据。

  • 2018-03-08 09:47:14

    ES6,Array.fill()函数的用法

    ES6为Array增加了fill()函数,使用制定的元素填充数组,其实就是用默认内容初始化数组。

  • 2018-03-08 09:53:39

    document.readyState

    一个document 的 Document.readyState 属性描述了文档的加载状态。

  • 2018-03-09 02:09:23

    ArrayBuffer:类型化数组

    ArrayBuffer对象、TypedArray对象、DataView对象是JavaScript操作二进制数据的一个接口。这些对象早就存在,属于独立的规格,ES6将它们纳入了ECMAScript规格,并且增加了新的方法。