1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Java Wordに透かしを追加する方法

Last updated at Posted at 2023-03-30

Word文書に透かしを追加することで、透明な文字や画像を文書ページに追加し、文書内容が他者によってコピーまたは変更されないようにすることができます。同時に、これは著作権を保護するための良い方法でもあります。Microsoft Wordでは、簡単な操作でWord文書に直接透かしを追加できるため、ここでは紹介しません。次に、Javaプログラムを使用して、プログラミング的な方法でこの機能を実現する方法を紹介します。使用するライブラリはFree Spire.Doc for Javaです。

Free Spire.Doc for Javaのインストール

方法1: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.2.0</version>
    </dependency>
</dependencies>

方法2:mavenを使用していない場合は、このリンクからFree Spire.Doc for Javaをダウンロードできます。ダウンロードが完了したら、ダウンロードパッケージを解凍します。次にIDEAで新しいプロジェクトを作成し、「File」、「Project Structure」、「Modules」、「Dependencies」を順にクリックします。「+」の「JARs or Directories」をクリックして、解凍したファイルを見つけ、その中のlibフォルダの下にあるSpire.Doc.jarをプロジェクトにインポートします。

Word にテキスト透かしを追加する

  • Document インスタンスを作成します。
  • Document.loadFromFile() メソッドを使用してサンプルドキュメントをロードします。
  • Document.getSections().get() メソッドを使用して最初のセクションを取得します。
  • TextWatermark インスタンスを作成します。
  • TextWatermark クラスで提供される方法を使用して、テキスト透かしのテキスト、フォントサイズ、色、レイアウトを設定します。
  • Section.getDocument().setWatermark() メソッドを使用して、サンプルドキュメントにテキストウォーターマークを追加します。
  • Document.saveToFile() メソッドを使用して、結果文書を保存します。
import com.spire.doc.*;
import com.spire.doc.documents.WatermarkLayout;
import java.awt.*;

public class WordTextWatermark {
    public static void main(String[] args) {
        //Documentインスタンスを作成する
        Document document = new Document();

        //サンプルドキュメントをロードする
        document.loadFromFile("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("result1.docx", FileFormat.Docx);
    }

}

2023-03-30_111624.png

Word に画像透かしを追加する

以下に詳細な操作方法を示します。

  • Document インスタンスを作成します。
  • Document.loadFromFile() メソッドを使用してサンプルドキュメントをロードします。
  • PictureWatermark インスタンスを作成します。
  • PictureWatermark.setPicture() メソッドを使用して画像を画像透かしとしてロードします。PictureWatermark.setScaling() メソッドと PictureWatermark.isWashout() メソッドを使用して、画像透かしのスケーリングとウォッシュアウトプロパティを設定します。
  • Document.setWatermark() メソッドを使用して、サンプルドキュメントに画像ウォーターマークを追加します。
  • Document.saveToFile() メソッドを使用して、結果文書を保存します。
import com.spire.doc.*;

public class WordImageWatermark {
    public static void main(String[] args)  throws Exception{
        //Documentインスタンスを作成する
        Document document = new Document();

        //サンプルドキュメントをロードする
        document.loadFromFile("Sample.docx");

        //PictureWatermarkインスタンスを作成する
        PictureWatermark picture = new PictureWatermark();

        //画像透かしの書式を設定する
        picture.setPicture("logo.png");
        picture.setScaling(100);
        picture.isWashout(false);

        //サンプルドキュメントに透かしを追加する
        document.setWatermark(picture);

        //結果文書を保存する
        document.saveToFile("result2.docx",FileFormat.Docx );
    }
}

2023-03-30_111534.png

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?