PDF水印接口设计
水印元素的样式
{字体、 字号、 颜色、 深浅、 排列、 角度、透明度}
一、需要依赖
com.itextpdf
itextpdf
${itextpdf.version}
二、字体设计
将ttf格式的字体文件,预置到系统内,然后通过此方法读取,加载成代码可用字体,中英文和汉字最好不用同一套字体,否则很丑
/**
* 判断是否包含中文
* @param text
* @return
*/
public static boolean isChinese(String text){
Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
Matcher m = p.matcher(text);
if (m.find()) {
return true;
}
return false;
}
/**
* 判断是不是中文,拿不同的字体
* @param isChinese
* @return
*/
private static BaseFont getFont(boolean isChinese){
BaseFont base = null;
try {
// 设置字体
if (isChinese) {
base = BaseFont.createFont(CN_FONT_NAME, CN_FONT_ENCODING, BaseFont.NOT_EMBEDDED);
} else {
base = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
}
} catch (Exception e) {
logger.error("水印构建字体出错:",e);
e.printStackTrace();
}
return base;
}
三、