Java 在PPT中添加文本水印的簡易方法(單一/平鋪水印)


【前言】

在PPT幻燈片中,可通過添加形狀的方式,來實現類似水印的效果,可添加單一文本水印效果,即在幻燈片中心位置水印以單個文本字樣顯示,但通過一定方法也可以添加多行(平鋪)文本水印效果,即在幻燈片中以一定方式平鋪排列多個文本水印效果到頁面上。上篇文章中介紹了通過C# 程序來添加多行水印效果,本文以Java程序代碼為例介紹如何實現水印添加,包括添加單一文本水印和平鋪文本內水印,代碼供參考。

【程序環境】

本次程序編譯環境為IntelliJ IDEA,JDK版本1.8.0,並引入free spire.presentation.jar3.9.0版本文件。

1. 添加單一文本水印

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.*;
import java.awt.geom.Rectangle2D;

public class TextWatermark {
    public  static void main(String[] args) throws Exception {
        //加載示例文檔
        Presentation ppt = new Presentation();
        ppt.loadFromFile("sample.pptx");

        //獲取指定幻燈片
        ISlide slide = ppt.getSlides().get(0);

        //設置文本水印的寬和高
        int width= 400;
        int height= 300;

        //定義一個長方形區域
        Rectangle2D.Double rect = new Rectangle2D.Double((ppt.getSlideSize().getSize().getWidth() - width) / 2,
                (ppt.getSlideSize().getSize().getHeight() - height) / 2, width, height);

        //添加一個shape到定義區域
        IAutoShape shape = slide.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.setShapeArrange(ShapeAlignmentEnum.ShapeArrange.SendToBack);

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

        //設置文本水印樣式
        textRange.getFill().setFillType(FillFormatType.SOLID);
        textRange.getFill().getSolidColor().setColor(new Color(211,211,211));
        textRange.setFontHeight(50);

        //保存文檔
        ppt.saveToFile("TextWatermark.pptx", FileFormat.PPTX_2013);
        ppt.dispose();
    }
}

單一水印效果:

 

2. 添加多行(平鋪)文本水印

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

public class TextWatermark2 {
    public static void main(String[] args) throws Exception{
        //加載PPT源文檔
        Presentation ppt = new Presentation();
        ppt.loadFromFile("sample.pptx");

        //獲取指定幻燈片
        ISlide slide = ppt.getSlides().get(0);

        //設置文本水印文本寬和高
        int width= 110;
        int height= 80;

        //起始坐標
        float x = 10;
        float y = 40;
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                //繪制文本,設置文本格式並將其添加到第一張幻燈片
                Rectangle2D.Double rect = new Rectangle2D.Double(x,y,width, height);
                IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE, rect);
                shape.getFill().setFillType(FillFormatType.NONE);
                shape.getShapeStyle().getLineColor().setColor(Color.white);
                shape.setRotation(-45);
                shape.getLocking().setSelectionProtection(true);
                shape.getLine().setFillType(FillFormatType.NONE);
                shape.getTextFrame().setText("內部使用");
                shape.setShapeArrange(ShapeAlignmentEnum.ShapeArrange.SendToBack);
                PortionEx textRange = shape.getTextFrame().getTextRange();
                textRange.getFill().setFillType(FillFormatType.SOLID);
                textRange.getFill().getSolidColor().setColor(new Color(238,130,238));
                textRange.setFontHeight(20);
                x += (100 + ppt.getSlideSize().getSize().getWidth()/6);
            }
            x = 30;
            y += (100 + ppt.getSlideSize().getSize().getHeight()/7) ;
        }

        //保存文檔
        ppt.saveToFile("TextWatermark2.pptx", FileFormat.PPTX_2013);
        ppt.dispose();
    }
}

這里通過設置文本寬度值int width的值可實現不同的水印文本字符排列樣式,如下width = 110時,水印字樣效果:

width=60時,水印效果:

width=10時,水印效果:

 

 

【最后】

以上是本次介紹Java添加PPT文本水印的全部內容,添加單一水印和平鋪水印效果的基本方法相差不大。

 

(本文完,如需轉載,請務必注明出處!!!)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM