Tablo Stili Uygula

Bir tablo stili, kolayca bir tabloya uygulanabilen bir dizi biçimlendirme tanımlamaktadır. Sınır, gölgelendirme, hizalama ve yazı tipi gibi biçimlendirme bir tablo tarzında ayarlanabilir ve birçok tabloya tutarlı görünüm sağlamak için uygulanabilir.

Aspose.Words bir tabloya tablo stilini uygulamayı ve ayrıca herhangi bir tablo stilinin özelliklerini okumayı destekler. Tablo stilleri aşağıdaki yollarla yükleme ve kaydetme sırasında korunur:

  • DOCX ve WordML formatlarında tablo stilleri yükleme ve kaydetme sırasında korunur
  • Tablo stilleri, DOC formatında yükleme ve kaydetme yapıldığında korunur (ama başka hiçbir formata değil)
  • Diğer formatlara dışa aktarırken veya yazdırırken tablo stilleri doğrudan tablo biçimlendirmesine genişletilir, böylece tüm biçimlendirme korunur

Bir Tablo Stili Oluşturun

Kullanıcı yeni bir stil oluşturabilir ve stil koleksiyonuna ekleyebilir. The Add yöntemi yeni bir tablo stili oluşturmak için kullanılır.

Aşağıdaki kod örneği yeni bir kullanıcı tanımlı tablo stilinin nasıl oluşturulacağını göstermektedir:

// 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");

Mevcut Bir Tablo Stilini Kopyala

Gerekirse, şu anki bir dosyadaki zaten var olan bir tablo stilini stil koleksiyonunuza AddCopy yöntemiyle kopyalayabilirsiniz.

Bunu bilmek önemlidir ki bu kopyalama ile birbirine bağlı olan stiller de kopyalanır.

Aşağıdaki kod örneği bir stilin bir belgeden başka bir belgeye nasıl içe aktarılacağını göstermektedir:

// 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());

Mevcut Bir Masa Stilini Uygula

Aspose.Words TableStyle‘ı Style sınıfından devralır. TableStyle kullanıcıya gölgelendirme, dolgu, girintileme, CellSpacing ve Font gibi farklı stil seçeneklerini uygulamaya olanak tanır.

Bunun yanı sıra, Aspose.Words sağladığı StyleCollection sınıfı ve Table sınıfının birkaç özelliğini belirtir ki hangi tablo stilini kullanacağımızı: Style, StyleIdentifier, StyleName ve StyleOptions.

Aspose.Words ayrıca ConditionalStyle sınıfını sağlar, bu da bir tablodaki belirli bir alanın atanan bir tablo stiline uygulanan özel biçimlendirmeyi temsil eder ve ConditionalStyleCollection bir koleksiyonu ConditionalStyle nesneyi temsil eder. Bu koleksiyon ConditionalStyleType numaralandırma türünün her değeri için bir öğe temsil eden kalıcı bir öğeler kümesini içerir. The ConditionalStyleType numaralandırma, bir tablo stilinde hangi koşullu biçimlendirmenin tanımlanabileceğini belirten tüm olası tablo alanlarını tanımlar.

Bu durumda, tüm olası tablo alanı tanımlanmış ‘ConditionalStyleType’ numaralandırma türünün altında koşullu biçimlendirme tanımlanabilir.

Aşağıdaki kod örneği, tablonun başlık satırına nasıl koşullu biçimlendirme tanımlanacağını göstermektedir:

// 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");

Sınıflara stil uygulamak istediğiniz tablo kısımlarını da seçebilirsiniz, örneğin ilk sütun veya son sütun, çizgili satırlar. Onlar TableStyleOptions numaralandırmada listelenir ve StyleOptions özelliği aracılığıyla uygulanır. Bu TableStyleOptions numaralandırma bu özelliklerin bir bitwise kombinasyonu sağlar.

Aşağıdaki kod örneği bir tablo stili uygulandıran yeni bir tablonun nasıl oluşturulacağını göstermektedir:

// 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");

Aşağıdaki resimler, Table Styles ‘nin Microsoft Word’deki gösterimini ve bunların karşılık gelen özellikleri Aspose.Words gösterir.

table-style-aspose-words-java

Masa Stilinden Biçimlendirme Al ve Doğrudan Biçimlendirme olarak Uygula

Aspose.Words ayrıca bir tablo stilinde bulunan biçimlendirmeyi alarak onu tablo satırlarına ve hücrelerine doğrudan biçimlendirme olarak genişleten ' ExpandTableStylesToDirectFormatting’ yöntemini de sağlar. Biçimlendirmeyi tablo stili ve hücre stiliyle birleştirmeyi dene.

Aşağıdaki kod örneği, biçimi stillerden masraf satırlarına ve hücrelere doğrudan biçimlendirme olarak genişletmenin nasıl yapılacağını göstermektedir:

// 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);