使用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-12-07 22:55:02

    nginx worker_processes 配置

    据另一种说法是,nginx开启太多的进程,会影响主进程调度,所以占用的cpu会增高, 这个说法我个人没有证实,估计他们是开了一两百个进程来对比的吧。

  • 2018-12-08 11:44:26

    php 时间函数strtotime 使用详解

    这篇文章介绍的内容是关于php 时间函数strtotime 使用详解 ,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

  • 2018-12-09 15:52:37

    【Android - 进阶】之Animator属性动画

    在3.0系统之前,Android给我们提供了逐帧动画Frame Animation和补间动画Tween Animation两种动画: 逐帧动画的原理很简单,就是将一个完整的动画拆分成一张张单独的图片,然后将它们连贯起来进行播放; 补间动画是专门为View提供的动画,可以实现View的透明度、缩放、平移和旋转四种效果。

  • 2018-12-09 18:12:45

    显示软键盘,并让布局压缩

    博客 学院 下载 图文课 论坛 APP 问答 商城 VIP会员 活动 招聘 ITeye GitChat 搜博主文章 写博客传资源 原

  • 2018-12-09 22:48:14

    ToolBar修改返回按钮图标

    使用Toolbar时,有时因为不同的手机设备,不能使用系统默认的主题样式或者图标,必须指定特定的资源,防止APP在不同设备上的效果不一样! 我在使用Toolbar时,把这个布局作为一个公共的了,所以修改起来比较容易,下面是该Toolbar的布局文件:

  • 2018-12-09 22:49:12

    Android 修改Toolbar自带的图标颜色

    toolbar自带的按钮颜色是黑色,现在想修改按钮图标颜色,方法如下: 在布局文件中的Toolbar中增加如下2个 属性

  • 2018-12-09 22:50:46

    Toolbar 标题字体大小及字体颜色

    无师无门遇到点破事也得百度,就单单这破问题 Toolbar 标题字体大小及字体颜色,结果百度一推重复没用的结果,要么一推英文的解释,小学学历的我表示很崩溃!