Aspose.Words for .NET使用表格教程之在表格中设置自动调整设置

Aspose.Words For .Net是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

接下来我们将进入“使用格式”的介绍,其中包括应用格式、介绍和创建表、添加和拆分表以及使用列和行。本文将为大家讲解如何在表格中运用自动调整设置。

>>Aspose.Words for .NET更新至最新版,欢迎下载体验 

 欢迎下载|体验更多Aspose文档管理产品  或 加入Aspose技术交流群(761297826)


使用可视代理(例如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.");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值