使用lowagie给pdf添加文字和图片水印
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);
}
}
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);
}
}
-
最新LNMP(linux+nginx+mysql+php+laravel)服务器环境配置教程四
通过nginx运行php服务返回phpinfo()信息。
-
windows下如何github ssh 公钥
windows下如何github ssh 公钥
-
如何查看git项目在哪个路径
如何查看git项目在哪个路径
-
使用IntelliJ IDEA 14和Maven创建java web项目
使用IntelliJ IDEA 14和Maven创建java web项目
-
两列布局——左侧宽度固定,右侧宽度自适应的两种方法
关于左侧宽度固定,右侧宽度自适应两列布局的一种很常用的方法我相信大家都知道。就是利用左侧元素浮动,或者绝对定位的方式使其脱离常规文档流,让两个块级元素能够在同一行显示。然后右侧元素 margin-left 的值等于左侧元素宽度,这时右侧元素将紧挨着左侧元素
-
onhashchange监听url,hash变化
onhashchange监听url,hash变化
-
js 判断字符串是否包含某字符串,String对象中查找子字符,indexOf
js 判断字符串是否包含某字符串,String对象中查找子字符,indexOf
-
Git:代码冲突常见解决方法
如果系统中有一些配置文件在服务器上做了配置修改,然后后续开发又新添加一些配置项的时候, 在发布这个配置文件的时候,会发生代码冲突:
-
linux学习之——vim简明教程
学习 vim 并且其会成为你最后一个使用的文本编辑器。没有比这个更好的文本编辑器了,非常地难学,但是却不可思议地好用。 我建议下面这四个步骤: 存活 感觉良好 觉得更好,更强,更快 使用VIM的超能力