段落の操作
段落は、論理ブロックに結合され、特殊文字で終わる文字のセットです–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-C | |
// The path to the documents directory. | |
System::String outputDataDir = GetOutputDataDir_WorkingWithDocument(); | |
// Initialize document. | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
// Specify font formatting | |
System::SharedPtr<Font> font = builder->get_Font(); | |
font->set_Size(16); | |
font->set_Bold(true); | |
font->set_Color(System::Drawing::Color::get_Blue()); | |
font->set_Name(u"Arial"); | |
font->set_Underline(Underline::Dash); | |
// Specify paragraph formatting | |
System::SharedPtr<ParagraphFormat> paragraphFormat = builder->get_ParagraphFormat(); | |
paragraphFormat->set_FirstLineIndent(8); | |
paragraphFormat->set_Alignment(ParagraphAlignment::Justify); | |
paragraphFormat->set_KeepTogether(true); | |
builder->Writeln(u"A whole paragraph."); | |
System::String outputPath = outputDataDir + u"DocumentBuilderInsertParagraph.doc"; | |
doc->Save(outputPath); |
段落の書式設定
現在の段落の書式設定は、ParagraphFormatプロパティによって返されるParagraphFormatオブジェクトによって表されます。 このオブジェクトはMicrosoft Wordで利用可能な様々な段落書式設定プロパティをカプセル化します。 ClearFormattingを呼び出すことで、段落の書式をデフォルトの通常のスタイル、左揃えのインデントなし、間隔なし、境界線なし、陰影なしに簡単にリセットできます。
次のコード例は、段落の書式設定を設定する方法を示しています:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
// Set paragraph formatting properties | |
System::SharedPtr<ParagraphFormat> paragraphFormat = builder->get_ParagraphFormat(); | |
paragraphFormat->set_Alignment(ParagraphAlignment::Center); | |
paragraphFormat->set_LeftIndent(50); | |
paragraphFormat->set_RightIndent(50); | |
paragraphFormat->set_SpaceAfter(25); | |
// Output text | |
builder->Writeln(u"I'm a very nice formatted paragraph. I'm intended to demonstrate how the left and right indents affect word wrapping."); | |
builder->Writeln(u"I'm another nice formatted paragraph. I'm intended to demonstrate how the space after paragraph looks like."); | |
System::String outputPath = outputDataDir + u"DocumentBuilderSetFormatting.SetParagraphFormatting.doc"; | |
doc->Save(outputPath); |
段落スタイルの適用
フォントやParagraphFormatのような一部の書式設定オブジェクトはスタイルをサポートしています。 単一の組み込みまたはユーザー定義のスタイルは、名前、基本スタイル、フォント、スタイルの段落書式などの対応するスタイルプロパティを含むStyle
オブジ
さらに、Styleオブジェクトは、StyleIdentifier列挙値によって表されるロケールに依存しないスタイル識別子を返すStyleIdentifierプロパティを提供します。 重要なのは、Microsoft Wordの組み込みスタイルの名前が異なる言語にローカライズされていることです。 スタイル識別子を使用すると、文書の言語に関係なく正しいスタイルを見つけることができます。 列挙値は、次のようなMicrosoft Word組み込みスタイルに対応しますNormal, Heading 1, Heading 2 など。 すべてのユーザー定義スタイルにはStyleIdentifier.User値が割り当てられます。
次のコード例は、段落スタイルを適用する方法を示しています:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
// Set paragraph style | |
builder->get_ParagraphFormat()->set_StyleIdentifier(StyleIdentifier::Title); | |
builder->Write(u"Hello"); | |
System::String outputPath = outputDataDir + u"DocumentBuilderSetFormatting.ApplyParagraphStyle.doc"; | |
doc->Save(outputPath); |
別の段落スタイルを配置するには、スタイル区切り文字を挿入します
スタイルセパレータは、Ctrl+Alt+EnterキーボードショートカットをMS Wordに使用して段落の最後に追加することができます。 この機能を使用すると、1つの論理的に印刷された段落で使用される2つの異なる段落スタイルを使用できます。 特定の見出しの先頭からのテキストを目次に表示したいが、見出し全体を目次に表示したくない場合は、この機能を使用できます。
次のコード例は、さまざまな段落スタイルに対応するためにスタイル区切り文字を挿入する方法を示しています:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
System::SharedPtr<Style> paraStyle = builder->get_Document()->get_Styles()->Add(StyleType::Paragraph, u"MyParaStyle"); | |
paraStyle->get_Font()->set_Bold(false); | |
paraStyle->get_Font()->set_Size(8); | |
paraStyle->get_Font()->set_Name(u"Arial"); | |
// Append text with "Heading 1" style. | |
builder->get_ParagraphFormat()->set_StyleIdentifier(StyleIdentifier::Heading1); | |
builder->Write(u"Heading 1"); | |
builder->InsertStyleSeparator(); | |
// Append text with another style. | |
builder->get_ParagraphFormat()->set_StyleName(paraStyle->get_Name()); | |
builder->Write(u"This is text with some other formatting "); | |
System::String outputPath = outputDataDir + u"InsertStyleSeparator.doc"; | |
// Save the document to disk. | |
doc->Save(outputPath); |
段落スタイルの区切り文字を識別する
Aspose.WordsはパブリックプロパティBreakIsStyleSeparatorをParagraph
クラスに提供し、以下の例に示すようにスタイル区切り段落を識別できます:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
// The path to the documents directory. | |
System::String inputDataDir = GetInputDataDir_RenderingAndPrinting(); | |
// Initialize document. | |
System::String fileName = u"TestFile.doc"; | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + fileName); | |
for (System::SharedPtr<Paragraph> paragraph : System::IterateOver<Paragraph>(doc->GetChildNodes(NodeType::Paragraph, true))) | |
{ | |
if (paragraph->get_BreakIsStyleSeparator()) | |
{ | |
std::cout << "Separator Found!" << std::endl; | |
} | |
} |
段落に罫線と陰影を適用する
Aspose.Wordsの境界線はBorderCollectionクラスによって表されます–これはインデックスまたは境界線の種類によってアクセスされるBorderオブジェクトのコレクションです。 境界線の型はBorderType
列挙体で表されます。 列挙体の一部の値は、複数または1つのドキュメント要素にのみ適用できます。 たとえば、BorderType.Bottomは段落または表のセルに適用できますが、BorderType.DiagonalDownは表のセル内の対角線の境界線のみを指定します。
Borderコレクションとそれぞれの境界線の両方には、色、線のスタイル、線の幅、テキストからの距離、オプションの影などの同様の属性があります。 それらは同じ名前のプロパティによって表されます。 プロパティ値を組み合わせることで、さまざまな境界線の種類を実現できます。 さらに、BorderCollectionオブジェクトとBorderオブジェクトの両方で、ClearFormattingメソッドを呼び出すことでこれらの値をデフォルトにリセットできます。
Aspose.Wordsには、文書要素のシェーディング属性が含まれているShadingクラスもあります。 必要なシェーディングテクスチャと、要素の背景と前景に適用される色を設定できます。
シェーディングテクスチャは、Shadingオブジェクトにさまざまなパターンを適用できるTextureIndex列挙値で設定されます。 たとえば、ドキュメント要素の背景色を設定するには、TextureIndex.TextureSolidの値を使用し、必要に応じて前景の陰影の色を設定します。
次のコード例は、段落に罫線と陰影を適用する方法を示しています:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
// Set paragraph borders | |
System::SharedPtr<BorderCollection> borders = builder->get_ParagraphFormat()->get_Borders(); | |
borders->set_DistanceFromText(20); | |
borders->idx_get(BorderType::Left)->set_LineStyle(LineStyle::Double); | |
borders->idx_get(BorderType::Right)->set_LineStyle(LineStyle::Double); | |
borders->idx_get(BorderType::Top)->set_LineStyle(LineStyle::Double); | |
borders->idx_get(BorderType::Bottom)->set_LineStyle(LineStyle::Double); | |
// Set paragraph shading | |
System::SharedPtr<Shading> shading = builder->get_ParagraphFormat()->get_Shading(); | |
shading->set_Texture(TextureIndex::TextureDiagonalCross); | |
shading->set_BackgroundPatternColor(System::Drawing::Color::get_LightCoral()); | |
shading->set_ForegroundPatternColor(System::Drawing::Color::get_LightSalmon()); | |
builder->Write(u"I'm a formatted paragraph with double border and nice shading."); | |
System::String outputPath = outputDataDir + u"DocumentBuilderSetFormatting.ApplyBordersAndShadingToParagraph.doc"; | |
doc->Save(outputPath); |