チュートリアル

簡単にライブラリーを使用するためのチュートリアルコレクション

チュートリアル»Java»Spire.Doc for Java»脚注»Java:Word ドキュメントに脚注を挿入または削除する方法
2022-08-04

Java:Word ドキュメントに脚注を挿入または削除する方法

脚注は、Word ドキュメントに追加情報や詳細な説明を提供するために一般的に使用されます。長くて複雑な単語やフレーズは、読者が文章を読みにくくします。しかし、本文に関連情報や説明を提供すると、文章の一貫性が損なわれ、文章が冗長になります。幸いなことに、脚注は、文章の流暢さと簡潔さを損なうことなく、ページの終わりに情報や説明を与えるために作者を助けることができます。この記事では、Spire.Doc for Java を使用して 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>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>10.7.10</version>
    </dependency>
</dependencies>

Word ドキュメントに脚注を挿入する

脚注を挿入する詳細な手順は次のとおりです。

  • Document クラスのオブジェクトを作成します。
  • Document.loadFromFile() メソッドを使用して Word ドキュメントを読み込みます。
  • Document.findString() メソッドを使用して、注記するテキストを検索します。
  • Paragraph.getChildObjects().insert() メソッドを使用して脚注を追加します。
  • Footnote.getTextBody().addParagraph() メソッドを使用して脚注の中に段落を追加します。
  • Paragraph.appendText() メソッドを使用して、追加された段落にテキストを追加します。
  • TextRange.getCharacterFormat() メソッドから返される CharacterFormat オブジェクトの下のメソッドを使用して脚注のテキスト書式を設定します。
  • Footnote.getMarkerCharacterFormat() メソッドから返される CharaterFormat オブジェクトのメソッドを使用して、マーカーのテキスト書式を設定します。
  • Document.saveToFile() を使用してドキュメントを保存します。
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import java.awt.*;

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

        //Document クラスのオブジェクトを作成する
        Document document = new Document();

        //Wordドキュメントを読み込む
        document.loadFromFile("実例.docx");

        //注釈を付けるテキストを検索する
        TextSelection selection = document.findString("ISO 9660", false, true);

        //脚注を挿入する
        TextRange textRange = selection.getAsOneRange();
        Paragraph paragraph = textRange.getOwnerParagraph();
        int index = paragraph.getChildObjects().indexOf(textRange);
        Footnote footnote = paragraph.appendFootnote(FootnoteType.Footnote);
        paragraph.getChildObjects().insert(index + 1, footnote);

        //脚注に段落を追加する
        Paragraph paragraph1 = footnote.getTextBody().addParagraph();

        //追加された段落にテキストを追加する
        textRange = paragraph1.appendText("ISO 9660は、1988年にISOで標準化されたCD-ROMのファイルシステムである。");

        //脚注のテキスト書式を設定する
        textRange.getCharacterFormat().setFontName("Yu Mincho");
        textRange.getCharacterFormat().setFontSize(10);
        textRange.getCharacterFormat().setTextColor(Color.RED);

        //マーカーのテキスト書式を設定する
        footnote.getMarkerCharacterFormat().setFontName("Calibri");
        footnote.getMarkerCharacterFormat().setFontSize(12);
        footnote.getMarkerCharacterFormat().setBold(true);
        footnote.getMarkerCharacterFormat().setTextColor(Color.red);

        //ドキュメントを保存する
        String output = "脚注の挿入.docx";
        document.saveToFile(output, FileFormat.Docx_2010);
    }
}

Java:Word ドキュメントに脚注を挿入または削除する方法

Word ドキュメントから脚注を削除する

脚注を削除する詳細な手順は次のとおりです。

  • Document クラスのオブジェクトを作成します。
  • Document.loadFromFile() メソッドで Word ドキュメントをディスクから読み込みます。
  • セクション、段落、その子オブジェクトの順にループして、特定の子オブジェクトを取得し、それが脚注であるかどうかを判断します。
  • Paragraph.getChildObjects().removeAt() メソッドを使用して、指定の脚注を削除しています。
  • Document.saveToFile() メソッドを使用してドキュメントを保存します。
  • Java
import com.spire.doc.*;
import com.spire.doc.fields.*;
import com.spire.doc.documents.*;
import java.util.*;

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

        //Document クラスのオブジェクトを作成する
        Document document = new Document();

        //Wordドキュメントを読み込む
        document.loadFromFile("実例.docx");

        //セクションをループする
        for (int i = 0; i < document.getSections().getCount(); i++) {

            //特定のセクションを取得する
            Section section = document.getSections().get(i);

            //セクション内の段落をループする
            for (int j = 0; j < section.getParagraphs().getCount(); j++)
            {
                //特定の段落を取得する
                Paragraph para = section.getParagraphs().get(j);

                //リストを作成する
                List footnotes = new ArrayList<>();

                //段落の中の子オブジェクトをループする
                for (int k = 0, cnt = para.getChildObjects().getCount(); k < cnt; k++)
                {
                    //特定の子オブジェクトを取得する
                    ParagraphBase pBase = (ParagraphBase)para.getChildObjects().get(k);

                    //子オブジェクトが脚注であるかどうかを判定する
                    if (pBase instanceof Footnote)
                    {
                        Footnote footnote = (Footnote)pBase;

                        //脚注をリストに追加する
                        footnotes.add(footnote);
                    }
                }
                if (footnotes != null) {

                    //リストにある脚注をループする
                    for (int y = 0; y < footnotes.size(); y++) {

                        //特定の脚注を削除する
                        para.getChildObjects().remove(footnotes.get(y));
                    }
                }
            }
        }

        //ドキュメントを保存する
        document.saveToFile("脚注の削除.docx", FileFormat.Docx);
    }
}

Java:Word ドキュメントに脚注を挿入または削除する方法

一時ライセンスを申請する

結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。

Read 701 times