C# Aspose RTF含无分隔符亚洲语言时列宽异常求助
解决Aspose.Words创建等宽两列RTF表格(支持亚洲无空格文本自动换行)
核心要解决两个问题:固定列宽比例,以及让无空格的亚洲文本自动断字换行。以下是具体实现步骤和代码:
关键设置要点
- 强制表格使用固定列宽模式,避免内容撑开列
- 开启亚洲文本自动断字功能,让无空格长文本能自动换行
- 确保表格和列的宽度设置正确,绑定到页面宽度
完整C#代码示例
using Aspose.Words; using Aspose.Words.Tables; // 创建文档和文档构建器 Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // 设置页面边距(可选,确保表格有足够空间) PageSetup pageSetup = builder.PageSetup; pageSetup.LeftMargin = ConvertUtil.InchToPoint(1); pageSetup.RightMargin = ConvertUtil.InchToPoint(1); // 创建表格 Table table = builder.StartTable(); // 1. 设置表格整体宽度为100%,强制固定列宽模式 table.PreferredWidth = PreferredWidth.FromPercent(100); table.AutoFit(AutoFitBehavior.FixedColumnWidths); // 2. 添加第一行单元格,设置每列宽度为50% // 第一列 builder.InsertCell(); builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(50); builder.CellFormat.WrapText = true; // 开启段落自动断字(针对亚洲文本) builder.ParagraphFormat.Hyphenation = true; builder.ParagraphFormat.AsianTypography.AutoHyphenation = true; builder.Writeln("第一列示例文本,这里可以放任意长度的内容,包括中文等亚洲语言"); // 第二列 builder.InsertCell(); builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(50); builder.CellFormat.WrapText = true; builder.ParagraphFormat.Hyphenation = true; builder.ParagraphFormat.AsianTypography.AutoHyphenation = true; // 无空格的长亚洲文本测试 builder.Writeln("这是一段没有空格的超长中文文本用来测试自动换行功能这是一段没有空格的超长中文文本用来测试自动换行功能这是一段没有空格的超长中文文本用来测试自动换行功能"); builder.EndRow(); builder.EndTable(); // 保存为RTF文件 doc.Save("EqualColumnsTable.rtf");
额外说明
AutoFitBehavior.FixedColumnWidths是核心:它会锁定列宽,无论内容多长都不会撑开列,而是让内容在列内换行AsianTypography.AutoHyphenation专门针对亚洲语言(中文、日文等)开启自动断字,即使没有空格也能按字符拆分换行- 确保每个单元格的
WrapText设为true,同时段落的断字设置要生效,避免内容溢出
内容的提问来源于stack exchange,提问作者János Szabó
相关产品推荐
相关产品推荐

