在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 + "==成功=====");
}
}