Aspose.Words for .NET使用表格教程之在表格中設置自動調整設置
推薦閱讀:【Aspose.Words for .NET使用表格教程之應用格式——將格式應用于表,行和單元格】
Aspose.Words For .Net是一種高級Word文檔處理API,用于執行各種文檔管理和操作任務。API支持生成,修改,轉換,呈現和打印文檔,而無需在跨平臺應用程序中直接使用Microsoft Word。此外,API支持所有流行的Word處理文件格式,并允許將Word文檔導出或轉換為固定布局文件格式和最常用的圖像/多媒體格式。
接下來我們將進入“使用格式”的介紹,其中包括應用格式、介紹和創建表、添加和拆分表以及使用列和行。本文將為大家講解如何在表格中運用自動調整設置。
>>Aspose.Words for .NET更新至最新版v19.9,歡迎下載體驗
使用可視代理(例如Microsoft Word)創建表時,通常會發現使用AutoFit選項之一自動將表調整為所需的寬度。例如,可以使用“自動適應窗口”選項使表格適合頁面的寬度,使用“自動適應目錄”選項允許每個單元格增加或縮小以容納其內容。
默認情況下,Aspose.Words使用“ 自動調整到窗口”插入一個新表。該表格將調整為頁面上的可用寬度。若要更改此類表或現有表的大小調整行為,可以調用Table.AutoFit方法。
自動將表格擬合到窗口
下面的示例自動調整表格以適合頁面寬度。
//文檔目錄的路徑。 string dataDir = RunExamples.GetDataDir_WorkingWithTables(); string fileName = "TestFile.doc"; //打開文檔 Document doc = new Document(dataDir + fileName); Table table = (Table)doc.GetChild(NodeType.Table, 0, true); //將第一個表格自動調整為頁面寬度。 table.AutoFit(AutoFitBehavior.AutoFitToWindow); dataDir = dataDir + RunExamples.GetOutputFilePath(fileName); //將文檔保存到磁盤。 doc.Save(dataDir); Debug.Assert(doc.FirstSection.Body.Tables[0].PreferredWidth.Type == PreferredWidthType.Percent, "PreferredWidth type is not percent"); Debug.Assert(doc.FirstSection.Body.Tables[0].PreferredWidth.Value == 100, "PreferredWidth value is different than 100");
當將對窗口的自動調整應用于表時,實際上是在后臺執行以下操作:
- 啟用Table.AllowAutoFit屬性可自動將列的大小調整為可用內容。
- Table.PreferredWidth值為100%。
- 從表中的所有單元格中刪除了CellFormat.PreferredWidth。
- 將針對表的當前內容重新計算列寬。
自動將表格擬合到目錄
下面的示例將文檔中的表格自動調整為其內容。
//文檔目錄的路徑。 string dataDir = RunExamples.GetDataDir_WorkingWithTables(); string fileName = "TestFile.doc"; Document doc = new Document(dataDir + fileName); Table table = (Table)doc.GetChild(NodeType.Table, 0, true); //自動使表格適合單元格內容 table.AutoFit(AutoFitBehavior.AutoFitToContents); dataDir = dataDir + RunExamples.GetOutputFilePath(fileName); // 將文檔保存到磁盤。 doc.Save(dataDir); Debug.Assert(doc.FirstSection.Body.Tables[0].PreferredWidth.Type == PreferredWidthType.Auto, "PreferredWidth type is not auto"); Debug.Assert(doc.FirstSection.Body.Tables[0].FirstRow.FirstCell.CellFormat.PreferredWidth.Type == PreferredWidthType.Auto, "PrefferedWidth on cell is not auto"); Debug.Assert(doc.FirstSection.Body.Tables[0].FirstRow.FirstCell.CellFormat.PreferredWidth.Value == 0, "PreferredWidth value is not 0");
在表上禁用自動調整并使用固定的列寬
下面的示例禁用自動擬合并為指定的表格啟用固定寬度。
//文檔目錄的路徑。 string dataDir = RunExamples.GetDataDir_WorkingWithTables(); string fileName = "TestFile.doc"; Document doc = new Document(dataDir + fileName); Table table = (Table)doc.GetChild(NodeType.Table, 0, true); //在此表上禁用自動擬合。 table.AutoFit(AutoFitBehavior.FixedColumnWidths); dataDir = dataDir + RunExamples.GetOutputFilePath(fileName); //將文檔保存到磁盤。 doc.Save(dataDir); //結束 Debug.Assert(doc.FirstSection.Body.Tables[0].PreferredWidth.Type == PreferredWidthType.Auto, "PreferredWidth type is not auto"); Debug.Assert(doc.FirstSection.Body.Tables[0].PreferredWidth.Value == 0, "PreferredWidth value is not 0"); Debug.Assert(doc.FirstSection.Body.Tables[0].FirstRow.FirstCell.CellFormat.Width == 69.2, "Cell width is not correct.");
*想要購買Aspose正版授權的朋友可以哦~
ASPOSE技術交流QQ群已開通,各類資源及時分享,歡迎交流討論!(掃描下方二維碼加入群聊)