Java 添加、删除PPT文本和图片水印

在前文中,我介绍过如何给PDF文档添加文本和图片水印。本文将详细讲解如何添加、删除PPT文本和图片水印。

此次代码示例所用到的工具是Free Spire.Presentation for Java。Free Spire.Presentation for Java 是一个专业的PowerPoint API,它允许开发人员在Java应用程序中创建、读取、写入、转换和保存PowerPoint文档。同时,作为一款独立的Java组件,其运行环境无需安装Microsoft PowerPoint。

操作步骤:

通过官网下载获取Jar包,解压后将lib文件夹下的Spire.Presentation.jar手动导入Java程序。

另外,也可通过maven仓库安装产品及导入相关依赖包。具体安装详情参见此教程

代码示例

示例1 添加图片水印

import com.spire.presentation.*;
import com.spire.presentation.drawing.*;
import javax.imageio.ImageIO;
import java.io.File;
public class AddImageWatermark {
    public static void main(String[] args) throws Exception {
        //加载示例文档
        Presentation presentation = new Presentation();
       presentation.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pptx");

        //获取水印图片
        File file =new File("C:\\Users\\Test1\\Desktop\\logo.png");
        IImageData image = presentation.getImages().append(ImageIO.read(file));

        //获取幻灯片背景属性,设置图片填充
        presentation.getSlides().get(0).getSlideBackground().setType(BackgroundType.CUSTOM);
        presentation.getSlides().get(0).getSlideBackground().getFill().setFillType(FillFormatType.PICTURE);
        presentation.getSlides().get(0).getSlideBackground().getFill().getPictureFill().setFillType(PictureFillType.STRETCH);
        presentation.getSlides().get(0).getSlideBackground().getFill().getPictureFill().getPicture().setEmbedImage(image);

        //保存文档
        presentation.saveToFile("output/addImageWatermark.pptx", FileFormat.PPTX_2013);
    }
}

添加效果:

image

示例2 添加文本水印

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.*;
import java.awt.geom.Rectangle2D;

public class AddTextWatermark {
    public static void main(String[] args) throws Exception {
        //创建presentation对象并加载示例文档
        Presentation presentation = new Presentation();
        presentation.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pptx");
        //设置文本水印的宽和高
        int width= 400;
        int height= 300;

        //定义一个长方形区域
        Rectangle2D.Double rect = new Rectangle2D.Double((presentation.getSlideSize().getSize().getWidth() - width) / 2,
                (presentation.getSlideSize().getSize().getHeight() - height) / 2, width, height);

        //添加一个shape到定义区域
        IAutoShape shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, rect);

        //设置shape样式
        shape.getFill().setFillType(FillFormatType.NONE);
        shape.getShapeStyle().getLineColor().setColor(Color.white);
        shape.setRotation(-45);
        shape.getLocking().setSelectionProtection(true);
        shape.getLine().setFillType(FillFormatType.NONE);

        //添加文本到shape
        shape.getTextFrame().setText("内部使用");
        PortionEx textRange = shape.getTextFrame().getTextRange();

        //设置文本水印样式
        textRange.getFill().setFillType(FillFormatType.SOLID);
        textRange.getFill().getSolidColor().setColor(Color.pink);
        textRange.setFontHeight(50);

        //保存文档
        presentation.saveToFile("output/addTextWatermark.pptx", FileFormat.PPTX_2010);
    }
}

添加效果:

image

示例3 删除图片和文本水印

package Watermark;
import com.spire.presentation.*;
import com.spire.presentation.drawing.*;
public class DeleteWatermark {
    public static void main(String[] args) throws Exception {
        //加载示例文档
        Presentation presentation = new Presentation();
        presentation.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pptx");

        //移除文本水印
        for (int i = 0; i < presentation.getSlides().getCount(); i++)
        {
            for (int j = 0; j < presentation.getSlides().get(i).getShapes().getCount(); j++)
            {
                if (presentation.getSlides().get(i).getShapes().get(j) instanceof IAutoShape)
                {
                    IAutoShape shape = (IAutoShape)presentation.getSlides().get(i).getShapes().get(j);
                    if (shape.getTextFrame().getText().contains("内部使用"))
                    {
                        presentation.getSlides().get(i).getShapes().remove(shape);
                    }
                }
            }
        }

        //移除图片水印
        for (int i = 0; i < presentation.getSlides().getCount(); i++)
        {
            presentation.getSlides().get(i).getSlideBackground().getFill().setFillType(FillFormatType.NONE);
        }

        //保存文档
        presentation.saveToFile("output/removeTextOrImageWatermark.pptx", FileFormat.PPTX_2013);
    }
}

(本文完)

你可能感兴趣的:(java,ppt,水印)