We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Aspose. Word Table autofit not working in Win service mode


Hi Team,

This code is not working in win service but it works in Console.

var tables = ProcessingDocument.GetChildNodes(NodeType.Table, true, true);
foreach (Node node in tables)
{
Aspose.Words.Tables.Table table = (Aspose.Words.Tables.Table)node;
table.AutoFit(Aspose.Words.Tables.AutoFitBehavior.AutoFitToWindow);
}

I am trying to adjust the word table using this code at end.

See attached o/p. as well this will be posted in priority forum as well.

Regards,
Nakul

Hi Nakul,


Thanks for your inquiry. I managed to reproduce this issue on my side. But, you should not that MS WORD does the same when auto fitting the table. The problem occurs because the table inside your document is probably too wide. I would suggest you set the page orientation to Landscape and then manually calculate the width for each column. Please use the following code as a work around:

Document doc = new Document(@“c:\temp\test.docx”);

PageSetup ps = doc.FirstSection.PageSetup;
// Set Orientation to Landscape
ps.Orientation = Orientation.Landscape;
ps.LeftMargin = 0.5 * 72; // 1-inch equals 72 points
ps.RightMargin = 0.5 * 72;
// Calculate width of page.
double pageWidth = ps.PageWidth - ps.LeftMargin - ps.RightMargin;
//Lets suppose that each cell should have the same width.
const int culumnsCount = 18;
double colWidth = pageWidth / culumnsCount;

Table tbl = doc.FirstSection.Body.Tables[0];
tbl.LeftIndent = 0;

foreach (Row r in tbl.Rows)
{
for (int i = 0; i < culumnsCount; i++)
{
if (r.Cells[i] != null)
r.Cells[i].CellFormat.PreferredWidth = PreferredWidth.FromPoints(colWidth);
}
}

doc.Save(@“c:\temp\out.docx”);

I hope, this will help.

Moreover, I have attached the output document i.e. generated on my side here for your reference.

Best Regards,