咦哟~~~ 2021-08-24 16:58 采纳率: 100%
浏览 356
已结题

XMLSlideShow给ppt加水印图片,水印图片可以添加成功,但是鼠标拖拽水印图片会移动

使用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","钟浩文智慧校区");
    }

```

  • 写回答

1条回答 默认 最新

  • Dadamia 2021-08-25 10:53
    关注

    看是不是能够通过创建母版的方式来添加,大致操作思路:创建幻灯片,创建母版,在母版上添加形状(形状中绘制文字或添加图片),保存文档,参考代码如下(这里采用的免费版Jar包Free Spire.Presentation for Java),

    import com.spire.presentation.*;
    import com.spire.presentation.drawing.BackgroundType;
    import com.spire.presentation.drawing.FillFormatType;
    import com.spire.presentation.drawing.IImageData;
    import com.spire.presentation.drawing.PictureFillType;
    
    import javax.imageio.ImageIO;
    import java.awt.*;
    import java.awt.geom.Rectangle2D;
    import java.awt.image.BufferedImage;
    
    public class TextWatermark {
        public  static void main(String[] args) throws Exception {
            //创建PPT文档(新建的PPT默认包含一张幻灯片),并设置幻灯片大小
            Presentation ppt = new Presentation();
            ppt.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);
    
            //获取第一张母版
            IMasterSlide masterSlide = ppt.getMasters().get(0);
    
            int width= 250;
            int height= 200;
    
            //添加形状到母版
            IAutoShape shape = masterSlide.getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Float((float) (ppt.getSlideSize().getSize().getWidth()-width)/2, (float) ((ppt.getSlideSize().getSize().getHeight() - height) / 2), width, height));
    
            /*//添加文字水印效果
            shape.getTextFrame().setText("水印效果");
            shape.getTextFrame().getTextRange().setFontHeight(50f);
            shape.getTextFrame().getTextRange().getFill().setFillType(FillFormatType.SOLID);
            shape.getTextFrame().getTextRange().getFill().getSolidColor().setColor(new Color(211,211,211));
            shape.getFill().setFillType(FillFormatType.NONE);*/
    
    
            //添加图片水印效果
            shape.getFill().setFillType(FillFormatType.PICTURE);
            shape.getFill().getPictureFill().setFillType(PictureFillType.STRETCH);
            BufferedImage image = ImageIO.read(new File("logo.png"));
            shape.getFill().getPictureFill().getPicture().setEmbedImage(ppt.getImages().append(image));
    
            //形状边框线、形状排列、对齐方式、旋转等格式化设置
            shape.getLine().setFillType(FillFormatType.NONE);
            shape.setShapeArrange(ShapeAlignmentEnum.ShapeArrange.SendToBack);//形状置于底层.
            shape.getTextFrame().getTextRange().getParagraph().setAlignment(TextAlignmentType.CENTER);
            shape.setRotation(-30);
    
            //添加一张幻灯片(创建PPT文档时,已默认生成一张幻灯片,这里添加一张幻灯片可对比查看母版添加效果)
            ppt.getSlides().append();
    
            //保存文档
            ppt.saveToFile("NewWatermark.pptx", FileFormat.PPTX_2013);
            ppt.dispose();
    
        }
    }
    

    文字水印效果:

    img

    图片水印效果:

    img

    在PPT的选择窗格里面如何不能选择对象的话,应该就不能移动水印了吧,你参考上面的内容看能不能满足你要的效果。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 9月2日
  • 已采纳回答 8月25日
  • 创建了问题 8月24日

悬赏问题

  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services