표 스타일 적용
표 스타일은 표에 쉽게 적용할 수 있는 서식 집합을 정의합니다. 테두리, 음영, 정렬, 글꼴 등의 서식을 표 스타일로 설정하고 일관된 모양을 위해 여러 표에 적용할 수 있습니다.
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 열거 유형의 각 값에 대해 하나의 항목을 나타내는 영구 항목 집합이 포함되어 있습니다. 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); |