关于java:Java-添加删除PPT文本和图片水印

4次阅读

共计 3556 个字符,预计需要花费 9 分钟才能阅读完成。

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

(本文完)

正文完
 0