使用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);
    }

}
  • 2020-01-07 10:06:00

    vue重定向beforeRouterEnter与replace的使用

    一个登录页面,只有第一次才会出现,一旦出现过,以后再也不会出现,即使是输入该页面的url也不会跳转到该页面,只会跳转到指定的其他页面;

  • 2020-01-07 10:14:07

    vue强制刷新组件 销毁和重建

    很多时候,通过重置数据将页面重置时,子组件可以提供重置的方法,或者提供props重置自己的状态。但是相对麻烦,那可以使用强制刷新来实现刷新组件。

  • 2020-01-07 10:17:42

    vue钩子函数beforeRouteUpdate没有反应

    由于会渲染同样的 Foo 组件,因此组件实例会被复用。而这个钩子就会在这个情况下被调用。 可以访问组件实例 `this`

  • 2020-01-08 13:23:24

    vue 中使用eventbus

    为了提高组件的独立性和重用性,父组件会通过props向下传数据给子组件,当子组件又事情要告诉父组件时用通过$emit事件告诉父组件,如此确保每个组件都是独立在相对隔离的环境中运行,可以大幅度提高组件的可维护性

  • 2020-01-08 13:30:30

    vue中eventbus被多次触发(vue中使用eventbus踩过的坑)

    一开始的需求是这样子的,我为了实现两个页面组件之间的数据传递,假设我有页面A,点击页面A上的某一个按钮之后,页面会自动跳转到页面B,同时我希望将页面A上的某一些参数携带过去给页面B。 然后我就想,这不就是不同组件之间的数据传递问题而已吗?直接用bus 巴士事件来传递数据不就行了吗。于是,我就很愉快地进行了。关于vue中的eventbus的使用,我之前在一篇vue中的数据传递中有提到过。

  • 2020-01-08 22:03:07

    修改MAC系统下默认PHP版本

    今天在使用mac时遇到了一个问题,因为需要composer拉取laravel5.6,但是提示我php版本过低,但是我本人使用的是集成环境MAMP,已经切换了php7.2的版本,这个为什么没有生效呢?经检查是因为composer检测得是mac下环境变量生效的php版本