LoginSignup
0
0

More than 3 years have passed since last update.

JavaはWord文書を作成します

Last updated at Posted at 2020-08-27

Wordは強力なワープロ機能を備えており、日常の仕事や生活で広く使用されているツールの1つです。この記事では、Free Spire.Doc for Javaを使用して、JavaアプリケーションでWord文書を作成し、画像を挿入し、フォント形式、配置、インデント、段落の間隔を設定する方法を紹介します。

JARパッケージのインポート
方法1:Free Spire.Doc for Javaをダウンロードして解凍し、libフォルダーのSpire.Doc.jarパッケージを依存関係としてJavaアプリケーションにインポートします。
方法2:Mavenリポジトリーを介してJARパッケージをインストールし、pom.xmlファイルを以下のように構成します

<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <name>e-iceblue</name>
            <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
        </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc.free</artifactId>
        <version>2.7.3</version>
    </dependency>
</dependencies>

Javaコード

import com.spire.doc.*;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.ParagraphStyle;
import com.spire.doc.fields.DocPicture;

import java.awt.*;

public class CreateWordDocument {
    public static void main(String[] args){
        //Word文書を作成する
        Document document = new Document();

        //sectionを追加
        Section section = document.addSection();

        //セクションに4つの段落を追加する
        Paragraph para1 = section.addParagraph();
        para1.appendText("痩せすぎだとうつのリスクが上昇́");

        Paragraph para2 = section.addParagraph();
        para2.appendText(" ほっそりとしたスタイルは常に女性の憧れだが、スタイルを気にし始める男性も近年増えている。"+
                "食事制限、脂肪吸引、注射など、痩せるために手段を選ばない人もいる。"+
                "韓国のソウル国立大学医学部の研究によると、痩せすぎると喜びを感じにくく、うつになるリスクも高いという。");

        Paragraph para3 = section.addParagraph();
        para3.appendText("研究者は個別に行われた183の研究のデータを分析した後、体重が軽すぎる人は幸福度指数が他の人を超えることはなく、精神障害を患うリスクも高いことを発見した。"+
                "多くの人はダイエットをする時に食欲を抑えることから始めるが、それによって身体機能が乱れ、消極的になりやすい。"+
                "一方で、痩せすぎの人は体脂肪率が低く、栄養不足により脳細胞がダメージを受け、記憶力が損なわれ、精神的な影響を受ける。");

        //段落4に画像を追加する
        Paragraph para4 = section.addParagraph();
        DocPicture picture = para4.appendPicture("img.jpg");
        //画像の幅を設定する
        picture.setWidth(300f);
        //画像の高さを設定する
        picture.setHeight(220f);

        //最初の段落をタイトルとして使用し、タイトルをフォーマットする
        ParagraphStyle style1 = new ParagraphStyle(document);
        style1.setName("titleStyle");
        style1.getCharacterFormat().setBold(true);
        style1.getCharacterFormat().setTextColor(Color.BLUE);
        style1.getCharacterFormat().setFontName("Mincho");
        style1.getCharacterFormat().setFontSize(12f);
        document.getStyles().add(style1);
        para1.applyStyle("titleStyle");

        //段落2と3のフォーマットを設定する
        ParagraphStyle style2 = new ParagraphStyle(document);
        style2.setName("paraStyle");
        style2.getCharacterFormat().setFontName("Mincho");
        style2.getCharacterFormat().setFontSize(11f);
        document.getStyles().add(style2);
        para2.applyStyle("paraStyle");
        para3.applyStyle("paraStyle");

        //段落1と段落4を水平方向の中央に配置する
        para1.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        para4.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);

        //段落2と段落3を最初の行のインデントとして設定する
        para2.getFormat().setFirstLineIndent(25f);
        para3.getFormat().setFirstLineIndent(25f);

        //最初の3つの段落の後にスペースを設定する
        para1.getFormat().setAfterSpacing(15f);
        para2.getFormat().setAfterSpacing(10f);
        para3.getFormat().setAfterSpacing(10f);

        //ドキュメントを保存します
        document.saveToFile("Word Document.docx", FileFormat.Docx);
    }
}

d

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