테이블 스타일 적용

테이블 스타일은 테이블에 쉽게 적용할 수 있는 서식 집합을 정의합니다. 테두리,음영,정렬 및 글꼴과 같은 서식을 테이블 스타일로 설정하고 일관된 모양을 위해 많은 테이블에 적용할 수 있습니다.

Aspose.Words테이블에 테이블 스타일을 적용하고 모든 테이블 스타일의 속성을 읽을 수 있습니다. 테이블 스타일은 다음과 같은 방법으로 로드 및 저장하는 동안 유지됩니다:

  • DOCX및WordML형식의 테이블 스타일은 이러한 형식으로 로드 및 저장할 때 유지됩니다
  • 테이블 스타일은DOC형식으로 로드 및 저장할 때 보존됩니다(다른 형식은 아님).
  • 다른 형식(렌더링 또는 인쇄)으로 내보낼 때 테이블 스타일이 테이블의 직접 형식으로 확장되므로 모든 서식이 유지됩니다

테이블 스타일 만들기

사용자는 새로운 스타일을 만들고 스타일 컬렉션에 추가 할 수 있습니다. Add메서드는 새 테이블 스타일을 만드는 데 사용됩니다.

다음 코드 예제에서는 새 사용자 정의 테이블 스타일을 만드는 방법을 보여 줍니다:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.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.getStyles().add(StyleType.TABLE, "MyTableStyle1");
tableStyle.getBorders().setLineStyle(LineStyle.DOUBLE);
tableStyle.getBorders().setLineWidth(1.0);
tableStyle.setLeftPadding(18.0);
tableStyle.setRightPadding(18.0);
tableStyle.setTopPadding(12.0);
tableStyle.setBottomPadding(12.0);
table.setStyle(tableStyle);
doc.save(getArtifactsDir() + "WorkingWithTableStylesAndFormatting.CreateTableStyle.docx");

기존 테이블 스타일 복사

필요한 경우AddCopy메서드를 사용하여 특정 문서에 이미 있는 테이블 스타일을 스타일 컬렉션에 복사할 수 있습니다.

이 복사를 사용하면 연결된 스타일도 복사된다는 것을 아는 것이 중요합니다.

다음 코드 예제에서는 한 문서에서 다른 문서로 스타일을 가져오는 방법을 보여 줍니다:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document srcDoc = new Document();
// Create a custom style for the source document.
Style srcStyle = srcDoc.getStyles().add(StyleType.PARAGRAPH, "MyStyle");
srcStyle.getFont().setColor(Color.RED);
// Import the source document's custom style into the destination document.
Document dstDoc = new Document();
Style newStyle = dstDoc.getStyles().addCopy(srcStyle);
// The imported style has an appearance identical to its source style.
Assert.assertEquals("MyStyle", newStyle.getName());
Assert.assertEquals(Color.RED.getRGB(), newStyle.getFont().getColor().getRGB());

기존 테이블 스타일 적용

Aspose.Words는Style클래스에서 상속된TableStyle를 제공합니다. TableStyle사용자가 음영,패딩,들여 쓰기,CellSpacingFont등과 같은 다양한 스타일 옵션을 적용 할 수 있습니다.

또한Aspose.Words는StyleCollection클래스와Table클래스의 몇 가지 속성을 제공하여 작업할 테이블 스타일을 지정합니다: Style, StyleIdentifier, StyleName, 그리고StyleOptions

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-Java.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.getStyles().add(StyleType.TABLE, "MyTableStyle1");
tableStyle.getConditionalStyles().getFirstRow().getShading().setBackgroundPatternColor(Color.yellow);
tableStyle.getConditionalStyles().getFirstRow().getShading().setTexture(TextureIndex.TEXTURE_NONE);
table.setStyle(tableStyle);
doc.save(getArtifactsDir() + "WorkingWithTableStylesAndFormatting.DefineConditionalFormatting.docx");

첫 번째 열,마지막 열,줄무늬 행과 같이 스타일을 적용할 테이블 파트를 선택할 수도 있습니다. TableStyleOptions열거형에 나열되며StyleOptions속성을 통해 적용됩니다. TableStyleOptions열거는 이러한 기능의 비트 조합을 허용합니다.

다음 코드 예제에서는 테이블 스타일이 적용된 새 테이블을 만드는 방법을 보여 줍니다:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.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.setStyleIdentifier(StyleIdentifier.MEDIUM_SHADING_1_ACCENT_1);
// Apply which features should be formatted by the style.
table.setStyleOptions(TableStyleOptions.FIRST_COLUMN | TableStyleOptions.ROW_BANDS | TableStyleOptions.FIRST_ROW);
table.autoFit(AutoFitBehavior.AUTO_FIT_TO_CONTENTS);
builder.writeln("Item");
builder.getCellFormat().setRightPadding(40.0);
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(getArtifactsDir() + "WorkingWithTableStylesAndFormatting.BuildTableWithStyle.docx");

아래 그림은Microsoft Word의Table Styles과Aspose.Words의 해당 속성을 나타냅니다.

table-style-aspose-words-java

표 스타일에서 서식을 가져와 직접 서식으로 적용

Aspose.Words는 또한ExpandTableStylesToDirectFormatting메서드를 제공하여 테이블 스타일에서 찾은 서식을 가져와 테이블의 행과 셀에 직접 서식으로 확장합니다. 서식을 표 스타일 및 셀 스타일과 결합해 보십시오.

다음 코드 예제에서는 스타일에서 테이블 행 및 셀로 서식을 직접 서식으로 확장하는 방법을 보여 줍니다:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document doc = new Document(getMyDir() + "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.getFirstRow().getFirstCell();
// 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.getCellFormat().getShading().getBackgroundPatternColor();
System.out.println("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.getCellFormat().getShading().getBackgroundPatternColor();
System.out.println("Cell shading after style expansion: " + cellShadingAfter);