使用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);
}
}
-
(用注解)springboot 整合缓存(Ehcache或者reids)
这里介绍Spring Boot结合JPA,MySQL和Ehcache实现缓存功能,提高程序访问效率。
-
(没有用注解)springboot+EHcache 实现文章浏览量的缓存和超时更新
这篇文章主要介绍了springboot+EHcache 实现文章浏览量的缓存和超时更新,问题描述和解决思路给大家介绍的非常详细,需要的朋友可以参考下
-
Java.io.tmpdir介绍
System.getproperty(“java.io.tmpdir”)是获取操作系统缓存的临时目录,不同操作系统的缓存临时目录不一样,
-
Ehcache配置持久化到硬盘,只存储到硬盘
Ehcache默认配置的话 为了提高效率,所以有一部分缓存是在内存中,然后达到配置的内存对象总量,则才根据策略持久化到硬盘中,这里是有一个问题的,假如系统突然中断运行 那内存中的那些缓存,直接被释放掉了,不能持久化到硬盘;这种数据丢失,对于一般项目是不会有影响的,但是对于我们的爬虫系统,我们是用来判断重复Url的,所以数据不能丢失;
-
put与putIfAbsent区别
put在放入数据时,如果放入数据的key已经存在与Map中,最后放入的数据会覆盖之前存在的数据, 而putIfAbsent在放入数据时,如果存在重复的key,那么putIfAbsent不会放入值。
-
Java对象的序列化与反序列化-Json篇
下面是各种对象的序列化和反序列化,但是依然不能解决session的本地缓存问题
-
在navicat中查看所有表的注释
点击工具栏中的查看按钮,选择详细信息如何再选择列 如何选择注释
-
软件测试的相关网站
只能说,留着这些网站,后期可能有用,可能会持续更新。
-
程序员实用工具网站
程序员开发需要具备良好的信息检索能力,为了备忘(收藏夹真是满了),将开发过程中常用的网站进行整理。
-
在 idea 中根据 eslint 格式化代码
在 idea 中根据 eslint 格式化代码