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);
}
}
使用lowagie给pdf添加文字和图片水印
-
cannot cast 'javax.xml.rpc.Call' to 'org.apache.axis.client.Call
用java调用axis报错的解决方案
-
jquery animate 连续点击不能平滑滚动的原因
通过设置延迟的方式解决平滑滚动
-
E45: 'readonly' option is set (add ! to override)
E45: 'readonly' option is set (add ! to override)
-
nginx 安装提示缺少pcre
经查看我的服务器里面有pcre,为什么还提示我缺少pcre。
-
启动Tomcat6.x时,manager does not exist or is not a readable directory-解决
几天重新安装了Tomcat6,安装完Tomcat6后在wepapps下面会有一些tomcat自带的项目(root、manager、host-manager等几个),这几天项目没什么用我就删掉了,后来在MyEclipse中配置好Tomcat,新建web项目,在启动Tomcat是会出现
-
Unable to set localhost. This prevents creation of a GUID
今天部署服务器,莫名其妙的出现了这样的问题,以前从没这情况过。
-
七牛上传图片 open failed: EACCES (Permission denied)
好久不做安卓了,把原来工程的七牛上传图片的代码粘贴过来,运行竟然出错了
-
uri,file,path互相转化
uri,file,path互相转化
-
Android中Uri和Path之间的转换
其实Uri分好多种,下面介绍介绍
-
mogodb 备份
error connecting to db server: server returned error on SASL authentication