チュートリアル

簡単にライブラリーを使用するためのチュートリアルコレクション

チュートリアル»Java»Spire.Presentation for Java»透かし»Java:PowerPoint にテキスト透かしを追加する方法
2023-08-14

Java:PowerPoint にテキスト透かしを追加する方法

テキスト透かしとは、透明または半透明のロゴや情報をテキストの形式でドキュメント、画像、その他のメディアに追加することを指します。この機能は PowerPoint プレゼンテーションにも適用されます。スライドにテキスト透かしを挿入することで、ユーザーは著作権の所有権を識別したり、ドキュメントのセキュリティを保護したり、他の情報を伝えることができます。この記事では、Spire.Presentation for Java を使用して PowerPoint スライドにテキスト透かしを追加する方法を示します。

Spire.Presentation for Java をインストールします

まず、Spire.Presentation for Java を Java プロジェクトに追加する必要があります。JAR ファイルは、このリンクからダウンロードできます。Maven を使用する場合は、次のコードをプロジェクトの pom.xml ファイルに追加する必要があります。

<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</artifactId>
        <version>8.7.3</version>
    </dependency>
</dependencies>

スライドに単一行のテキスト透かしを追加する

Spire.Presentation for Java では、Presentation.getSlides().get().getShapes().appendShape() メソッドを使用してスライドに形状を挿入することで、単一のテキスト透かしを追加できます。次に、IAutoShape.getTextFrame().setText() メソッドを呼び出してテキストを設定します。以下は詳細な手順です。

  • Presentation オブジェクトを作成し、Presentation.loadFromFile() メソッドを使用してサンプルファイルをロードします。
  • 透かし文字列の幅と高さを設定します。
  • Rectangle2D.Double オブジェクトを作成し、Presentation.getSlideSize().getSize().getWidth() メソッドと Presentation.getSlideSize().getSize().getHeight() メソッドを呼び出して、形状の位置とサイズを指定します。
  • Presentation.getSlides().get().getShapes().appendShape() メソッドを使用して、最初のスライドに形状を追加します。
  • 形状のスタイルを設定します。
  • IAutoShape.getTextFrame().setText() を使用して形状のテキストを設定し、IAutoShape.getTextFrame().getTextRange() メソッドを使用して特定の範囲を取得します。
  • テキスト範囲のスタイルを設定します。
  • Presentation.saveToFile() メソッドを使用して結果文書を保存します。
  • Java
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.*;
import java.awt.geom.Rectangle2D;

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

        //Presentationオブジェクトを作成し、サンプルファイルをロードする
        Presentation presentation = new Presentation();
        presentation.loadFromFile("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);

        //最初のスライドに形状を追加する
        IAutoShape shape = presentation.getSlides().get(0).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("Confidential");
        PortionEx textRange = shape.getTextFrame().getTextRange();

        //テキスト範囲のスタイルを設定する
        textRange.getFill().setFillType(FillFormatType.SOLID);
        textRange.setFontHeight(50);
        Color color = new Color(237,129,150,200);
        textRange.getFill().getSolidColor().setColor(color);


        //結果文書を保存する
        presentation.saveToFile("output/SingleWatermark.pptx", FileFormat.PPTX_2010);
        presentation.dispose();
    }
}

Java:PowerPoint にテキスト透かしを追加する方法

スライドに複数行のテキスト透かしを追加する

ループ内で、Presentation.getSlides().get().getShapes().appendShape() メソッドを複数回呼び出すことで、異なる位置に複数の長方形の形状を追加します。これにより、スライドに複数行のテキスト透かしを追加する効果が実現されます。以下は詳細な手順です。

  • Presentation オブジェクトを作成し、Presentation.loadFromFile() メソッドを使用してサンプルファイルをロードします。
  • 透かしのテキストを設定し、透かしテキストのサイズを測定します。
  • 透かしの x 座標と y 座標を初期化します。次に、ループ内で Presentation.getSlides().get().getShapes().appendShape() メソッドを複数回呼び出して、スライドに複数の長方形の形状を追加します。
  • 形状のスタイルを設定します。
  • IAutoShape.getTextFrame().setText() メソッドを使用して形状にテキストを追加します。
  • IAutoShape.getTextFrame().getTextRange() メソッドを使用して特定の範囲を取得します。
  • テキスト範囲のスタイルを設定します。
  • Presentation.saveToFile() メソッドを使用して結果文書を保存します。
  • Java
import com.spire.pdf.graphics.PdfTrueTypeFont;
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Rectangle2D;

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

        //Presentationオブジェクトを作成し、サンプルファイルをロードする
        Presentation presentation = new Presentation();
        presentation.loadFromFile("sample.pptx");

        //透かしテキストを設定する
        String watermarkText = "Confidential";

        //透かしテキストのサイズを測定する
        Font font = new java.awt.Font("Arial", java.awt.Font.BOLD, 20);
        PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(font);
        Dimension2D strSize = trueTypeFont.measureString(watermarkText);

        //x座標とy座標を初期化する
        float x = 30;
        float y = 80;

        for (int rowNum = 0; rowNum < 4; rowNum++) {
            for (int colNum = 0; colNum < 4; colNum++) {

                //最初のスライドに長方形の形状を追加する
                Rectangle2D rect = new Rectangle2D.Float(x, y, (float) strSize.getWidth() + 10, (float) strSize.getHeight());
                IAutoShape shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, rect);

                //形状のスタイルを設定する
                shape.getFill().setFillType(FillFormatType.NONE);
                shape.getShapeStyle().getLineColor().setColor(new Color(1, 1, 1, 0));
                shape.setRotation(-45);
                shape.getLocking().setSelectionProtection(true);
                shape.getLine().setFillType(FillFormatType.NONE);

                //形状に透かしテキストを追加する
                shape.getTextFrame().setText(watermarkText);
                PortionEx textRange = shape.getTextFrame().getTextRange();

                //テキスト範囲のスタイルを設定する
                textRange.getFill().setFillType(FillFormatType.SOLID);
                textRange.setLatinFont(new TextFont(trueTypeFont.getName()));
                textRange.setFontMinSize(trueTypeFont.getSize());
                Color color = new Color(237,129,150,200);
                textRange.getFill().getSolidColor().setColor(color);

                x += (100 + strSize.getWidth());

            }
            x = 30;
            y += (100 + strSize.getHeight());
        }

        //結果文書を保存する
        presentation.saveToFile("output/TiledWatermark.pptx", FileFormat.PPTX_2013);
        presentation.dispose();
    }
}

Java:PowerPoint にテキスト透かしを追加する方法

一時ライセンスを申請する

結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。

Read 297 times