itext 5.3.0实现对pdf文件添加(文字和图片)水印

在itext 较新的版本中, 对中文的支持还是存在着问题,在网络上得到的信息和多方尝试下,将字体文件xx.TTF放到项目里面,然后加载到BaseFont 中,可行。如下:
BaseFont font = BaseFont.createFont(path + "MSYH.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

需要jar包:

官网下载的 itext-5.3.0.zip 下的jar包

itextpdf-5.3.0.jar

itext-xtra-5.3.0.jar

不需要对中文支持的扩展jar包,其由上文提到的 加载字体文件到BaseFont中来实现;

具体的加水印代码如下:

package com.sslinm.tools;

import java.io.FileOutputStream;

/**
 * 【功能描述:给PDF 加水印功能,(文字水印和图片水印)】 
 *  【功能详细描述:逐条详细描述功能】
 * 
 * @author 【lfssay】
 * @see 【相关类/方法】
 * @version 【类版本号, 2013-8-20 上午11:22:21】
 * @since 【产品/模块版本】
 */
public class PdfAddWaterMark {
    static Log log = LogFactory.getLog(PdfAddWaterMark.class);

    public static void main(String[] args) throws DocumentException, IOException {
        new PdfAddWaterMark().addWaterMark("E:/tt/1.pdf", "E:/tt/129.pdf", "国家图书馆藏", "E:/tt/1.jpg", 400, 800, 200, 800);
    }

    /**
     * 
     * 【功能描述:添加图片和文字水印】 【功能详细描述:功能详细描述】
     * 
     * @see 【类、类#方法、类#成员】
     * @param srcFile
     *            待加水印文件
     * @param destFile
     *            加水印后存放地址
     * @param text
     *            加水印的文本内容
     * @param imgFile
     *            加水印图片文件
     * @param textWidth
     *            文字横坐标
     * @param textHeight
     *            文字纵坐标
     * @param imgWidth
     *            图片横坐标
     * @param imgHeight
     *            图片纵坐标
     * @throws IOException
     * @throws DocumentException
     */
    public void addWaterMark(String srcFile, String destFile, String text, String imgFile, int textWidth,
            int textHeight, int imgWidth, int imgHeight) throws IOException, DocumentException {
        // 待加水印的文件
        PdfReader reader = new PdfReader(srcFile);
        // 加完水印的文件
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(destFile));

        int total = reader.getNumberOfPages() + 1;
        PdfContentByte content;
        String path = this.getClass().getResource("/").getPath();
        // 设置字体
        // BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
        // BaseFont.EMBEDDED);
        //加载字库来完成对字体的创建
        BaseFont font = BaseFont.createFont(path + "MSYH.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        // BaseFont base2 = BaseFont.createFont(BaseFont.HELVETICA,
        // BaseFont.WINANSI, BaseFont.EMBEDDED);
        // 水印文字
        String waterText = text;
        Image image = null;
        if (!StringUtils.isBlank(imgFile)) {
            image = Image.getInstance(imgFile);
            image.setAbsolutePosition(imgWidth, imgHeight);
            // 设置图片的显示大小
             image.scaleToFit(100, 125);
        }
        int j = waterText.length(); // 文字长度
        char c = 0;
        int high = 0;// 高度
        // 循环对每页插入水印
        for (int i = 1; i < total; i++) {
            // 水印的起始
            high = 50;
            // 水印在之前文本之上
            content = stamper.getOverContent(i);
            if (image != null) {
                content.addImage(image);
            }

            if (!StringUtils.isBlank(text)) {
                // 开始
                content.beginText();
                // 设置颜色 默认为蓝色
                content.setColorFill(BaseColor.BLUE);
                // content.setColorFill(Color.GRAY);
                // 设置字体及字号
                content.setFontAndSize(font, 38);
                // 设置起始位置
                // content.setTextMatrix(400, 880);
                content.setTextMatrix(textWidth, textHeight);
                // 开始写入水印
                content.showTextAligned(Element.ALIGN_LEFT, text, textWidth, textHeight, 45);
                // for (int k = 0; k < j; k++) {
                // content.setTextRise(14);
                // c = waterText.charAt(k);
                // // 将char转成字符串
                // content.showText(c + "");
                // high -= 5;
                // }
                content.endText();
            }
        }
        stamper.close();
        log.info("===" + srcFile + "===添加水印到==" + destFile + "==成功=====");
    }
}


  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值