0
0

More than 1 year has passed since last update.

Wordドキュメントにワードアートを挿入するまたは読む方法(Java)

Last updated at Posted at 2022-09-29

ワードアートは、テキストに塗りつぶし、輪郭、影などのデザイン要素を追加することができる一連のテキストスタイルです。ワードアートを挿入することで、テキストを目立たせることができます。Microsoft Wordでドキュメントを作成する際、ワードアートの挿入が必要になることがあります。この記事では、Free Spire.Doc for Javaを使用して、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ファイルを依存関係としてプロジェクトにインポートしてください。

JavaでWordドキュメントにワードアートを挿入する

Wordドキュメントにワードアートを挿入する主な手順は次のとおりです。

  • Document クラスのインスタンスを作成します。
  • Document.addSection() メソッドを使用して、ドキュメントにセクションを追加します。
  • Section.addParagraph() メソッドを使用して、セクションに段落を追加します。
  • Paragraph.appendShape() メソッドを使用して、指定されたサイズとタイプの図形を段落に追加します。
  • 図形の位置を設定します。
  • ShapeObject.getWordArt().setText() メソッドで、指定した文字列のワードアートを形状に挿入します。
  • ワードアートの塗りつぶし色と輪郭色を設定する。
  • Document.saveToFile() メソッドを使用して、結果のドキュメントを保存します。

Java

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.ShapeType;
import com.spire.doc.fields.ShapeObject;

import java.awt.*;

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

        //Document クラスのインスタンスを作成する
        Document doc = new Document();

        //ドキュメントにセクションを追加する
        Section section = doc.addSection();

        //このセクションに段落を追加する
        Paragraph paragraph = section.addParagraph();

        //この段落に図形を追加する
        ShapeObject shape = paragraph.appendShape(500, 70, ShapeType.Text_Wave_3);

        //この図形の位置を設定する
        shape.setVerticalPosition(150);
        shape.setHorizontalPosition(10);

        //ワードアートのテキストを設定する
        shape.getWordArt().setText("お誕生日おめでとうございます。");

        //塗りつぶしの色を設定する
        shape.setFillColor(Color.orange);

        //テキストの輪郭色を設定する
        shape.setStrokeColor(Color.YELLOW);

        //結果のドキュメントを保存する
        doc.saveToFile("ワードアートの挿入.docx", FileFormat.Docx);
    }
}

【結果のWordファイル】

Wordドキュメントにワードアートを挿入する

JavaでWordドキュメント内のワードアートを読み込む

WordドキュメントでWordArtを読み込むための主な手順は次のとおりです。

  • Document クラスのインスタンスを作成します。
  • Document.loadFromFile() メソッドを使用してWordドキュメントを読み込みます。
  • ドキュメント内のすべてのセクションをループします。
  • 各セクションのすべての段落をループします。
  • 各段落に含まれるすべての子オブジェクトをループします。
  • 子オブジェクトが ShapeObject であるかどうかを検出します。
  • ShapeObject.getWordArt().getText() メソッドを使用して、図形オブジェクト内のワードアートのテキストを取得します。
  • テキストがNULLでなければ、それをコンソールに表示します。

Java

import com.spire.doc.Document;
import com.spire.doc.DocumentObject;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.ShapeObject;

import java.io.FileWriter;
import java.io.IOException;

public class readWordArt {
    public static void main(String[] args) throws IOException {

        //Document クラスのインスタンスを作成する
        Document doc = new Document();

        //Wordドキュメントを読みます
        doc.loadFromFile("ワードアートの挿入.docx");

        //ドキュメント内のすべてのセクションをループする
        for (Section section :(Iterable<? extends Section>) doc.getSections()) {

            //各セクションのすべての段落をループする
            for (Paragraph paragraph : (Iterable<? extends Paragraph>) section.getBody().getParagraphs()) {

                //各段落に含まれるすべての子オブジェクトをループする
                for (DocumentObject documentObject : (Iterable<? extends DocumentObject>) paragraph.getChildObjects()) {

                    //子オブジェクトが図形であるかどうかを検出する
                    if (documentObject instanceof ShapeObject) {
                        ShapeObject shapeObject = (ShapeObject) documentObject;

                        //図形がワードアートであるかどうかを検出する
                        String text = shapeObject.getWordArt().getText();
                        if (text != "") {

                            //TXTファイルにテキストを書き込む
                            writeStringToTxt(text,"ワードアートのテキスト.txt");
                        }
                    }
                }
            }
        }
    }
    //TXTファイルにテキストを書き込む
    public static void writeStringToTxt(String content, String txtFileName) throws IOException {

        FileWriter fWriter= new FileWriter(txtFileName,true);
        try {
            fWriter.write(content);
        }catch(IOException ex){
            ex.printStackTrace();
        }finally{
            try{
                fWriter.flush();
                fWriter.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

【結果のファイル】

Wordドキュメント内のワードアートを読み込む

0
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
0
0