段落の操作

段落は、論理ブロックに結合され、特殊文字で終わる文字のセットです–aparagraph break。 Aspose.Wordsでは、段落はParagraphクラスで表されます。

段落の挿入

文書に新しい段落を挿入するには、実際には段落区切り文字を挿入する必要があります。 DocumentBuilder.Writelnテキストの文字列をドキュメントに挿入するだけでなく、段落区切りも追加します。

現在のフォントの書式設定はFontプロパティでも指定され、現在の段落の書式設定はParagraphFormatプロパティで決定されます。 次のセクションでは、段落の書式設定について詳しく説明します。

次のコード例は、文書に段落を挿入する方法を示しています:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// Open the document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Font font = builder.getFont();
font.setSize(16);
font.setColor(Color.DARK_GRAY);
font.setBold(true);
font.setName("Algerian");
font.setUnderline(2);
ParagraphFormat paragraphFormat = builder.getParagraphFormat();
paragraphFormat.setFirstLineIndent(12);
paragraphFormat.setAlignment(1);
paragraphFormat.setKeepTogether(true);
builder.write("This is a sample Paragraph");
doc.save(dataDir + "InsertParagraph_out.doc");

段落の書式設定

現在の段落の書式設定は、ParagraphFormatプロパティによって返されるParagraphFormatオブジェクトによって表されます。 このオブジェクトはMicrosoft Wordで利用可能な様々な段落書式設定プロパティをカプセル化します。 ClearFormattingを呼び出すことで、段落の書式をデフォルト(通常のスタイル、左揃え、インデントなし、間隔なし、境界線なし、陰影なし)に簡単にリセットできます。

次のコード例は、段落の書式設定を設定する方法を示しています:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// Open the document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
ParagraphFormat paragraphFormat = builder.getParagraphFormat();
paragraphFormat.setAlignment(ParagraphAlignment.CENTER);
paragraphFormat.setLeftIndent(50);
paragraphFormat.setRightIndent(50);
paragraphFormat.setSpaceAfter(25);
paragraphFormat.setKeepTogether(true);
builder.writeln(
"I'm a very nice formatted paragraph. I'm intended to demonstrate how the left and right indents affect word wrapping.");
builder.writeln(
"I'm another nice formatted paragraph. I'm intended to demonstrate how the space after paragraph looks like.");
doc.save(dataDir + "SetParagraphFormatting_out.doc");

段落スタイルの適用

フォントやParagraphFormatのような一部の書式設定オブジェクトはスタイルをサポートしています。 単一の組み込みまたはユーザー定義のスタイルは、名前、基本スタイル、フォント、スタイルの段落書式などの対応するスタイルプロパティを含むStyleオブジ

さらに、Styleオブジェクトは、StyleIdentifier列挙値によって表されるロケールに依存しないスタイル識別子を返すStyleIdentifierプロパティを提供します。 重要なのは、Microsoft Wordの組み込みスタイルの名前が異なる言語にローカライズされていることです。 スタイル識別子を使用すると、文書の言語に関係なく正しいスタイルを見つけることができます。 列挙値は、次のようなMicrosoft Word組み込みスタイルに対応しますNormal, Heading 1, Heading 2, など。 すべてのユーザー定義スタイルにはStyleIdentifier.User valueが割り当てられます。

次のコード例は、段落スタイルを適用する方法を示しています:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// Open the document.
Document doc = new Document();
// Set paragraph style
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.TITLE);
builder.write("Hello");
doc.save(dataDir + "ApplyParagraphStyle_out.doc");

別の段落スタイルを配置するには、スタイル区切り文字を挿入します

スタイルセパレータは、Ctrl+Alt+EnterキーボードショートカットをMSWordに使用して段落の最後に追加できます。 この機能を使用すると、1つの論理的に印刷された段落で使用される2つの異なる段落スタイルを使用できます。 特定の見出しの先頭からのテキストを目次に表示したいが、見出し全体を目次に表示したくない場合は、この機能を使用できます。

次のコード例は、さまざまな段落スタイルに対応するためにスタイル区切り文字を挿入する方法を示しています:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Style paraStyle = builder.getDocument().getStyles().add(StyleType.PARAGRAPH, "MyParaStyle");
paraStyle.getFont().setBold(false);
paraStyle.getFont().setSize(8);
paraStyle.getFont().setName("Arial");
// Append text with "Heading 1" style.
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1);
builder.write("Heading 1");
builder.insertStyleSeparator();
// Append text with another style.
builder.getParagraphFormat().setStyleName(paraStyle.getName());
builder.write("This is text with some other formatting ");
dataDir = dataDir + "InsertStyleSeparator_out.doc";
doc.save(dataDir);

段落に罫線と陰影を適用する

Aspose.Wordsの境界線はBorderCollectionクラスによって表されます–これはインデックスまたは境界線の種類によってアクセスされるBorderオブジェクトのコレクションです。 Border型はBorderType列挙型で表されます。 列挙体の一部の値は、複数または1つのドキュメント要素にのみ適用できます。 たとえば、BorderType.Bottomは段落または表のセルに適用できますが、BorderType.DiagonalDownは表のセル内の対角線の境界線のみを指定します。

Borderコレクションとそれぞれの境界線の両方には、色、線のスタイル、線の幅、テキストからの距離、オプションの影などの同様の属性があります。 それらは同じ名前のプロパティによって表されます。 プロパティ値を組み合わせることで、さまざまな境界線の種類を実現できます。 さらに、BorderCollectionオブジェクトとBorderオブジェクトの両方で、ClearFormattingメソッドを呼び出すことでこれらの値を既定値にリセットできます。

Aspose.Wordsには、文書要素のシェーディング属性が含まれているShadingクラスもあります。 必要なシェーディングテクスチャと、要素の背景と前景に適用される色を設定できます。

シェーディングテクスチャはTextureIndex列挙値で設定され、Shadingオブジェクトにさまざまなパターンを適用できます。 たとえば、ドキュメント要素の背景色を設定するには、TextureIndex.TextureSolidの値を使用し、必要に応じて前景の陰影の色を設定します。 次のコード例は、段落に境界線と陰影を適用する方法を示しています。

次のコード例は、段落に罫線と陰影を適用する方法を示しています:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// Open the document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Set paragraph borders
BorderCollection borders = builder.getParagraphFormat().getBorders();
borders.setDistanceFromText(20);
borders.getByBorderType(BorderType.LEFT).setLineStyle(LineStyle.DOUBLE);
borders.getByBorderType(BorderType.RIGHT).setLineStyle(LineStyle.DOUBLE);
borders.getByBorderType(BorderType.TOP).setLineStyle(LineStyle.DOUBLE);
borders.getByBorderType(BorderType.BOTTOM).setLineStyle(LineStyle.DOUBLE);
// Set paragraph shading
Shading shading = builder.getParagraphFormat().getShading();
shading.setTexture(TextureIndex.TEXTURE_DIAGONAL_CROSS);
shading.setBackgroundPatternColor(Color.YELLOW);
shading.setForegroundPatternColor(Color.GREEN);
builder.write("I'm a formatted paragraph with double border and nice shading.");
doc.save(dataDir + "ApplyBordersAndShading_out.doc");