LoginSignup
0
0

More than 3 years have passed since last update.

Java  PowerPointにウォーターマークを追加・削除

Posted at

ウォーターマークとは静止画像や動画の盗用を防ぐため、著作権を表示する文字やロゴ、図のことです。今回はSpire.Presentation for Javaを利用してJavaでPowerPointに画像のウォーターマークと文字のウォーターマークを追加・削除する方法を紹介します。

下準備

1.E-iceblueの公式サイトからFree Spire. Presentation for Java無料版をダウンロードしてください。

f:id:lendoris:20210106113230p:plain

2.IDEを起動して新規プロジェクトを作成してから、インストールされたファイルにあった相応しいSpire. Presentation.jarを参照に追加してください。

 f:id:lendoris:20210106113242p:plain

元のファイル

f:id:lendoris:20210106113408p:plain

文字のウォーターマーク

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

public class watermark {
    public static void main(String[] args) throws Exception {

        //presentation objectを作成します。 
        Presentation presentation = new Presentation();
        presentation.loadFromFile("C:\\Users\\Administrator\\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/result.pptx", FileFormat.PPTX_2010);
    }

}

完成例

f:id:lendoris:20210106113430p:plain

画像のウォーターマーク

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("Sample.pptx");

        //画像のウォーターマークを取得します。

        File file =new File("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("addImageWatermark.pptx";, FileFormat.PPTX_2013);
    }
}

完成例

f:id:lendoris:20210106113546p:plain

ウォーターマークを削除するコード

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

public class removeTextOrImageWatermark {

    public static void main(String[] args) throws Exception {
        //ドキュメントをロードします。
        Presentation presentation = new Presentation();
        presentation.loadFromFile("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("E-iceblue"))
                    {
                        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("removeTextOrImageWatermark.pptx";, FileFormat.PPTX_2013);
    }
}

 

 

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0