チュートリアル
簡単にライブラリーを使用するためのチュートリアルコレクション
Word ドキュメントのすべての段落では、意図的または非意図的に段落スタイルが使用されます。段落スタイルは、見出し1や見出し2などの組み込みスタイルであることもあれば、カスタマイズされたスタイルであることもあります。この記事では、Spire.Doc for Java を使用して、特定のスタイルを使用している段落を抽出する方法を紹介します。
下表は、MS Word のスタイル名と Spire.Doc のスタイル名の対応表です。非常に簡単なルールとして、プログラミングによって返されるスタイル名にはスペースが含まれません。
MS Word のスタイル名 | Spire.Doc のスタイル名 |
表題 | Title |
副題 | Subtitle |
見出し1 | Heading1 |
見出し2 | Heading2 |
見出し3 | Heading3 |
行間詰め | NoSpacing |
引用文 | Quote |
引用文2 | IntenseQuote |
リスト段落 | ListParagraph |
標準 | Normal |
カスタム名 | CustomName |
まず、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>12.3.1</version>
</dependency>
</dependencies>
特定の段落のスタイル名は、Paragraph.getStyleName() メソッドで取得できます。段落のスタイル名がまさに照会したいものである場合は、Paragraph.getText() メソッドを使用して段落の内容を取得できます。以下は、特定のスタイルを使用している段落を抽出する手順です。
import com.spire.doc.Document;
import com.spire.doc.documents.Paragraph;
public class ExtractWordParagraphByStyle {
public static void main(String[] args) {
// Documentオブジェクトを初期化しながらサンプルのWordドキュメントをロードする
Document doc = new Document("サンプル.docx");
// 変数を宣言する
Paragraph paragraph;
// セクションをループする
for (int i = 0; i < doc.getSections().getCount(); i++) {
// 特定のセクションの段落をループする
for (int j = 0; j < doc.getSections().get(i).getParagraphs().getCount(); j++) {
// 特定の段落を取得する
paragraph = doc.getSections().get(i).getParagraphs().get(j);
// 段落のスタイルが「Heading 1」かどうかを判定する
if (paragraph.getStyleName().equals("Heading1")) {
// 「Heading 1」の段落のテキストを取得する
System.out.println("見出し1: " + paragraph.getText() + "\n");
}
// 段落のスタイルが「My Custom Style」かどうかを判定する
if (paragraph.getStyleName().equals("私のカスタムスタイル")) {
// 「私のカスタムスタイル」の段落のテキストを取得する
System.out.println("カスタムスタイル: " + paragraph.getText());
}
}
}
}
}
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。
MS Word では、重複するコンテンツや主題に関係のない情報を含む段落を削除することが非常に重要です。そうすることで、ドキュメントを簡素化し、情報の正確性を確保できます。この記事では、Spire.Doc for 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.3.11</version>
</dependency>
</dependencies>
すべての段落を削除するには、文書内すべてのセクションをループします。そして、Section.getParagraphs().clear() メソッドを使用して各セクションのすべての段落を削除できます。詳細な手順は次のとおりです。
import com.spire.doc.*;
public class removeAllParagraphs {
public static void main(String[] args) {
//Documentインスタンスを作成する
Document document = new Document();
//ディスクからWord 文書をロードする
document.loadFromFile("sample.docx");
//各セクション内のすべての段落を削除する
for ( Object sectionObj: document.getSections()) {
Section section = (Section)sectionObj;
section.getParagraphs().clear();
}
//結果文書を保存する
document.saveToFile("removeAllParagraphs.docx", FileFormat.Docx_2013);
}
}
重複した情報や役に立たない情報を含む段落を見つけた場合、Spire.Doc for Java では、Section.getParagraphs().removeAt() メソッドを使用して指定された段落を削除できます。詳細な手順は次のとおりです。
import com.spire.doc.*;
public class removeSpecificParagraph {
public static void main(String[] args) {
//Documentインスタンスを作成する
Document document = new Document();
//ディスクからWord 文書をロードする
document.loadFromFile("sample.docx");
//最初のセクションを取得する
Section section = document.getSections().get(0);
//セクションの3番目の段落を削除する
section.getParagraphs().removeAt(2);
//結果文書を保存する
document.saveToFile("removeSpecificParagraph.docx", FileFormat.Docx_2013);
}
}
文書に空の段落/行が多数ある場合は、読みやすくするためにそれらを削除すできます。以下は、Word 文書内のすべての空白の段落/行を削除する手順です。
import com.spire.doc.*;
import com.spire.doc.documents.*;
public class removeEmptyLines {
public static void main(String[] args) {
//Documentインスタンスを作成する
Document document = new Document();
//ディスクからWord 文書をロードする
document.loadFromFile("sample.docx");
//文書の各段落をループする
for (Object sectionObj : document.getSections()) {
Section section=(Section)sectionObj;
for (int i = 0; i < section.getBody().getChildObjects().getCount(); i++) {
if ((section.getBody().getChildObjects().get(i).getDocumentObjectType().equals(DocumentObjectType.Paragraph) )) {
String s= ((Paragraph)(section.getBody().getChildObjects().get(i))).getText().trim();
//段落が空白段落かどうかを判断する
if (s.isEmpty()) {
//空白段落を削除する
section.getBody().getChildObjects().remove(section.getBody().getChildObjects().get(i));
i--;
}
}
}
}
//結果文書を保存する
document.saveToFile("removeEmptyLines.docx", FileFormat.Docx_2013);
}
}
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。
Word ドキュメントを操作する過程で、重要な情報を他人に知られたくない場合があります。このため、それらを隠して機密性を確保することができます。この記事では、Spire.Doc for Java を使って Word ドキュメント内の特定の段落を非表示にする方法を紹介します。
まず、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>
Spire.Doc for Java では、TextRange.getCharacterFormat().setHidden(boolean value) メソッドを使用して、Word の特定の段落を非表示にすることができます。以下は、その詳細な手順です。
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
public class hideParagraph {
public static void main(String[] args) {
//Documentクラスのインスタンスを作成する
Document document = new Document();
//Wordドキュメントを読み込む
document.loadFromFile("C:/例.docx");
//Wordドキュメント内の特定のセクションを取得する
Section sec = document.getSections().get(0);
//セクションの特定の段落を取得する
Paragraph para = sec.getParagraphs().get(1);
//子オブジェクトをループする
for (Object docObj : para.getChildObjects()) {
DocumentObject obj = (DocumentObject)docObj;
//子オブジェクトが TextRange クラスのインスタンスであるかどうかを判定する
if ((obj instanceof TextRange)) {
TextRange range = ((TextRange)(obj));
//テキスト範囲を非表示にする
range.getCharacterFormat().setHidden(true);
}
}
//ドキュメントを保存する
document.saveToFile("段落の非表示.docx", FileFormat.Docx_2013);
}
}
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。
箇条書きリストとは、先頭に点があり、各項目のインデントが同じであるリストで、番号付きリストとは、先頭に数字があるリストのことです。箇条書きリストや番号付きリストは、よく整理されたリストであれば、読者は各項目の構成やポイントを容易に把握できるため、長いテキスト内容よりもはるかに効果的です。この記事では、Spire.Doc for Java を使って、Word ドキュメント内の既存のテキストからリストを作成する方法を紹介します。
まず、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>
Spire.Doc for Java では、ListFormat.applyBulletStyle() とListFormat.applyNumberedStyle() という、箇条書きと番号付きのリストを作成するためのメソッドが用意されています。
箇条書きと番号付きリストの詳しい作成手順は、以下の通りです。
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.formatting.ListFormat;
public class createLists {
public static void main(String[] args) {
//Document クラスのオブジェクトを作成する
Document document = new Document();
//Wordドキュメントを読み込む
document.loadFromFile("C:/例.docx");
//最初のセクションを取得する
Section section = document.getSections().get(0);
//4番目から6番目までの段落をループする
for(int i = 3; i <= 5; i++){
Paragraph para = section.getParagraphs().get(i);
ListFormat listFormat = para.getListFormat();
//箇条書きのスタイルを適用する
listFormat.applyBulletStyle();
//リストの位置を設定する
listFormat.getCurrentListLevel().setNumberPosition(-10);
}
//10番目から12番目までの段落をループさせる
for(int i = 9; i <= 11; i++){
Paragraph para = section.getParagraphs().get(i);
ListFormat listFormat = para.getListFormat();
//番号付きリストのスタイルを適用する
listFormat.applyNumberedStyle();
//リストの位置を設定する
listFormat.getCurrentListLevel().setNumberPosition(-10);
}
//ドキュメントを保存する
document.saveToFile("リストの作成.docx", FileFormat.Docx_2013);
}
}
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。
Microsoft 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>
Spire.Doc for Java では、段落書式を扱うために ParagraphFormat クラスが用意されています。Paragraph.getFormat() メソッドを使用して ParagraphFormat クラスのオブジェクトを取得し、ParagraphFormat.setHorizontalAlignment() メソッドを使用して段落のテキスト配置を設定することができます。
Word ドキュメントで段落のテキスト配置を設定する手順は次のとおりです。
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.formatting.ParagraphFormat;
public class setParagraphAlignment {
public static void main(String[] args) {
//Documentクラスのインスタンスを作成する
Document document = new Document();
//セクションを追加する
Section section = document.addSection();
//段落を追加し、左揃えに設定する
Paragraph para = section.addParagraph();
para.appendText("この段落の配置は、左揃えに設定されています。");
ParagraphFormat format = para.getFormat();
format.setHorizontalAlignment(HorizontalAlignment.Left);
//段落を追加し、中央揃えに設定する
para = section.addParagraph();
para.appendText("この段落の配置は、中央揃えに設定されています。");
format = para.getFormat();
format.setHorizontalAlignment(HorizontalAlignment.Center);
//段落を追加し、右揃えに設定する
para = section.addParagraph();
para.appendText("この段落の配置は、右揃えに設定されています。");
format = para.getFormat();
format.setHorizontalAlignment(HorizontalAlignment.Right);
//段落を追加し、両端揃えに設定する
para = section.addParagraph();
para.appendText("この段落の配置は、両端揃えに設定されています。");
format = para.getFormat();
format.setHorizontalAlignment(HorizontalAlignment.Justify);
//段落を追加し、分散に設定する。
para = section.addParagraph();
para.appendText("この段落の配置は、分散に設定されています。");
format = para.getFormat();
format.setHorizontalAlignment(HorizontalAlignment.Distribute);
//ドキュメントを保存する
document.saveToFile("段落の配置.docx", FileFormat.Docx_2013);
}
}
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。
Word ドキュメントでは、段落のインデントを左、右、1行目、ぶら下げに設定することができます。今回は、この機能を 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>
Spire.Doc for Java では、段落書式を扱うために ParagraphFormat クラスが用意されています。Paragraph.getFormat() メソッドを使用して ParagraphFormat クラスのオブジェクトを取得し、以下のメソッドを使用して、そのオブジェクトに対応する段落インデントを設定することができます。
インデント | メソード | 説明 |
左 | ParagraphFormat.setLeftIndent(float value) | 設定した量だけ段落を左にインデントします。 |
右 | ParagraphFormat.setRightIndent(float value) | 設定した量だけ段落を右へインデントします。 |
最初の行 | ParagraphFormat.setFirstLineIndent(float value) | 段落の最初の行をインデントします。 |
ぶら下がり | ParagraphFormat.setFirstLineIndent(float negativeValue) | 段落の最初の行を余白に合わせ、それ以降の段落の各行をインデントします。 |
段落のインデントを設定する手順は、以下のとおりです。
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.formatting.ParagraphFormat;
public class SetParagraphIndent {
public static void main(String[] args) {
//Documentクラスのインスタンスを作成する
Document document= new Document();
//Wordドキュメントを読み込む
document.loadFromFile("実例.docx");
//最初のセクションを取得する
Section section = document.getSections().get(0);
//2段落目を取得し、左インデントを設定する
Paragraph para = section.getParagraphs().get(1);
ParagraphFormat format = para.getFormat();
format.setLeftIndent(30);
//3段落目を取得し、右インデントを設定する
para = section.getParagraphs().get(2);
format = para.getFormat();
format.setRightIndent(30);
//4段落目を取得し、1行目のインデントを設定する
para = section.getParagraphs().get(3);
format = para.getFormat();
format.setFirstLineIndent(30);
//5段落目を取得し、ぶら下がりインデントを設定する
para = section.getParagraphs().get(4);
format = para.getFormat();
format.setFirstLineIndent(-30);
//結果のドキュメントを保存する
document.saveToFile("段落の字下げを設定.docx", FileFormat.Docx_2013);
}
}
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。