チュートリアル

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

チュートリアル»Java»Spire.Doc for Java»ページの背景»Java:Word で繰り返し透かしを挿入する方法
2023-07-05

Java:Word で繰り返し透かしを挿入する方法

繰り返し透かしは、複数行の透かしとも呼ばれ、Word ドキュメントのページ上に一定の間隔で複数回表示される透かしの一種です。通常の透かしに比べ、繰り返し透かしは削除や隠蔽が難しいため、不正コピーや不正配布の防止に効果的です。この記事では、Spire.Doc for Java を使用して、プログラムによって Word ドキュメントに繰り返しテキストと画像の透かしを挿入する方法を紹介します。

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

まず、Spire. Doc 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.doc</artifactId>
        <version>11.6.0</version>
    </dependency>
</dependencies>

Word ドキュメントに繰り返しテキスト透かしを追加する

Word ドキュメントのヘッダーに指定した間隔で繰り返しワードアートを追加することで、Word ドキュメントに繰り返しテキスト透かしを挿入することができます。詳しい手順は次のとおりです。

  • Document クラスのオブジェクトを作成します。
  • Document.loadFromFile() メソッドを使用して Word ドキュメントを読み込みます。
  • ShapeObject クラスのオブジェクトを作成し、ShapeObject.getWordArt().setText() メソッドを使用してワードアートのテキストを設定します。
  • 回転角度と垂直方向の繰り返し回数、水平方向の繰り返し回数を指定し ます。
  • ShapeObject クラスのメソッドを使用して、図形の書式を設定します。
  • Paragraph.getChildObjects().add(ShapeObject) メソッドを使用して、各セクションのヘッダーにワードアート図形を指定された間隔で複数回追加することにより、各セクションに繰り返し透かしを挿入するために、ドキュメント内のセクションをループします。
  • Document.saveToFile() メソッドを使用してドキュメントを保存します。
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.HeaderFooter;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.ShapeLineStyle;
import com.spire.doc.documents.ShapeType;
import com.spire.doc.fields.ShapeObject;

import java.awt.*;

public class insertRepeatingTextWatermark {
    public static void main(String[] args) {

        //Documentクラスのオブジェクトを作成する
        Document doc = new Document();

        //Wordドキュメントを読み込む
        doc.loadFromFile("サンプル.docx");

        //ShapeObjectクラスのオブジェクトを作成し、アートフォントのテキストを設定する
        ShapeObject shape = new ShapeObject(doc, ShapeType.Text_Plain_Text);
        shape.getWordArt().setText("オリジナル");

        //テキスト透かしの回転角度と、垂直方向と水平方向の繰り返し回数を設定する
        double rotation = 315;
        int ver = 5;
        int hor = 3;

        //ワードアートの書式を設定する
        shape.setWidth(60);
        shape.setHeight(20);
        shape.setVerticalPosition(30);
        shape.setHorizontalPosition(20);
        shape.setRotation(rotation);
        shape.getWordArt().setFontFamily("HarmonyOS Sans SC");
        shape.setFillColor(Color.BLUE);
        shape.setLineStyle(ShapeLineStyle.Single);
        shape.setStrokeColor(Color.CYAN);
        shape.setStrokeWeight(1);

        //ドキュメントのセクションをループする
        for (Section section : (Iterable) doc.getSections()
        ) {
            //セクションのヘッダーを取得する
            HeaderFooter header = section.getHeadersFooters().getHeader();
            //ヘッダーに段落を追加する
            Paragraph paragraph = header.addParagraph();
            for (int i = 0; i < ver; i++) {
                for (int j = 0; j < hor; j++) {
                    //ヘッダーに段落を追加する
                    shape = (ShapeObject) shape.deepClone();
                    shape.setVerticalPosition((float) (section.getPageSetup().getPageSize().getHeight()/ver * i  + Math.sin(rotation) * shape.getWidth()/2));
                    shape.setHorizontalPosition((float) ((section.getPageSetup().getPageSize().getWidth()/hor - shape.getWidth()/2) * j));
                    paragraph.getChildObjects().add(shape);
                }
            }
        }

        //ドキュメントを保存する
        doc.saveToFile("繰り返しテキスト透かし.docx", FileFormat.Auto);
        doc.dispose();
    }
}

Java:Word で繰り返し透かしを挿入する方法

Word ドキュメントに繰り返し画像透かしを追加する

同様に、ヘッダーに一定の間隔で繰り返し画像を追加することで、Wordドキュメントに繰り返し画像透かしを挿入することができます。詳しい手順は次のとおりです。

  • Document クラスのオブジェクトを作成します。
  • Document.loadFromFile() メソッドを使用して Word ドキュメントを読み込みます。
  • DocPicture.loadImage() メソッドを使用して画像を読み込みます。
  • DocPicture.setTextWrappingStyle(TextWrappingStyle.Behind) メソッドを使用して、画像の文字列の折り返しスタイルを背面に設定します。
  • 垂直方向の繰り返し回数と水平方向の繰り返し回数を指定します。
  • Paragraph.getChildObjects().add(DocPicture) メソッドを使用して、各セクションのヘッダーに画像を指定された間隔で複数回追加することにより、各セクションに繰り返し透かしを挿入するために、ドキュメント内のセクションをループします。
  • Document.saveToFile() メソッドを使用してドキュメントを保存します。
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.HeaderFooter;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextWrappingStyle;
import com.spire.doc.fields.DocPicture;

public class insertRepeatingPictureWatermark {
    public static void main(String[] args) {

        //Documentクラスのオブジェクトを作成する
        Document doc = new Document();

        //Wordドキュメントを読み込む
        doc.loadFromFile("サンプル.docx");

        //画像を読み込む
        DocPicture pic = new DocPicture(doc);
        pic.loadImage("透かし.png");

        //画像のテキストの折り返しを背面に設定する
        pic.setTextWrappingStyle(TextWrappingStyle.Behind);

        //垂直方向と水平方向の繰り返し回数を設定する
        int ver = 4;
        int hor = 3;

        //ドキュメントのセクションをループする
        for (Section section : (Iterable) doc.getSections()
        ) {
            //セクションのヘッダーを取得する
            HeaderFooter header = section.getHeadersFooters().getHeader();
            //ヘッダーに段落を追加する
            Paragraph paragraph = header.addParagraph();
            for (int i = 0; i < ver; i++) {
                for (int j = 0; j < hor; j++) {
                    //ヘッダーに画像を追加する
                    pic = (DocPicture) pic.deepClone();
                    pic.setVerticalPosition((float) ((section.getPageSetup().getPageSize().getHeight()/ver) * i));
                    pic.setHorizontalPosition((float) (section.getPageSetup().getPageSize().getWidth()/hor - pic.getWidth()/2) * j);
                    paragraph.getChildObjects().add(pic);
                }
            }
        }

        //ドキュメントを保存する
        doc.saveToFile("繰り返し画像透かし.docx", FileFormat.Auto);
        doc.dispose();
    }
}

Java:Word で繰り返し透かしを挿入する方法

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

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

Read 409 times