透かしは、文書の背景に表示されるテキストまたは淡い色の画像です。PowerPoint文書で透かしを使用すると、文書の著作権を保護したり、文書の状態(ドラフト、最終版など)を指定したり、自社のブランドを宣伝したりするなど、さまざまな目的で使用することができます。この記事では、無料のFree Spire.Presentation for Javaを使用して、JavaでPowerPointに透かしを追加または削除する方法を説明します。
【依存関係の追加】
この方法は、無料のFree Spire.Presentation for Javaが必要ですので、先にjarファイルをインポートしてください。
1. Maven
Maven を使用している場合、プロジェクトの pom.xml ファイルに以下のコードを追加することで、簡単にアプリケーションに JAR ファイルをインポートすることができます。
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation.free</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
2. 公式サイトよりJarファイルをダウンロード
まず、Free Spire.Presentation for Java の公式サイトよりzipファイルをダウンロードします。zipファイルを解凍し、libフォルダの下にあるSpire.Presentation.jarファイルを依存関係としてプロジェクトにインポートしてください。
PowerPointのスライドにテキストの透かしを挿入する
Javaを使用してPowerPoint文書にテキストの透かしを追加する主な手順は次のとおりです。
- Presentation クラスのインスタンスを作成します。
- Presentation.loadFromFile() メソッドを使用してPowerPointドキュメントを読み込みます。
- ドキュメント内のすべてのスライドをループします。
- ISlide.getShapes().appendShape() メソッドを使用して、各スライドに図形を追加します。
- 図形の名前、塗りつぶしの種類、回転角度、罫線を設定します。
- IAutoShape.getLocking().setSelectionProtection() メソッドで図形をロックし、選択禁止にします。
- IAutoShape.getTextFrame().setText() メソッドを使用して、図形に透かしテキストを追加します。
- 透かしテキストのフォント名、フォントサイズ、フォント色を設定します。
- Presentation.saveToFile() メソッドを使用して、結果ドキュメントを保存します。
Java
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.*;
import java.awt.geom.Rectangle2D;
public class insertTextWatermark {
public static void main(String []args) throws Exception {
//Presentationクラスのインスタンスを作成し、PowerPointドキュメントを読み込む
Presentation ppt = new Presentation();
ppt.loadFromFile("年次レビュー.pptx");
//ドキュメント内の全スライドをループする
for(int i = 0; i < ppt.getSlides().getCount(); i++)
{
ISlide slide = ppt.getSlides().get(i);
//スライドに図形を追加する
IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Double(340, 150, 300, 200));
//図形の名称を設定する
shape.setName("テキスト透かし");
//図形の塗りつぶし種類を設定する
shape.getFill().setFillType(FillFormatType.NONE);
//図形の回転角度を設定する
shape.setRotation(-45);
//図形の罫線を削除する
shape.getLine().setFillType(FillFormatType.NONE);
//図形をロックして、選択と編集から保護します
shape.getLocking().setSelectionProtection(true);
//図形に透かしテキストを追加し、テキストのフォント色、フォントサイズ、フォント名を設定する
shape.getTextFrame().setText("部外秘");
PortionEx textRange = shape.getTextFrame().getTextRange();
textRange.getFill().setFillType(FillFormatType.SOLID);
textRange.getFill().getSolidColor().setColor(Color.GRAY);
textRange.setFontHeight(60);
textRange.setLatinFont(new TextFont("Yu Mincho"));
}
//結果ドキュメントを保存する
ppt.saveToFile("テキスト透かし.pptx", FileFormat.PPTX_2013);
}
}
【結果のPowerPointドキュメント】
PowerPointのスライドに画像の透かしを挿入する
PowerPointドキュメントに画像透かしを追加する主な手順は、次のとおりです。
- Presentation クラスのインスタンスを作成します。
- Presentation.loadFromFile() メソッドを使用してPowerPointドキュメントを読み込みます。
- ドキュメント内の全スライドをループします。
- ISlide.getShapes().appendEmbedImage() メソッドを使用して、各スライドに透かし画像を追加します。
- 画像名、透明度、罫線を設定します。
- IEmbedImage.getShapeLocking().setSelectionProtection() メソッドを使用して、選択からそれを保護するために画像をロックします。
- Presentation.saveToFile() メソッドを使用して結果ドキュメントを保存します。
Java
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.geom.Rectangle2D;
public class insertImageWatermark {
public static void main(String []args) throws Exception {
//Presentationクラスのインスタンスを作成し、PowerPointドキュメントを読み込む
Presentation ppt = new Presentation();
ppt.loadFromFile("年次レビュー.pptx");
//ドキュメント内の全スライドをループする
for(int i = 0; i < ppt.getSlides().getCount(); i++) {
ISlide slide = ppt.getSlides().get(i);
//スライドに透かし画像を追加する
IEmbedImage image = slide.getShapes().appendEmbedImage(ShapeType.RECTANGLE, "Logo.png", new Rectangle2D.Double(340, 150, 200, 200));
//画像名を設定する
image.setName("画像透かし");
//画像の透明度を設定する
image.getPictureFill().getPicture().setTransparency(70);
//画像の罫線を消す
image.getLine().setFillType(FillFormatType.NONE);
//画像をロックし、選択から保護する
image.getShapeLocking().setSelectionProtection(true);
}
//結果ドキュメントを保存する
ppt.saveToFile("画像透かし.pptx", FileFormat.PPTX_2013);
}
}
【結果のPowerPointドキュメント】
PowerPointのスライドから透かしを削除する
PowerPoint ドキュメントから、その形状名を使用して透かしを削除することができます。
以下は、PowerPoint文書から透かしを削除する主な手順です。
- Presentation クラスのインスタンスを作成します。
- Presentation.LoadFromFile() メソッドを使用してPowerPointドキュメントを読み込みます。
- ドキュメント内の全スライドをループ処理します。
- 各スライドにあるすべての図形をループ処理します。
- IShape.getName() メソッドで指定された名前の図形を検索します。
- ISlide.getShapes().removeAt() メソッドを使用して、スライドから図形を削除します。
- Presentation.saveToFile() メソッドを使用して結果ドキュメントを保存します。
Java
import com.spire.presentation.*;
public class removeWatermark {
public static void main(String []args) throws Exception {
//Presentationクラスのインスタンスを作成し、PowerPointドキュメントを読み込む
Presentation ppt = new Presentation();
ppt.loadFromFile("画像透かし.pptx");
//ドキュメント内の全スライドをループする
for(int i = 0; i < ppt.getSlides().getCount(); i++)
{
ISlide slide = ppt.getSlides().get(i);
//スライド内の図形をループする
for (int j = slide.getShapes().getCount()-1; j >= 0; j--)
{
IShape shape = slide.getShapes().get(j);
//「画像透かし」という名前のシェイプを削除する
if (shape.getName().equals("画像透かし"))
{
slide.getShapes().removeAt(j);
}
}
}
//結果ドキュメントを保存する
ppt.saveToFile("透かしの削除.pptx", FileFormat.PPTX_2013);
}
}
【結果のPowerPointドキュメント】
この記事では、PowerPointプレゼンテーションでテキストと画像の透かしを追加または削除する手順を説明します。あなたはまだ透かしの任意の問題があるか、またはあなたがより多くのPowerPointプレゼンテーションの処理について知りたい場合は、Spire.Presentation Forumに移動してください。