テーブルスタイルを適用
表スタイルは、表に簡単に適用できる一連の書式設定を定義します。枠線、網掛け、配置、フォントなどの書式設定を表スタイルで設定し、多くの表に適用して一貫した外観を実現できます。
Aspose.Words は、テーブル スタイルのテーブルへの適用と、任意のテーブル スタイルのプロパティの読み取りをサポートします。表のスタイルは、ロードおよび保存中に次の方法で保持されます。
- DOCX および WordML 形式の表スタイルは、これらの形式にロードおよび保存するときに保持されます。
- DOC 形式でロードおよび保存する場合、テーブル スタイルは保持されます (ただし、他の形式では保持されません)。
- 他の形式にエクスポートする場合、レンダリングまたは印刷する場合、表スタイルは表内の直接書式設定に拡張されるため、すべての書式設定が保持されます。
表スタイルを作成する
ユーザーは新しいスタイルを作成し、スタイル コレクションに追加できます。 Add メソッドは、新しいテーブル スタイルを作成するために使用されます。
次のコード例は、新しいユーザー定義のテーブル スタイルを作成する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Table table = builder.StartTable(); | |
builder.InsertCell(); | |
builder.Write("Name"); | |
builder.InsertCell(); | |
builder.Write("Value"); | |
builder.EndRow(); | |
builder.InsertCell(); | |
builder.InsertCell(); | |
builder.EndTable(); | |
TableStyle tableStyle = (TableStyle) doc.Styles.Add(StyleType.Table, "MyTableStyle1"); | |
tableStyle.Borders.LineStyle = LineStyle.Double; | |
tableStyle.Borders.LineWidth = 1; | |
tableStyle.LeftPadding = 18; | |
tableStyle.RightPadding = 18; | |
tableStyle.TopPadding = 12; | |
tableStyle.BottomPadding = 12; | |
table.Style = tableStyle; | |
doc.Save(ArtifactsDir + "WorkingWithTableStylesAndFormatting.CreateTableStyle.docx"); |
既存の表スタイルをコピーする
必要に応じて、AddCopy
メソッドを使用して、特定のドキュメントに既に存在する表スタイルをスタイル コレクションにコピーできます。
このコピーでは、リンクされたスタイルもコピーされることに注意してください。
次のコード例は、あるドキュメントから別のドキュメントにスタイルをインポートする方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document srcDoc = new Document(); | |
// Create a custom style for the source document. | |
Style srcStyle = srcDoc.Styles.Add(StyleType.Paragraph, "MyStyle"); | |
srcStyle.Font.Color = Color.Red; | |
// Import the source document's custom style into the destination document. | |
Document dstDoc = new Document(); | |
Style newStyle = dstDoc.Styles.AddCopy(srcStyle); | |
// The imported style has an appearance identical to its source style. | |
Assert.AreEqual("MyStyle", newStyle.Name); | |
Assert.AreEqual(Color.Red.ToArgb(), newStyle.Font.Color.ToArgb()); |
既存の表スタイルを適用する
Aspose.Words は、Style クラスから継承された TableStyle を提供します。 TableStyle を使用すると、ユーザーはシェーディング、パディング、インデント、CellSpacing、Font などのさまざまなスタイル オプションを簡単に適用できます。
さらに、Aspose.Words は、Style、StyleIdentifier、StyleName、StyleOptions のどのテーブル スタイルを使用するかを指定するための StyleCollection クラスと Table
クラスのいくつかのプロパティを提供します。
Aspose.Words は、テーブル スタイルが割り当てられたテーブルの一部の領域に適用される特別な書式設定を表す ConditionalStyle クラスと、ConditionalStyle オブジェクトのコレクションを表す ConditionalStyleCollection も提供します。このコレクションには、ConditionalStyleType 列挙型の値ごとに 1 つの項目を表す項目の永続的なセットが含まれています。 ConditionalStyleType 列挙は、表スタイルで条件付き書式を定義できるすべての可能な表領域を定義します。
この場合、条件付き書式設定は、ConditionalStyleType 列挙型で定義されたすべての可能な表領域に対して定義できます。
次のコード例は、テーブルのヘッダー行の条件付き書式設定を定義する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Table table = builder.StartTable(); | |
builder.InsertCell(); | |
builder.Write("Name"); | |
builder.InsertCell(); | |
builder.Write("Value"); | |
builder.EndRow(); | |
builder.InsertCell(); | |
builder.InsertCell(); | |
builder.EndTable(); | |
TableStyle tableStyle = (TableStyle) doc.Styles.Add(StyleType.Table, "MyTableStyle1"); | |
tableStyle.ConditionalStyles.FirstRow.Shading.BackgroundPatternColor = Color.GreenYellow; | |
tableStyle.ConditionalStyles.FirstRow.Shading.Texture = TextureIndex.TextureNone; | |
table.Style = tableStyle; | |
doc.Save(ArtifactsDir + "WorkingWithTableStylesAndFormatting.DefineConditionalFormatting.docx"); |
最初の列、最後の列、帯状の行など、スタイルを適用する表の部分を選択することもできます。これらは TableStyleOptions 列挙にリストされ、StyleOptions プロパティを通じて適用されます。 TableStyleOptions 列挙では、これらの機能をビットごとに組み合わせることができます。
次のコード例は、テーブル スタイルが適用された新しいテーブルを作成する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Table table = builder.StartTable(); | |
// We must insert at least one row first before setting any table formatting. | |
builder.InsertCell(); | |
// Set the table style used based on the unique style identifier. | |
table.StyleIdentifier = StyleIdentifier.MediumShading1Accent1; | |
// Apply which features should be formatted by the style. | |
table.StyleOptions = | |
TableStyleOptions.FirstColumn | TableStyleOptions.RowBands | TableStyleOptions.FirstRow; | |
table.AutoFit(AutoFitBehavior.AutoFitToContents); | |
builder.Writeln("Item"); | |
builder.CellFormat.RightPadding = 40; | |
builder.InsertCell(); | |
builder.Writeln("Quantity (kg)"); | |
builder.EndRow(); | |
builder.InsertCell(); | |
builder.Writeln("Apples"); | |
builder.InsertCell(); | |
builder.Writeln("20"); | |
builder.EndRow(); | |
builder.InsertCell(); | |
builder.Writeln("Bananas"); | |
builder.InsertCell(); | |
builder.Writeln("40"); | |
builder.EndRow(); | |
builder.InsertCell(); | |
builder.Writeln("Carrots"); | |
builder.InsertCell(); | |
builder.Writeln("50"); | |
builder.EndRow(); | |
doc.Save(ArtifactsDir + "WorkingWithTableStylesAndFormatting.BuildTableWithStyle.docx"); |
以下の図は、Microsoft Word での Table Styles の表現と、Aspose.Words での対応するプロパティを示しています。
テーブル スタイルから書式設定を取得し、直接書式設定として適用します
Aspose.Words は、テーブル スタイルで見つかった書式設定を取得し、それを直接書式設定としてテーブルの行とセルに展開する ExpandTableStylesToDirectFormatting メソッドも提供します。書式設定とテーブル スタイルおよびセル スタイルを組み合わせてみてください。
次のコード例は、スタイルの書式設定を直接書式設定としてテーブルの行とセルに拡張する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(MyDir + "Tables.docx"); | |
// Get the first cell of the first table in the document. | |
Table table = (Table) doc.GetChild(NodeType.Table, 0, true); | |
Cell firstCell = table.FirstRow.FirstCell; | |
// First print the color of the cell shading. | |
// This should be empty as the current shading is stored in the table style. | |
Color cellShadingBefore = firstCell.CellFormat.Shading.BackgroundPatternColor; | |
Console.WriteLine("Cell shading before style expansion: " + cellShadingBefore); | |
doc.ExpandTableStylesToDirectFormatting(); | |
// Now print the cell shading after expanding table styles. | |
// A blue background pattern color should have been applied from the table style. | |
Color cellShadingAfter = firstCell.CellFormat.Shading.BackgroundPatternColor; | |
Console.WriteLine("Cell shading after style expansion: " + cellShadingAfter); |