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