java写代码加水印 java添加代码( 二 )


int x, int y) {
try {
//目标文件
File _file = new File(targetImg);
Image src = https://www.04ip.com/post/ImageIO.read(_file);
int wideth = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(wideth, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
g.drawImage(src, 0, 0, wideth, height, null);
//水印文件
File _filebiao = new File(pressImg);
Image src_biao = ImageIO.read(_filebiao);
int wideth_biao = src_biao.getWidth(null);
int height_biao = src_biao.getHeight(null);
g.drawImage(src_biao, (wideth - wideth_biao) / 2,
(height - height_biao) / 2, wideth_biao, height_biao, null);
//水印文件结束
g.dispose();
FileOutputStream out = new FileOutputStream(targetImg);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 打印文字水印图片
*
* @param pressText
*--文字
* @param targetImg --
*目标图片
* @param fontName --
*字体名
* @param fontStyle --
*字体样式
* @param color --
*字体颜色
* @param fontSize --
*字体大小
* @param x --
*偏移量
* @param y
*/
public static void pressText(String pressText, String targetImg,
String fontName, int fontStyle, int color, int fontSize, int x,
int y) {
try {
File _file = new File(targetImg);
Image src = https://www.04ip.com/post/ImageIO.read(_file);
int wideth = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(wideth, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
g.drawImage(src, 0, 0, wideth, height, null);
// String s="";
g.setColor(Color.RED);
g.setFont(new Font(fontName, fontStyle, fontSize));
g.drawString(pressText, wideth - fontSize - x, height - fontSize
/ 2 - y);
g.dispose();
FileOutputStream out = new FileOutputStream(targetImg);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) {
pressImage("F:/logo.png","F:/123.jpg", 0, 0);
}
}
java 如何给pdf文件加水印可以使用Spire.PDF for Java通过Java来添加水印 。
首先,您需要在 Java 程序中添加 Spire.Pdf.jar 文件作为依赖项 。您可以从这个链接下载 JAR 文件;如果您使用 Maven , 则可以通过在 pom.xml 文件中添加以下代码导入 JAR 文件 。
repositories
repository
idcom.e-iceblue/id
url;/url
/repository/repositoriesdependencies
dependency
groupIde-iceblue/groupId
artifactIdspire.pdf/artifactId
version5.3.1/version
/dependency/dependencies
1.添加图片水印
代码如下:
import com.spire.pdf.*;
import java.awt.geom.Rectangle2D;
public class watermark {
public static void main(String[] args) {
//加载PDF文档
PdfDocument doc = new PdfDocument();
doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.pdf");
//获取第一页
PdfPageBase page = doc.getPages().get(0);
//设置背景图片
page.setBackgroundImage("C:\\Users\\Administrator\\Desktop\\logo.png");
//设置背景区域
Rectangle2D.Float rect = new Rectangle2D.Float();
rect.setRect(280, 300, 150, 150);
page.setBackgroundRegion(rect);
//保存文档
doc.saveToFile("output/imageWaterMark.pdf");
doc.close();
}
}
2.添加文本水印

推荐阅读