package 水印;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.file.Paths;
public class png生成 {
public static void main(String[] args) throws Exception {
String rootPath = "C:\\Users\\86151\\Desktop";
createImage("ssss中华人民共和国",
new Font("宋体", Font.PLAIN, 20),
Paths.get(rootPath, "b" + 4000 + ".png").toFile());
}
private static int[] getWidthAndHeight(String text, Font font) {
Rectangle2D r = font.getStringBounds(text, new FontRenderContext(
AffineTransform.getScaleInstance(1, 1), false, false));
int unitHeight = (int) Math.floor(r.getHeight());//
// 获取整个str用了font样式的宽度这里用四舍五入后+1保证宽度绝对能容纳这个字符串作为图片的宽度
int width = (int) Math.round(r.getWidth()) + 1;
// 把单个字符的高度+3保证高度绝对能容纳字符串作为图片的高度
int height = unitHeight + 3;
System.out.println("width:" + width + ", height:" + height);
return new int[]{width, height};
}
// 根据str,font的样式以及输出文件目录
public static void createImage(String text, Font font, File outFile)
throws Exception {
// 获取font的样式应用在str上的整个矩形
int[] arr = getWidthAndHeight(text, font);
int width = arr[0];
int height = arr[1];
// 创建图片
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_BGR);//创建图片画布
Graphics g = image.getGraphics();
g.setColor(Color.WHITE); // 先用白色填充整张图片,也就是背景
g.fillRect(0, 0, width, height);//画出矩形区域,以便于在矩形区域内写入文字
g.setColor(Color.black);// 再换成黑色,以便于写入文字
g.setFont(font);// 设置画笔字体
g.drawString(text, 0, font.getSize());// 画出一行字符串
g.drawString(text, 0, 2 * font.getSize());// 画出第二行字符串,注意y轴坐标需要变动
g.dispose();
ImageIO.write(image, "png", outFile);// 输出png图片
}
}
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.12</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.13</version>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5-20081211</version>
</dependency>
<!-- word文档水印jar -->
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.0</version>
</dependency>