チュートリアル
簡単にライブラリーを使用するためのチュートリアルコレクション
目次は Word ドキュメントのフィールドで、すべてのレベルのタイトルとそれに対応するページ番号を表示するために使用されます。ドキュメントの内容が変更された場合、目次フィールドを更新するだけで、新しいタイトルと対応するページ番号に従って新しい目次を生成することができます。 あなたがドキュメントを編集していても、ドキュメントを読んでいても、目次は大きな利便性を提供することができます。 同時に、目次はドキュメントをより専門的なものにすることもでき、これは科学、技術、学術、その他のドキュメントにとって重要な部分です。 今回は、Spire.Doc for Java を使って、Java プログラムを通して Word ドキュメントに目次を挿入する方法をご紹介します。
まず、Spire. Doc for Java を Java プロジェクトに追加する必要があります。JAR ファイルは、このリンクからダウンロードできます。Maven を使用する場合は、次のコードをプロジェクトの pom.xml ファイルに追加する必要があります。
<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</artifactId>
<version>11.7.0</version>
</dependency>
</dependencies>
目次を作成する前に、目次フィールドが見出し、見出しレベル、および対応するページ番号に基づいて目次を生成できるように、各見出しの見出しレベルを設定する必要があります。 見出しレベルは、Paragraph.getListFormat().setListLevelNumber(int) メソッドを使用して設定できます。
Spire.Doc for Java には、ドキュメントの段落に目次を挿入する Paragraph.appendTOC() メソッドが用意されています。 Word ドキュメントにデフォルト形式の目次を挿入する詳細な手順は次のとおりです。
import com.spire.doc.Document;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
public class insertTableOfContents {
public static void main(String[] args) {
//Documentクラスのオブジェクトを作成する
Document doc = new Document();
//Wordドキュメントを読み込む
doc.loadFromFile("サンプル.docx");
//セクションを追加し、ドキュメントの表紙セクションの後に挿入する
Section section = doc.addSection();
doc.getSections().insert(1, section);
//セクションに段落を追加する
Paragraph paragraph = section.addParagraph();
paragraph.appendText("目次\r\n");
paragraph.getStyle().getCharacterFormat().setFontSize(14f);
paragraph.getStyle().getCharacterFormat().setFontName("HarmonyOS Sans SC");
//この段落に2〜3レベルの見出しを表示するデフォルト形式の目次を作成する
paragraph.appendTOC(2, 3);
//目次を更新する
doc.updateTableOfContents();
//ファイルを保存する
doc.saveToFile("デフォルト形式の目次.docx");
doc.dispose();
}
}
Spire.Doc for Java では、Word ドキュメントの目次を表現するために TableOfContent クラスを使用しています。 TableOfContent クラスのオブジェクトを作成する際に、スイッチによって目次に表示する内容をカスタマイズすることができます。 例えば、スイッチ "{\o \"1-3\" \\1-2}" は、目次に第1レベルから第3レベルまでの見出しを表示し、第1レベルと第2レベルの見出しのページ番号は表示しない。
Word ドキュメントでカスタム書式の目次を作成する詳細な手順は次のとおりです。
import com.spire.doc.Document;
import com.spire.doc.Section;
import com.spire.doc.documents.FieldMarkType;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.TableOfContent;
public class insertTableOfContentsWithCustomizedStyle {
public static void main(String[] args) {
//Documentクラスのオブジェクトを作成する
Document doc = new Document();
//Wordドキュメントを読み込む
doc.loadFromFile("サンプル.docx");
//表紙セクションの後にセクションを挿入し、そのセクションに段落を追加する
Section section = doc.addSection();
doc.getSections().insert(1, section);
Paragraph paragraph = section.addParagraph();
paragraph.getStyle().getCharacterFormat().setFontSize(14f);
//TableOfContentクラスのオブジェクトを作成する
TableOfContent toc = new TableOfContent(doc, "{\\o \"1-3\" \\n 1-2}");
//作成した目次をその段落に挿入する
paragraph.getItems().add(toc);
//フィールドの区切り記号と終了記号を挿入して、目次フィールドを終了させる
paragraph.appendFieldMark(FieldMarkType.Field_Separator);
paragraph.appendFieldMark(FieldMarkType.Field_End);
//作成した目次をこのドキュメントの目次に設定する
doc.setTOC(toc);
//目次を更新する
doc.updateTableOfContents();
//ファイルを保存する
doc.saveToFile("カスタム形式の目次.docx");
doc.dispose();
}
}
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。
Word ドキュメントでは、他の人が情報を入力するために使用できるフォームを作成することができます。記入可能なフォームは、さまざまな用途で使用されます。人事部では、従業員やコンサルタントの情報を収集するためにフォームを使用します。営業部門は、製品やサービスに対する顧客の満足度を調査するためにフォームを使用します。組織は、会員、学生、顧客を登録するためにフォームを使用します。フォームを作成する際に使用するツールには、以下のようなものがあります:
Word ドキュメントのコンテンツコントロールは、ユーザーが構造化されたドキュメントを構築できるようにするためのコンテンツのコンテナです。構造化されたドキュメントでは、ドキュメント内でコンテンツが表示される場所を制御します。Word 2013 では、基本的に10種類のコンテンツコントロールが利用可能です。この記事では、Spire.Doc for Java を使用して、以下の7つの一般的なコンテンツコントロールで構成される Word ドキュメントに記入可能なフォームを作成する方法を中心に説明します。
コンテンツ コントロール | 説明 |
テキスト | テキストフィールドはプレーンテキストに限定されるため、書式を含めることはできません。 |
リッチ テキスト | テキストフィールドで、フォーマットされたテキストや、テーブル、画像、その他のコンテンツコントロールなどのアイテムを含むことができます。 |
画像 | 1枚の画像を含むことができます。 |
ドロップダウン リスト | ドロップダウンリストは、ユーザーが選択できるように、あらかじめ定義されたアイテムのリストを表示します。 |
コンボ ボックス | コンボボックスは、ユーザーがリストの中からあらかじめ定義された値を選択したり、コントロールのテキストボックスに独自の値を入力したりすることができるものです。 |
チェック ボックス | チェックボックスは、ユーザーが「はい(チェック済み)」か「いいえ(チェックなし)」かの二者択一を行うことができるグラフィカルなウィジェットです。 |
日付選択 | ユーザーが日付を選択できるカレンダーコントロールが含まれる。 |
まず、Spire. Doc for Java を Java プロジェクトに追加する必要があります。JAR ファイルは、このリンクからダウンロードできます。Maven を使用する場合は、次のコードをプロジェクトの pom.xml ファイルに追加する必要があります。
<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</artifactId>
<version>11.4.2</version>
</dependency>
</dependencies>
Spire.Doc for Java が提供する StructureDocumentTagInline クラスは、段落内のインラインレベルの構造(DrawingML オブジェクト、フィールドなど)に対する構造化文書タグを作成するために使用する。このクラスの下にある SDTProperties プロパティと SDTContent プロパティは、現在の構造化文書タグのプロパティとコンテンツを指定するために使用されるものとする。以下は、Word ドキュメントにコンテンツコントロールを備えた記入可能なフォームを作成するための詳細な手順です。
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.DocPicture;
import com.spire.doc.fields.TextRange;
import com.spire.pdf.graphics.PdfTrueTypeFont;
import java.util.Date;
public class CreateFillableForm {
public static void main(String[] args) {
//Documentのオブジェクトを作成する
Document doc = new Document();
//セクションを追加する
Section section = doc.addSection();
//表を追加する
Table table = section.addTable(true);
table.resetCells(7, 2);
//1列目のセルにテキストを追加する
Paragraph paragraph = table.getRows().get(0).getCells().get(0).addParagraph();
paragraph.getStyle().getCharacterFormat().setFontName("Yu Gothic UI");
paragraph.getStyle().getCharacterFormat().setFontSize(12);
paragraph.appendText("テキスト コンテンツ コントロール");
paragraph = table.getRows().get(1).getCells().get(0).addParagraph();
paragraph.appendText("リッチ テキスト コンテンツ コントロール");
paragraph = table.getRows().get(2).getCells().get(0).addParagraph();
paragraph.appendText("画像コンテンツ コントロール");
paragraph = table.getRows().get(3).getCells().get(0).addParagraph();
paragraph.appendText("ドロップダウン リスト コンテンツ コントロール");
paragraph = table.getRows().get(4).getCells().get(0).addParagraph();
paragraph.appendText("チェック ボックス コンテンツ コントロール");
paragraph = table.getRows().get(5).getCells().get(0).addParagraph();
paragraph.appendText("コンボ ボックス コンテンツ コントロール");
paragraph = table.getRows().get(6).getCells().get(0).addParagraph();
paragraph.appendText("日付選択コンテンツ コントロール");
//セル(0,1)にテキストコンテンツコントロールを追加する
paragraph = table.getRows().get(0).getCells().get(1).addParagraph();
StructureDocumentTagInline sdt = new StructureDocumentTagInline(doc);
paragraph.getChildObjects().add(sdt);
sdt.getSDTProperties().setSDTType(SdtType.Text);
sdt.getSDTProperties().setAlias("テキスト");
sdt.getSDTProperties().setTag("テキスト");
sdt.getSDTProperties().isShowingPlaceHolder(true);
SdtText text = new SdtText(true);
text.isMultiline(false);
sdt.getSDTProperties().setControlProperties(text);
TextRange tr = new TextRange(doc);
tr.setText("ここをクリックまたはタップしてテキストを入力してください。");
sdt.getSDTContent().getChildObjects().add(tr);
//セル(1,1)にリッチテキストコンテンツコントロールを追加する
paragraph = table.getRows().get(1).getCells().get(1).addParagraph();
sdt = new StructureDocumentTagInline(doc);
paragraph.getChildObjects().add(sdt);
sdt.getSDTProperties().setSDTType(SdtType.Rich_Text);
sdt.getSDTProperties().setAlias("リッチ テキスト");
sdt.getSDTProperties().setTag("リッチ テキスト");
sdt.getSDTProperties().isShowingPlaceHolder(true);
text = new SdtText(true);
text.isMultiline(false);
sdt.getSDTProperties().setControlProperties(text);
tr = new TextRange(doc);
tr.setText("ここをクリックまたはタップしてテキストを入力してください。");
sdt.getSDTContent().getChildObjects().add(tr);
//セル(2,1)に画像コンテンツコントロールを追加する
paragraph = table.getRows().get(2).getCells().get(1).addParagraph();
sdt = new StructureDocumentTagInline(doc);
paragraph.getChildObjects().add(sdt);
sdt.getSDTProperties().setSDTType(SdtType.Picture);
sdt.getSDTProperties().setAlias("画像");
sdt.getSDTProperties().setTag("画像");
SdtPicture sdtPicture = new SdtPicture();
sdt.getSDTProperties().setControlProperties(sdtPicture);
DocPicture pic = new DocPicture(doc);
pic.loadImage("C:/画像を選択.png");
sdt.getSDTContent().getChildObjects().add(pic);
//セル(3,1)にドロップダウンリストコンテンツコントロールを追加する
paragraph = table.getRows().get(3).getCells().get(1).addParagraph();
sdt = new StructureDocumentTagInline(doc);
sdt.getSDTProperties().setSDTType(SdtType.Drop_Down_List);
sdt.getSDTProperties().setAlias("ドロップダウン リスト");
sdt.getSDTProperties().setTag("ドロップダウン リスト");
paragraph.getChildObjects().add(sdt);
SdtDropDownList sddl = new SdtDropDownList();
sddl.getListItems().add(new SdtListItem("アイテムを選択してください。", "1"));
sddl.getListItems().add(new SdtListItem("アイテム2", "2"));
sddl.getListItems().add(new SdtListItem("アイテム3", "3"));
sddl.getListItems().add(new SdtListItem("アイテム4", "4"));
sdt.getSDTProperties().setControlProperties(sddl);
tr = new TextRange(doc);
tr.setText(sddl.getListItems().get(0).getDisplayText());
sdt.getSDTContent().getChildObjects().add(tr);
//セル(4,1)にチェックボックスのコンテンツコントロールを2つ追加する
paragraph = table.getRows().get(4).getCells().get(1).addParagraph();
sdt = new StructureDocumentTagInline(doc);
paragraph.getChildObjects().add(sdt);
sdt.getSDTProperties().setSDTType(SdtType.Check_Box);
SdtCheckBox scb = new SdtCheckBox();
sdt.getSDTProperties().setControlProperties(scb);
tr = new TextRange(doc);
sdt.getChildObjects().add(tr);
scb.setChecked(false);
paragraph.appendText("オプション1");
paragraph = table.getRows().get(4).getCells().get(1).addParagraph();
sdt = new StructureDocumentTagInline(doc);
paragraph.getChildObjects().add(sdt);
sdt.getSDTProperties().setSDTType(SdtType.Check_Box);
scb = new SdtCheckBox();
sdt.getSDTProperties().setControlProperties(scb);
tr = new TextRange(doc);
sdt.getChildObjects().add(tr);
scb.setChecked(false);
paragraph.appendText("オプション2");
//セル(5,1)にコンボボックスのコンテンツコントロールを追加する
paragraph = table.getRows().get(5).getCells().get(1).addParagraph();
sdt = new StructureDocumentTagInline(doc);
paragraph.getChildObjects().add(sdt);
sdt.getSDTProperties().setSDTType(SdtType.Combo_Box);
sdt.getSDTProperties().setAlias("コンボ ボックス");
sdt.getSDTProperties().setTag("コンボ ボックス");
SdtComboBox cb = new SdtComboBox();
cb.getListItems().add(new SdtListItem("アイテムを選択してください。"));
cb.getListItems().add(new SdtListItem("アイテム2"));
cb.getListItems().add(new SdtListItem("アイテム3"));
sdt.getSDTProperties().setControlProperties(cb);
tr = new TextRange(doc);
tr.setText(cb.getListItems().get(0).getDisplayText());
sdt.getSDTContent().getChildObjects().add(tr);
//セル(6,1)に日付ピッカーコンテンツコントロールを追加する
paragraph = table.getRows().get(6).getCells().get(1).addParagraph();
sdt = new StructureDocumentTagInline(doc);
paragraph.getChildObjects().add(sdt);
sdt.getSDTProperties().setSDTType(SdtType.Date_Picker);
sdt.getSDTProperties().setAlias("日付選択");
sdt.getSDTProperties().setTag("日付選択");
SdtDate date = new SdtDate();
date.setCalendarType(CalendarType.Default);
date.setDateFormat("yyyy.MM.dd");
date.setFullDate(new Date());
sdt.getSDTProperties().setControlProperties(date);
tr = new TextRange(doc);
tr.setText("クリックまたはタップして日付を入力してください。");
sdt.getSDTContent().getChildObjects().add(tr);
//ユーザーがフォームフィールドを編集することのみを許可する
doc.protect(ProtectionType.Allow_Only_Form_Fields, "password");
//ドキュメントを保存する
doc.saveToFile("記入用フォーム.docx", FileFormat.Docx_2013);
}
}
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。