Java 生成一张带文字的图片,并写入Word文档

今天收到一个需求,要做一个报表的功能。

如果是图表,比如柱状图、折线图、饼图之类的,用jfreechart就可以做到了,但是有些图是纯文字的,这就需要做到将文字放到一张图片中。

搜了很多,总算找到一篇能达到效果的,这里做个记录。

参考链接:

https://www.dandelioncloud.cn/article/details/1533801141372743682

https://blog.csdn.net/u014641168/article/details/125338018

先上图:

		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>3.15</version>
		</dependency>
		<dependency>
			<groupId>org.dom4j</groupId>
			<artifactId>dom4j</artifactId>
			<version>2.0.0</version>
		</dependency>

实现其实也很简单

package test;


import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

import javax.imageio.ImageIO;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.dom4j.DocumentException;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class Demo{

	// 生成Word
	public static void createXWPFDocument(BufferedImage image)
			throws IOException, DocumentException, InvalidFormatException {
		XWPFDocument doc = new XWPFDocument();
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ImageIO.write(image, "gif", baos);
		InputStream is = new ByteArrayInputStream(baos.toByteArray());

		XWPFParagraph paragraph = doc.createParagraph();
		XWPFRun run = paragraph.createRun();
		run.addPicture(is, XWPFDocument.PICTURE_TYPE_PNG, "图片1", 200, 140);

		OutputStream os = new FileOutputStream("D:\\picture\\" + UUID.randomUUID().toString().substring(28) + ".docx");
		// 把doc输出到输出流
		doc.write(os);
	}

	// 生成图片
	private static void createImage(String fileLocation, BufferedImage image) {
		try {
			FileOutputStream fos = new FileOutputStream(fileLocation);
			BufferedOutputStream bos = new BufferedOutputStream(fos);
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
			encoder.encode(image);
			bos.close();
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws Exception {
		int imageWidth = 330;// 图片的宽度
		int imageHeight = 200;// 图片的高度
		BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
		Graphics graphics = image.getGraphics();
		graphics.setColor(Color.WHITE);
		graphics.fillRect(0, 0, imageWidth, imageHeight);
		graphics.setColor(Color.BLACK);
		int high = 50;
		int wigth = 20;

		graphics.setFont(new Font("宋体", Font.BOLD, 20));
		graphics.drawString("正在连接主机台数", wigth, high);
		high += 10;

		graphics.drawLine(20, high, 310, high);
		high += 30;
		graphics.setColor(Color.GRAY);
		graphics.setFont(new Font("宋体", Font.BOLD, 15));
		graphics.drawString("数据刷新于:2022-10-12 20:52:41", wigth, high);
		high += 30;

		graphics.drawString("统计", wigth, high);
		wigth += 50;
		graphics.setColor(Color.BLACK);
		graphics.setFont(new Font("宋体", Font.BOLD, 30));
		graphics.drawString("7", wigth, high);
		wigth += 20;
		graphics.setColor(Color.GRAY);
		graphics.setFont(new Font("宋体", Font.BOLD, 15));
		graphics.drawString("人", wigth, high);
		wigth = 20;
		high += 20;

		graphics.drawString("同比:", wigth, high);
		wigth += 50;
		graphics.setColor(Color.RED);
		graphics.drawString("99.99%", wigth, high);
		graphics.setColor(Color.GRAY);
		wigth = 20;
		high += 20;

		graphics.drawString("环比:", wigth, high);
		wigth += 50;
		graphics.setColor(Color.GREEN);
		graphics.drawString("99.99%", wigth, high);

		// createImage("E:\\picture\\"+UUID.randomUUID().toString().substring(25)+".jpg",
		// image);
		createXWPFDocument(image);
	}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值