1、图片模板
2、实现代码
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.TextAttribute;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.AttributedString;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.awt.Font;
import java.awt.font.FontRenderContext;
import java.awt.font.LineBreakMeasurer;
import java.awt.font.TextLayout;public class InsertPic {public static final String PATH = "D:\\OA_Temp";public static void MakeCertificateOfAppreciation(String recognizor,String content){try{BufferedImage backgroundImage = ImageIO.read(new File(PATH + "\\templates\\Certificate.jpg"));BufferedImage imageToInsert = ImageIO.read(new File(PATH + "\\templates\\CompanySeal.png"));double scaleFactor = 0.5;int scaledWidth = (int) (imageToInsert.getWidth() * scaleFactor);int scaledHeight = (int) (imageToInsert.getHeight() * scaleFactor);Graphics2D g2d = backgroundImage.createGraphics();g2d.setColor(Color.black); Font font = new Font("SimSun", Font.PLAIN, 20);g2d.setFont(font);FontMetrics fm = g2d.getFontMetrics();g2d.drawString(recognizor, 80, 280);FontRenderContext frc = g2d.getFontRenderContext();AttributedString attributedText = new AttributedString(content);attributedText.addAttribute(TextAttribute.FONT, font);float drawX = 80; float drawY = 310; LineBreakMeasurer measurer = new LineBreakMeasurer(attributedText.getIterator(), frc);while (measurer.getPosition() < content.length()) {TextLayout layout = measurer.nextLayout(backgroundImage.getWidth() - drawX * 2); float y1 = drawY + layout.getAscent();layout.draw(g2d, drawX, y1);drawY += layout.getAscent() + layout.getDescent() + layout.getLeading();}String text = "特此表彰,以资鼓励。";g2d.drawString(text, 80, 580);;text = "这是一个公司名(中国)有限公司";g2d.drawString(text, 650, 600);;Date date = new Date();SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月 ");String formattedDate = sdf.format(date);text = "年 月:"+formattedDate;g2d.drawString(text, 710, 620);g2d.drawImage(imageToInsert.getScaledInstance(scaledWidth, scaledHeight, Image.SCALE_SMOOTH), 680, 580, null);g2d.dispose();sdf = new SimpleDateFormat("yyyyMMdd HHmm ");formattedDate = sdf.format(date);String fileName = formattedDate + "temp.png";ImageIO.write(backgroundImage, "jpg", new File(PATH +"\\attachments\\"+fileName));} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args) {String recognizor = "你好,星期六";String content =" 苍茫的天涯是我的爱 绵绵的青山脚下花正开 什么样的节奏是最呀最摇摆 什么样的歌声才是最开怀 弯弯的河水从天上来 流向那万紫千红一片海 哗啦啦的歌谣是我们的期待 " +"一路边走边唱才是最自在 我们要唱就要唱得最痛快 你是我天边最美的云彩 让我用心把你留下来 留下来 悠悠的唱着最炫的民族风 让爱卷走所有的尘埃 我知道 " +"你是我心中最美的云彩 斟满美酒让你留下来 留下来 永远都唱着最炫的民族风 是整片天空最美的姿态 留下来";MakeCertificateOfAppreciation(recognizor,content);}
}
3、生成模板