在前文中,我介绍过如何给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);    }}

增加成果:

示例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);    }}

增加成果:

示例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);    }}

(本文完)