適用表格格式化

每個表格元素都可以應用不同的格式化處理。 例如,表格格式將適用於整個表格,而行格式僅適用於特定行,而單元格格式僅適用於某些單元格。

Aspose.Words 提供一個豐富的 API 來檢索和應用格式設定於一個表格。 您可以利用 TableRowFormatCellFormat 節點來設定格式。

在本文中,我們將討論如何對不同的表格節點應用格式化以及表格格式設定 Aspose.Words 支援哪些。

對不同節點施加格式化

在本節中,我們將看看如何在各種表格節點上應用格式。

表格層級格式化

若要為表格應用格式設定,您可以使用對應的 Table 節點上可用的屬性,並使用 TablePreferredWidthTableCollection 類別。

下面這些圖片展示了 Table格式特點在 Microsoft Word中的表現及其對應的 Aspose.Words特性。

formattin-features-table-level-aspose-words-net

formatting-table-options-aspose-words-net

接下來的程式碼範例示範了如何將輪廓邊框應用到一個表格上:

// 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");
Table table = (Table) doc.GetChild(NodeType.Table, 0, true);
// Align the table to the center of the page.
table.Alignment = TableAlignment.Center;
// Clear any existing borders from the table.
table.ClearBorders();
// Set a green border around the table but not inside.
table.SetBorder(BorderType.Left, LineStyle.Single, 1.5, Color.Green, true);
table.SetBorder(BorderType.Right, LineStyle.Single, 1.5, Color.Green, true);
table.SetBorder(BorderType.Top, LineStyle.Single, 1.5, Color.Green, true);
table.SetBorder(BorderType.Bottom, LineStyle.Single, 1.5, Color.Green, true);
// Fill the cells with a light green solid color.
table.SetShading(TextureIndex.TextureSolid, Color.LightGreen, Color.Empty);
doc.Save(ArtifactsDir + "WorkingWithTableStylesAndFormatting.ApplyOutlineBorder.docx");

以下範例顯示如何建立具有所有邊框啟用(網格)的表格:

// 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");
Table table = (Table) doc.GetChild(NodeType.Table, 0, true);
// Clear any existing borders from the table.
table.ClearBorders();
// Set a green border around and inside the table.
table.SetBorders(LineStyle.Single, 1.5, Color.Green);
doc.Save(ArtifactsDir + "WorkingWithTableStylesAndFormatting.BuildTableWithBorders.docx");

資料列層級格式化

行層級 格式化可以透過 RowRowFormatRowCollection 等類別來控制。

下面的圖片展示了 Row 在 Microsoft Word 的格式化功能及其在 Aspose.Words 中的對應屬性。

formatting-row-level-aspose-words-net

以下範例顯示如何修改表列格式:

// 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");
Table table = (Table) doc.GetChild(NodeType.Table, 0, true);
// Retrieve the first row in the table.
Row firstRow = table.FirstRow;
firstRow.RowFormat.Borders.LineStyle = LineStyle.None;
firstRow.RowFormat.HeightRule = HeightRule.Auto;
firstRow.RowFormat.AllowBreakAcrossPages = true;

細胞級格式化

細胞級別的格式化是由 CellCellFormatCellCollection 類別控制的。

下面的圖片顯示了 Cell 格式化功能在 Microsoft Word 中的表現及其對應的屬性在 Aspose.Words 。

formatting-cell-level-aspose-words-net

auto-formatting-cell-level-aspose-words-net

以下範例說明如何修改表格欄位的格式:

// 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");
Table table = (Table) doc.GetChild(NodeType.Table, 0, true);
Cell firstCell = table.FirstRow.FirstCell;
firstCell.CellFormat.Width = 30;
firstCell.CellFormat.Orientation = TextOrientation.Downward;
firstCell.CellFormat.Shading.ForegroundPatternColor = Color.LightGreen;

以下範例顯示了如何設定儲存格內容左邊/上邊/右邊/下邊的空間(以點為單位):

// 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);
builder.StartTable();
builder.InsertCell();
// Sets the amount of space (in points) to add to the left/top/right/bottom of the cell's contents.
builder.CellFormat.SetPaddings(30, 50, 30, 50);
builder.Writeln("I'm a wonderful formatted cell.");
builder.EndRow();
builder.EndTable();
doc.Save(ArtifactsDir + "WorkingWithTableStylesAndFormatting.CellPadding.docx");
view raw cell-padding.cs hosted with ❤ by GitHub

設定行高度

設定行高最簡單的方式是使用 DocumentBuilder。 透過適當的 RowFormat 屬性,您可以設定預設高度設定或為表格中的每一列應用不同的高度。

在 Aspose.Words 中,表格列的高度由以下項目控制:

“- 行高屬性 – Height” “- 給定的行高度規則屬性 - HeightRule

同時,您可以為每一列設定不同的高度–這讓您能夠廣泛地控制桌子設定。

以下範例顯示如何建立包含單個細胞的表格並套用格式:

// 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();
RowFormat rowFormat = builder.RowFormat;
rowFormat.Height = 100;
rowFormat.HeightRule = HeightRule.Exactly;
// These formatting properties are set on the table and are applied to all rows in the table.
table.LeftPadding = 30;
table.RightPadding = 30;
table.TopPadding = 30;
table.BottomPadding = 30;
builder.Writeln("I'm a wonderful formatted row.");
builder.EndRow();
builder.EndTable();
doc.Save(ArtifactsDir + "WorkingWithTableStylesAndFormatting.ApplyRowFormatting.docx");

指定表和細胞寬度

在一份 Microsoft Word 文檔中,一個表格提供了一些不同的方式來重新調整其大小和個別的細胞。 這些屬性讓表格的樣貌和行為可做很多控制,讓 Aspose.Words 可支援表格行為,如 Microsoft Word。

知道很重要的是,表元素呈現許多不同的屬性,可以影響整體表寬度以及個別細胞的計算:

  • 桌子最寬的偏好 “- 個別細胞的首選寬度”
  • 在表格上允許自動調整

這篇文章詳細說明了各樣的桌子寬度計算屬性如何運作以及如何完全控制桌子寬度計算。 這是 在這種情況下,如果表格排版沒有預期的出現,那麼知道這事尤其有用。

如何使用首選寬度

表格或個別細胞所需的寬度透過首選寬度屬性來定義,而這個屬性是元素嘗試適應的尺寸。 也就是說,你可以為整個表格或個別細胞設定首選寬度。 在某些情況下,可能無法完全符合這個寬度,但在大多數情況下實際的寬度會接近這個值。

透過 PreferredWidth 類別的方法來設定合適的寬度類型和值。

  • Auto 方法來指定自動或"無首選寬度
  • FromPercent 方法來指定百分比寬度
  • FromPoints 方法來指定點數的寬度

以下圖片顯示了 Microsoft Word 中 首選寬度設定功能 的示範及其在 Aspose.Words 中的對應特性。

formatting-table-properties-aspose-words-net

這些選項如何應用於文件中真實的表格,可從下面的圖片見到。

todo:image_alt_text

指定首選表格或儲存格寬度

在 Aspose.Words 中,透過 Table.PreferredWidth 屬性和 CellFormat.PreferredWidth 屬性來設定表格和細胞的寬度,且可從 PreferredWidthType 列舉中取得選項:

  • Auto,等於未設定首選寬度 “- Percent,其適合於在視窗或容器中可用的空間,並重新計算值,當可用的寬度改變時” “- Points,對應於指定的點數寬度中的元素”

使用 Table.PreferredWidth 屬性會依其容器:頁面、文本列或內嵌表格中的外層表格細胞而調整其首選寬度。

以下範例顯示如何將表格設定為自動符合 50% 的頁面寬度:

// 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);
// Insert a table with a width that takes up half the page width.
Table table = builder.StartTable();
builder.InsertCell();
table.PreferredWidth = PreferredWidth.FromPercent(50);
builder.Writeln("Cell #1");
builder.InsertCell();
builder.Writeln("Cell #2");
builder.InsertCell();
builder.Writeln("Cell #3");
doc.Save(ArtifactsDir + "WorkingWithTables.AutoFitPageWidth.docx");

在一個指定的欄位上使用 CellFormat.PreferredWidth 屬性會調整其首選寬度。

以下範例說明如何設定不同的首選寬度設定:

// 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);
// Insert a table row made up of three cells which have different preferred widths.
// Insert an absolute sized cell.
builder.InsertCell();
builder.CellFormat.PreferredWidth = PreferredWidth.FromPoints(40);
builder.CellFormat.Shading.BackgroundPatternColor = Color.LightYellow;
builder.Writeln("Cell at 40 points width");
// Insert a relative (percent) sized cell.
builder.InsertCell();
builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(20);
builder.CellFormat.Shading.BackgroundPatternColor = Color.LightBlue;
builder.Writeln("Cell at 20% width");
// Insert a auto sized cell.
builder.InsertCell();
builder.CellFormat.PreferredWidth = PreferredWidth.Auto;
builder.CellFormat.Shading.BackgroundPatternColor = Color.LightGreen;
builder.Writeln(
"Cell automatically sized. The size of this cell is calculated from the table preferred width.");
builder.Writeln("In this case the cell will fill up the rest of the available space.");
doc.Save(ArtifactsDir + "WorkingWithTables.PreferredWidthSettings.docx");

找到首選寬度型和值

您可以使用 TypeValue 屬性來找到所需的表格或 komór 的首選寬度詳細資料。

以下程式碼範例示範如何取得表格細胞的首選寬度類型:

// 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");
Table table = (Table) doc.GetChild(NodeType.Table, 0, true);
table.AllowAutoFit = true;
Cell firstCell = table.FirstRow.FirstCell;
PreferredWidthType type = firstCell.CellFormat.PreferredWidth.Type;
double value = firstCell.CellFormat.PreferredWidth.Value;

如何設定自動調整?

AllowAutoFit屬性允許表格中的細胞根據選定的準則來成長和縮小。 例如,您可以使用 自動調整到視窗 選項將表格調整為頁面的寬度,並使用 自動符合內容 選項允許每個單元格根據其內容增長或縮減。

預設 Aspose.Words 會插入新的表格,使用 自動適應窗口。 桌子會根據可用頁面寬度而調整大小。 若要調整表格的大小,您可以呼叫 AutoFit 方法。 此方法接受一個 AutoFitBehavior 列舉值,指定對表格應用何種自動調整。

很重要的一點是,自動調整方法其實是一個縮路,它會將不同的屬性同時套用到表格上。 這些是實際讓表格表現出所觀察的行為的特性。 我們將討論每個自動調整選項的這些性質。

以下範例示範如何設定表格,讓其根據各單元內容而縮小或擴大:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
table.AllowAutoFit = true;

自動調整表格至視窗

當自動調整到一個窗口適用於一個表格時,接下來這些操作实际上是在後台執行的:

  1. Table.AllowAutoFit 屬性已啟用,以自動調整列以適應可用內容,使用 Table.PreferredWidth 值 100%。
  2. CellFormat.PreferredWidth 從所有表格單元格中移除了 3。 列寬度會重新計算給當前的表格內容 – 最終結果是一個佔據所有可用寬度的表格。
  3. 表格中的欄位寬度會隨著使用者編輯文字而自動變更。

以下範例示範了如何自動調整表格以符合頁面寬度:

// 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");
Table table = (Table) doc.GetChild(NodeType.Table, 0, true);
// Autofit the first table to the page width.
table.AutoFit(AutoFitBehavior.AutoFitToWindow);
doc.Save(ArtifactsDir + "WorkingWithTables.AutoFitTableToWindow.docx");

自動調整表格至內容

當資料表自動調整時,實際執行的步驟如下:

  1. Table.AllowAutoFit屬性會自動使每個細胞根據其內容而變大。

2。 首選的表格寬度從 Table.PreferredWidth 中移除了, CellFormat.PreferredWidth 每個表格單元格都移除了

3。 列寬度會重新計算給當前表格內容–最終結果是一個表格,其中列寬度和整個表格寬度會自動調整以最佳地適合內容,因為使用者編輯文本。

以下範例說明如何自動將表格調整為其內容:

// 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");
Table table = (Table) doc.GetChild(NodeType.Table, 0, true);
table.AutoFit(AutoFitBehavior.AutoFitToContents);
doc.Save(ArtifactsDir + "WorkingWithTables.AutoFitTableToContents.docx");

在表中禁用自動調整功能並使用固定欄寬

如果一張桌子已禁用自動調整,而固定列寬被用來使用,以下步驟將被執行:

  1. Table.AllowAutoFit 屬性被停用,因此欄位不會隨著內容增長或縮小。

2。 整張桌子的首選寬度已被移除,所有桌子單元格中Table.PreferredWidthCellFormat.PreferredWidth已被移除。 3。 最終結果是一張桌子,其欄寬由 CellFormat.Width 屬性決定,且當使用者輸入文字或當頁面重新調整大小时,其欄位並非自動重新調整大小

接下來這個程式碼範例示範了如何關閉自動調整寬度,並為指定的表格啟用固定寬度:

// 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");
Table table = (Table) doc.GetChild(NodeType.Table, 0, true);
// Disable autofitting on this table.
table.AutoFit(AutoFitBehavior.FixedColumnWidths);
doc.Save(ArtifactsDir + "WorkingWithTables.AutoFitTableToFixedColumnWidths.docx");

計算細胞寬度時之優先順序

“Aspose.Words 能讓使用者透過多個物件來設定表格或セル的寬度,包括 CellFormat – 它的 Width 屬性大部分是從舊版本遺留下來的,然而它仍可簡化設定セル寬度的動作。

很重要的是要知道,CellFormat.Width 屬性會根據表格中已存在的哪些其他寬度屬性而有所不同。

Aspose.Words 使用以下順序來計算細胞寬度:

訂單 財產 描述
1 AllowAutoFit是決定的 如果 AutoFit 啟用:
- 表格可能會超過首選寬度來容納內容 – 它通常不會縮小至低于首選寬度
- 对 CellFormat.Width 值的任何更改都将被忽略,而細胞將與其內容相適應
2 PreferredWidthType 的值為 PointsPercent CellFormat.Width 被忽略
3 PreferredWidthType 為值的 Auto CellFormat.Width 的值被複製並成為該單元格的首選寬度(以點為單位)。

允許細胞之間留白

你可以取得或設定任意額外的空間,它與 Microsoft Word 中的Cell Spacing選項相似。 這可以用 AllowCellSpacing 屬性來做。

這些選項如何套用到一張真實的文件中的表格,可以看下面的圖。

formatting-spacing-between-cells-aspose-words-net

以下範例示範了如何設定細胞之間的間距:

// 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");
Table table = (Table) doc.GetChild(NodeType.Table, 0, true);
table.AllowCellSpacing = true;
table.CellSpacing = 2;
doc.Save(ArtifactsDir + "WorkingWithTableStylesAndFormatting.AllowCellSpacing.docx");

应用边框和阴影

邊界和陰影可以透過 Table.SetBorderTable.SetBordersTable.SetShading 來應用到整個表格,或僅透過 CellFormat.BordersCellFormat.Shading 來應用到特定的單元格。 此外,透過 RowFormat.Borders 可設定列邊框,但無法以此方式來做陰影。

下面的圖片顯示了 Microsoft Word 中的邊框和陰影設定及其在 Aspose.Words 中的對應屬性。

formatting-border-line-aspose-words-net

formatting-cell-color-aspose-words-net

以下範例展示了如何以不同的邊界與陰影來格式化表格和細胞:

// 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();
// Set the borders for the entire table.
table.SetBorders(LineStyle.Single, 2.0, Color.Black);
// Set the cell shading for this cell.
builder.CellFormat.Shading.BackgroundPatternColor = Color.Red;
builder.Writeln("Cell #1");
builder.InsertCell();
// Specify a different cell shading for the second cell.
builder.CellFormat.Shading.BackgroundPatternColor = Color.Green;
builder.Writeln("Cell #2");
builder.EndRow();
// Clear the cell formatting from previous operations.
builder.CellFormat.ClearFormatting();
builder.InsertCell();
// Create larger borders for the first cell of this row. This will be different
// compared to the borders set for the table.
builder.CellFormat.Borders.Left.LineWidth = 4.0;
builder.CellFormat.Borders.Right.LineWidth = 4.0;
builder.CellFormat.Borders.Top.LineWidth = 4.0;
builder.CellFormat.Borders.Bottom.LineWidth = 4.0;
builder.Writeln("Cell #3");
builder.InsertCell();
builder.CellFormat.ClearFormatting();
builder.Writeln("Cell #4");
doc.Save(ArtifactsDir + "WorkingWithTableStylesAndFormatting.FormatTableAndCellWithDifferentBorders.docx");