使用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);
}
}
-
mysql自增ID起始值修改方法
本文介绍mysql自增ID的起始值修改与设置方法
-
Linux下查找大文件,大目录的方法
find . -type f -size +800M12
-
Mr.Smile填坑记——AndroidStudio3.0 Can
javaCompileOptions { annotationProcessorOptions { includeCompileClasspath = true } } } ...}
-
正则提取字段
如下文案,如何提取中间的文案呢 eq: 我们的%%aaa%%不一致,哈哈哈 提取后是aaa
-
Laravel ENV—— 环境变量的加载与源码解析
laravel 在启动时,会加载项目的 env 文件,本文将会详细介绍 env 文件的使用与源码的分析。
-
Urlconnection对象获得inputStream值
Urlconnection对象获得inputStream值
-
String和inputstream互转
String和inputstream互转
-
xUtils3.0版本的发送同步网络请求的方式
对于Android开发来说,基本都是用异步来从网络上请求数据,很少用到同步请求的。近日项目有个地方需要使用到同步请求(以我目前的知识储备来说好像只能用同步请求来解决这个问题了),去网上搜索相关资料,又没有找到什么明确的使用方法。所以记下来,以备不时之需。
-
Retrofit 设置 超时时间
今天开发的时候遇到一个网络请求超时的问题,后台处理是成功的,但是移动端返回的总是提示请求超时,在设置了retrofit请求超时的时间延长以后,就可以请求成功了,下面是配置的方法:
-
laravel 5.5 打印sql,查询慢sql
laravel 5.5,打印sql,查询慢sql