使用aspose.word 创建文档,设置
table.AllowAutoFit = true ,
table.PreferredWidth = PreferredWidth.FormPoints(467.8)
后, 使用 LayoutCollector 不能准确读到 word的高度。
ps: 我想让表格固定宽度,列自适应,但在单元格内容过多的情况下,表格宽度不能生效。 生成word后表格有被撑开, layoutConllector 不能读到准确的pageIndex
Document doc = new Document();
// 添加表格
Table table = new Table(doc);
Row row = new Row(doc);
table.Rows.Add(row);
Paragraph cell1P = new Paragraph(doc);
Cell cell1 = new Cell(doc);
Run cellRun = new Run(doc);
cellRun.Text = "wei";
cell1P.AppendChild(cellRun);
cell1.AppendChild(cell1P);
row.AppendChild(cell1);
Paragraph cell2P = new Paragraph(doc);
Cell cell2 = new Cell(doc);
Run cell2Run = new Run(doc);
StringBuilder sbuilder = new StringBuilder();
for (int i=0;i<3000; i++)
{
sbuilder.Append("早安");
}
cell2Run.Text = sbuilder.ToString();
cell2P.AppendChild(cell2Run);
cell2.AppendChild(cell2P);
row.AppendChild(cell2);
table.AllowAutoFit = true;
table.PreferredWidth = PreferredWidth.FromPoints(467.8);
doc.FirstSection.Body.AppendChild(table);
Aspose.Words.Layout.LayoutCollector layout = new Aspose.Words.Layout.LayoutCollector(doc);
layout.Clear();
doc.UpdateFields();
doc.UpdatePageLayout();
Console.WriteLine(layout.GetEndPageIndex(table));
layout.Clear();
doc.UpdateTableLayout();
doc.UpdateFields();
doc.UpdatePageLayout();
//layout = new Aspose.Words.Layout.LayoutCollector(doc);
Console.WriteLine(layout.GetEndPageIndex(table));
@james_yeh_honlitech_com,
请ZIP并上传您期望的Word文档(DOCX文件),此处显示所需的输出以进行测试。 您可以使用MS Word创建所需的文档。 然后,我们将在最后调查此方案,并通过使用Aspose.Words为您提供实现该方案的代码。
您好 谢谢您的关注。
这里附上示例代码, 代码中生成文档保存后,查看文档可以看到:表格对象结束页码在 第5页
但我们使用 LayoutCollector.GetEndPageIndex(table) 值为2. 与我预期的不匹配
补充说明下:
在文档保存为pdf格式, 显示结果和 layoutCollector 一致,表格宽度严重变形
在文档保存为word格式,显示结果格式完整,但和 layoutCollector 结果不一致
Document doc = new Document();
doc.CompatibilityOptions.GrowAutofit = false;
// 添加表格
Table table = new Table(doc);
Row row = new Row(doc);
row.RowFormat.HeightRule = HeightRule.AtLeast;
table.Rows.Add(row);
Paragraph cell1P = new Paragraph(doc);
Cell cell1 = new Cell(doc);
Run cellRun = new Run(doc);
cellRun.Text = "wei";
cell1P.AppendChild(cellRun);
cell1.AppendChild(cell1P);
row.AppendChild(cell1);
Paragraph cell2P = new Paragraph(doc);
Cell cell2 = new Cell(doc);
cell2.CellFormat.WrapText = true;
Run cell2Run = new Run(doc);
StringBuilder sbuilder = new StringBuilder();
for (int i=0;i<3000; i++)
{
sbuilder.Append("早安");
}
cell2Run.Text = sbuilder.ToString();
cell2P.AppendChild(cell2Run);
cell2.AppendChild(cell2P);
row.AppendChild(cell2);
table.AllowAutoFit = true;
table.PreferredWidth = PreferredWidth.FromPoints(467.8);
doc.FirstSection.Body.AppendChild(table);
Paragraph parag = new Paragraph(doc);
Run paragRun = new Run(doc);
paragRun.Text = sbuilder.ToString();
parag.AppendChild(paragRun);
doc.FirstSection.Body.AppendChild(parag);
Aspose.Words.Layout.LayoutCollector layout = new Aspose.Words.Layout.LayoutCollector(doc);
layout.Clear();
doc.UpdateFields();
doc.UpdateTableLayout();
doc.UpdateWordCount(true);
doc.UpdatePageLayout();
Console.WriteLine(layout.GetStartPageIndex(cell2P));
Console.WriteLine(layout.GetEndPageIndex(cell2P));
Console.WriteLine(layout.GetStartPageIndex(parag));
Console.WriteLine(layout.GetEndPageIndex(parag));
string dirPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tmp.docx");
doc.Save(dirPath);
@james_yeh_honlitech_com,
在使用最新版本的Aspose.Words for .NET(即20.1)时,我们最终使用您的代码生成了以下DOCX和PDF文件:
我们最终产生的控制台输出为:
1
4
5
8
因此,请升级到.NET的最新版本的Aspose.Words。 希望这可以帮助。
抱歉 我这代码看还是有问题, 能否帮忙看下 。 版本也是20.1的。 但生成出来的文档,确实有问题。
FileManagerCoreDemo.zip (12.1 KB)
还希望能帮忙看下,谢谢。
非常抱歉,打扰了,这里有新的问题。
在示例中 SaveTableDemo 方法中, 生成的pdf和word 会因为属性 AllowAutoFit 产生变化, 都不能保持一致。 希望能帮忙看下。
TestAsposeWords.zip (6.5 KB)
非常感谢您的关注,期待您的答复
@james_yeh_honlitech_com,
我们测试了这种情况,并最终设法重现了同样的问题。 为了更正,我们已将此问题记录在问题跟踪系统中。 此问题的ID为WORDSNET-19847。 我们将进一步调查该问题的详细信息,并让您及时了解更正的状态。 对于给您带来的不便,我们深表歉意。