Wordドキュメントの透かしは、所有者を示したり、ドキュメントの状態を表示したりすることができます。この記事では、無料のFree Spire.Doc for Javaライブラリを使用して、テキストと画像の透かしをWordドキュメントに追加する方法を説明します。
【依存関係の追加】
この方法は、無料のFree Spire.Doc 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.doc.free</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
2. 公式サイトよりJarファイルをダウンロード
まず、Free Spire.Doc for Java の公式サイトよりzipファイルをダウンロードします。zipファイルを解凍し、libフォルダの下にあるSpire.Doc.jarファイルを依存関係としてプロジェクトにインポートしてください。
Wordドキュメントにテキスト透かしを追加する
テキストの透かしは「オリジナル」と「コピー」が一般的で、文書の状態を示しています。
Wordドキュメントに1行のテキスト透かしを追加する手順は次のとおりです。
- Document クラスのインスタンスを作成し、Document.loadFromFile() メソッドを使用して Word ドキュメントを読み込みます。
- Document.getSections().get() メソッドを使用して、最初のセクションを取得します。
- TextWatermark クラスのインスタンスを作成します。
- TextWatermark クラスが提供するメソッドを使用して、テキスト透かしのテキスト、文字サイズ、色、レイアウトを設定します。
- Section.getDocument().setWatermark() メソッドを使用して、Wordドキュメントにテキストの透かしを追加します。
- Document.saveToFile() メソッドを使用して、ドキュメントを別のファイルに保存します。
Java
import com.spire.doc.*;
import com.spire.doc.documents.WatermarkLayout;
import java.awt.*;
public class addTextWatermark {
public static void main(String[] args) throws Exception {
//Documentクラスのインスタンスを作成する
Document document = new Document();
//Wordドキュメントを読み込む
document.loadFromFile("C:/Sample.docx");
//最初のセクションを取得する
Section section = document.getSections().get(0);
//TextWatermarkクラスのインスタンスを作成する
TextWatermark txtWatermark = new TextWatermark();
//テキストの透かしの形式を設定する
txtWatermark.setText("オリジナル");
txtWatermark.setFontSize(40);
txtWatermark.setColor(Color.red);
txtWatermark.setLayout(WatermarkLayout.Diagonal);
//テキストの透かしを文書に追加する
section.getDocument().setWatermark(txtWatermark);
//ドキュメントを保存する
document.saveToFile("1行のテキスト透かし.docx", FileFormat.Docx);
}
}
【結果のWordドキュメント】
Wordドキュメントに複数行のテキスト透かしを追加する
Wordドキュメントに複数行のテキスト透かしを挿入する場合は、以下の手順で行います。
- Document クラスのインスタンスを作成し、Document.loadFromFile() メソッドを使用してWordドキュメントを読み込みます。
- ShapeObject クラスを使用してWordArt形状を定義し、そのサイズとスタイルを設定します。
- HeaderFooter クラスでセクションのヘッダーを取得します。そして、header.addParagraph() メソッドを使用して、段落にヘッダーを追加します。
- WordArtの図形をコピーし、paragraph.getChildObjects().add(shape) メソッドを使用して多くの位置に追加しています。
- 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 addMultiLineTextWatermark {
public static void main(String[] args) throws Exception {
//Documentクラスのインスタンスを作成する
Document document = new Document();
document.loadFromFile("C:/Sample.docx");
//WordArtの図形を追加し、サイズを設定する
ShapeObject shape = new ShapeObject(document, ShapeType.Text_Plain_Text);
shape.setWidth(60);
shape.setHeight(20);
//図形のテキスト、位置、スタイルを設定する
shape.setVerticalPosition(40);
shape.setHorizontalPosition(20);
shape.setRotation(315);
shape.getWordArt().setText("サンプル");
shape.setFillColor(Color.red);
shape.setLineStyle(ShapeLineStyle.Single);
shape.setStrokeColor(new Color(245, 192, 192, 255));
shape.setStrokeWeight(1);
Section section;
HeaderFooter header;
for (int n = 0; n < document.getSections().getCount(); n++) {
section = document.getSections().get(n);
//セクションのヘッダーを取得する
header = section.getHeadersFooters().getHeader();
Paragraph paragraph;
for (int i = 0; i < 5; i++) {
//段落にヘッダーを追加する
paragraph = header.addParagraph();
for (int j = 0; j < 4; j++) {
//ワードアートの図形をコピーして、多くの位置に追加する
shape = (ShapeObject) shape.deepClone();
shape.setVerticalPosition(50 + 150 * i);
shape.setHorizontalPosition(20 + 160 * j);
paragraph.getChildObjects().add(shape);
}
}
//ドキュメントを保存する
document.saveToFile("複数行のテキスト透かし.docx", FileFormat.Docx_2013);
}
}
}
【結果のWordドキュメント】
Wordドキュメントに画像透かしを追加する
画像透かしは通常、文書の所有権を示すものである。
以下は、画像透かしを追加する手順です。
- Document クラスのインスタンスを作成し、Document.loadFromFile() メソッドを使用してWordドキュメントを読み込みます。
- PictureWatermark クラスのインスタンスを作成します。
- PictureWatermark.setPicture() メソッドを使用して、画像透かしとして画像を読み込み、画像スタイルを設定します。
- Document.setWatermark() メソッドを使用して、Word ドキュメントに画像透かしを追加します。
- Document.saveToFile() メソッドを使用して、ドキュメントを別のファイルに保存します。
Java
import com.spire.doc.*;
public class addImageWatermark {
public static void main(String[] args){
//Documentクラスのインスタンスを作成する
Document document = new Document();
//Wordドキュメントを読み込む
document.loadFromFile("C:/Sample.docx");
//PictureWatermarkクラスのインスタンスを作成する
PictureWatermark picture = new PictureWatermark();
//画像透かしの形式を設定する
picture.setPicture("C:/Logo.png");
picture.setScaling(150);
picture.isWashout(false);
//画像透かしをドキュメントに追加する
document.setWatermark(picture);
//結果ファイルを保存する
document.saveToFile("単一画像透かし.docx",FileFormat.Docx );
}
}
【結果のWordドキュメント】
Wordドキュメントに複数の画像の透かしを追加する
以下は、Wordドキュメントに複数の画像透かしを追加する手順です。
- Document クラスのインスタンスを作成し、Document.loadFromFile() メソッドを使用してWordドキュメントを読み込みます。
- DocPicture.loadImage() メソッドを使用して、画像を読み込みます。
- picture.setTextWrappingStyle() メソッドを使用してテキストの折り返しスタイルを設定します。
- HeaderFooter クラスでセクションのヘッダーを取得し、header.addParagraph() メソッドを使用して段落にヘッダーを追加します。
- 画像をコピーして、paragraph.getChildObjects().add(image) メソッドを使用して、多くの位置に追加しています。
- 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 addMultiImageWatermark {
public static void main(String[] args){
//Documentクラスのインスタンスを作成する
Document document = new Document();
//Wordドキュメントを読み込む
document.loadFromFile("C:/Sample.docx");
//画像を読み込む
DocPicture picture = new DocPicture(document);
picture.loadImage("C:/Logo1.png");
//テキストの折り返しスタイルを設定する
picture.setTextWrappingStyle(TextWrappingStyle.Behind);
for (int n = 0; n < document.getSections().getCount(); n++) {
Section section = document.getSections().get(n);
//セクションのヘッダーを取得する
HeaderFooter header = section.getHeadersFooters().getHeader();
Paragraph paragraph;
if(header.getParagraphs().getCount()>0){
paragraph=header.getParagraphs().get(0);
}else {
//段落にヘッダーを追加する
paragraph = header.addParagraph();
}
for (int p = 0; p < 5; p++) {
for (int q = 0; q < 4; q++) {
//画像をコピーして、多くの位置に追加する
picture = (DocPicture)picture.deepClone();
picture.setVerticalPosition(100 + 200 * p);
picture.setHorizontalPosition(50 + 210 * q);
paragraph.getChildObjects().add(picture);
}
}
}
//結果ファイルを保存する
document.saveToFile("複数の画像の電子透かし.docx",FileFormat.Docx );
}
}
【結果のWordドキュメント】
透かしはとても便利な機能です。上記は、Wordドキュメントにいくつかの種類の透かしを追加する方法を示しています。もし、透かしに関するより多くの情報を知りたければ、Spire.Doc Forumに移動してください。