使用poi的XMLSlideShow给ppt幻灯片添加图片水印,图片水印添加无异常,但是打开加好的文档,鼠标可以对水印进行拖拽,有没有大 佬做过类似的功能,能否设置水印图片让它不能被拖拽,谢谢了
```java
import org.apache.poi.sl.usermodel.PictureData;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFPictureShape;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 水印工具类
*/
public class WaterMarkUtils {
// 水印透明度
private static float alpha = 0.5f;
// 水印之间的间隔
private static final int XMOVE = 200;
// 水印之间的间隔
private static final int YMOVE = 200;
/**
* PPT设置水印
*
* @param path
* @param targetpath
* @param markStr
* @throws IOException
*/
public static boolean setPPTWaterMark(String path, String targetpath, String markStr) {
boolean flag = true;
XMLSlideShow slideShow = null;
try {
slideShow = new XMLSlideShow(new FileInputStream(path));
} catch (IOException e) {
e.printStackTrace();
flag = false;
}
ByteArrayOutputStream os = null;
FileOutputStream out = null;
try {
//获取水印
os = getImage(markStr);
PictureData pictureData1 = slideShow.addPicture(os.toByteArray(), PictureData.PictureType.PNG);
for (XSLFSlide slide : slideShow.getSlides()) {
XSLFPictureShape pictureShape = slide.createPicture(pictureData1);
//pictureShape.setAnchor(new java.awt.Rectangle(250, 0, 500, 500));
pictureShape.setAnchor(pictureShape.getAnchor());
}
out = new FileOutputStream(targetpath);
slideShow.write(out);
} catch (IOException e) {
e.printStackTrace();
flag = false;
} finally {
if (slideShow != null) {
try {
slideShow.close();
} catch (IOException e) {
e.printStackTrace();
flag = false;
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
flag = false;
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
flag = false;
}
}
}
return flag;
}
/**
* 获取水印文字图片流
*
* @param text
* @return
*/
private static ByteArrayOutputStream getImage(String text) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
// 导出到字节流B
BufferedImage image = createWaterMarkImageBig(text);
ImageIO.write(image, "png", os);
} catch (IOException e) {
e.printStackTrace();
}
return os;
}
/**
* 根据文字生成水印图片(大号 平铺)
*
* @param text
* @return
*/
public static BufferedImage createWaterMarkImageBig(String text) {
Integer width = 1000;
Integer height = 800;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);// 获取bufferedImage对象
Font font = new Font("宋体", Font.PLAIN, 35);
Graphics2D g2d = image.createGraphics();
image = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
g2d.dispose();
g2d = image.createGraphics();
//设置字体颜色和透明度
g2d.setColor(new Color(0, 0, 0, 60));
//设置字体
g2d.setStroke(new BasicStroke(1));
//设置字体类型 加粗 大小
g2d.setFont(font);
//设置倾斜度
g2d.rotate(Math.toRadians(-30), (double) image.getWidth() / 2, (double) image.getHeight() / 2);
FontRenderContext context = g2d.getFontRenderContext();
Rectangle2D bounds = font.getStringBounds(text, context);
double x = (width - bounds.getWidth()) / 2;
double y = (height - bounds.getHeight()) / 2;
double ascent = -bounds.getY();
double baseY = y + ascent;
//写入水印文字原定高度过小,所以累计写水印,增加高度
g2d.drawString(text, (int) x, (int) baseY);
//设置透明度
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
//释放对象
g2d.dispose();
return image;
}
public static void main(String[] args) {
setPPTWaterMark("C:\\Users\\Administrator\\Desktop\\112233.pptx","C:\\Users\\Administrator\\Desktop\\ceshi2.pptx","钟浩文智慧校区");
}
```