That is because you maintained KeepSourceFormatting but with that the width is not common for all pages. We need all pages with A4 size as standard width and margin hence we have maintained the code like that.
Additionally I tried maintaining table width using below code.
foreach (Table tbl in srcDoc.GetChildNodes(NodeType.Table, true))
{
//AutoFit Tables To Window
tbl.AllowAutoFit = true;
tbl.AutoFit(AutoFitBehavior.AutoFitToWindow);
}
Document first_Page = srcDoc.ExtractPages(0, 1);
foreach (Section section in srcDoc.Sections)
{
PageSetup pageSetup = section.PageSetup;
pageSetup.LeftMargin = first_Page.FirstSection.PageSetup.LeftMargin;
pageSetup.RightMargin = first_Page.FirstSection.PageSetup.RightMargin;
pageSetup.TopMargin = first_Page.FirstSection.PageSetup.TopMargin;
pageSetup.BottomMargin = first_Page.FirstSection.PageSetup.BottomMargin;
section.PageSetup.PaperSize = Aspose.Words.PaperSize.A4;
}
Table firsttbl = (Table)srcDoc.GetChildNodes(NodeType.Table, true)[0];
double standardtableWidth = firsttbl.PreferredWidth.Value;
LayoutCollector collector = new LayoutCollector(srcDoc);
LayoutEnumerator enumerator = new LayoutEnumerator(srcDoc);
foreach (Table tbl in srcDoc.GetChildNodes(NodeType.Table, true))
{
double percWidth = 0;
if (tbl.PreferredWidth.Type == PreferredWidthType.Percent)
{
Row row = tbl.FirstRow;
Cell firstCell = row.FirstCell;
Cell lastCell = row.LastCell;
enumerator.Current = collector.GetEntity(firstCell.FirstParagraph);
enumerator.MoveParent(LayoutEntityType.Cell);
double firststart = enumerator.Rectangle.X;
double width = enumerator.Rectangle.Width;
enumerator.Current = collector.GetEntity(lastCell.LastParagraph);
enumerator.MoveParent(LayoutEntityType.Cell);
double laststart = enumerator.Rectangle.X;
double lastwidth = enumerator.Rectangle.Width;
percWidth = (laststart - firststart) + lastwidth;
}
//AutoFit Tables To Window
if (tbl.PreferredWidth.Type == PreferredWidthType.Points && tbl.PreferredWidth.Value > standardtableWidth)
tbl.PreferredWidth = PreferredWidth.FromPoints(standardtableWidth);
if (tbl.PreferredWidth.Type == PreferredWidthType.Percent && percWidth > standardtableWidth)
{
tbl.PreferredWidth = PreferredWidth.FromPoints(standardtableWidth);
tbl.TextWrapping = TextWrapping.Around;
tbl.AutoFit(AutoFitBehavior.AutoFitToWindow);
}
//if (tbl.PreferredWidth.Type == PreferredWidthType.Percent && percWidth > standardtableWidth)
//{
// tbl.AllowAutoFit = true;
// tbl.TextWrapping = TextWrapping.Around;
// tbl.AutoFit(AutoFitBehavior.AutoFitToContents);
//}
}
srcDoc.UpdatePageLayout();
The output document is attached here.FinalTableOutput_1.docx (27.9 KB)
As you can see, the table has text but it is getting cut.
Thanks
Hardik