Different column width in table header

I have a doc template with a table. The table has 3 columns and 4 rows, including the header. I open the doc using aspose and then i dynamically add n columns after the second column. In order to keep the intial column size in the header row, before i add the columns, i clone the header row and then i remove it. After i add the extra columns, i insert the header in the index 0, but the columns are being adjusted to the other table’s rows. How can i keep the initial column width, independtly from the columns that are being added.

I set the table’s column width using percentage with the following code:

row.Cells[0].CellFormat.PreferredWidth = PreferredWidth.FromPercent(25);
row.Cells[0].CellFormat.PreferredWidth = PreferredWidth.FromPercent(25);

etc.

whereas the sum of percentage is 100, the last column and the previous, whereas they have the same percentage, they don’t have the same width. Also in the produced doc, if i open the table properties, the preferred width is not set to percentage. Am i doing something wrong?

Hi,

Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document
  • Aspose.Words generated output DOCX file showing the undesired behavior
  • Please attach your expected document here for our reference. We will investigate the structure of your expected document as to how you want your final output be generated like. You can create expected document using Microsoft Word.
  • Please create a standalone console application (source code without compilation errors) that helps us reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip them and Click ‘Reply’ button that will bring you to the ‘reply page’ and there at the bottom you can include any attachments with that post by clicking the ‘Add/Update’ button.

Best regards,

I had a problem to upload the zip file either using the link url or upload the file. I link to the file using this link. In the zip file i linked to, i have all the requirements in order to investigate the issue. Inside the project i have the template doc, the result doc and the desired result doc.

Thank you in advance!

I link to my zip file including all the desired documents and files needed to investigate my issue. The problem i have is that i want to set the percentage of the table’s columns but i face some issues.

  1. In the resulted doc when i edit a column’s width properties, via table properties, the size is not set as a percentage, but it remains in inches.

  2. Probably because of the issue 1, the column width is not set properly. As you can see in the result doc, the last 2 columns whereas they have the same percentage, they don’t have the same width. You can see more clear what i mean if you see the result doc and the desired result doc.

Please inform me if the problem i face is not clear, or you need further information.

Thank you in advance!

Hi,

Thanks for the additional information. We are working over your query and will get back to you soon.

Best regards,

Hi,

Thanks for being patient. You can post-process your ‘result.docx’ document and use any of the following methods to get the desired result:

Document doc = new Document(MyDir + @"result.docx");
Bookmark bm = doc.Range.Bookmarks["baseTable"];
Table table = (Table)bm.BookmarkStart.GetAncestor(NodeType.Table);
if (table != null)
{
    table.FirstRow.Cells[0].CellFormat.Width = 72 * 1.7;
    table.FirstRow.Cells[1].CellFormat.Width = 72 * 4;
    table.FirstRow.Cells[2].CellFormat.Width = 72 * 1.56;
    table.AutoFit(AutoFitBehavior.FixedColumnWidths);
}
doc.Save(MyDir + @"17.1.0.docx");
Document doc = new Document(MyDir + @"result.docx");
Bookmark bm = doc.Range.Bookmarks["baseTable"];
Table table = (Table)bm.BookmarkStart.GetAncestor(NodeType.Table);
if (table != null)
{
    Row row = table.FirstRow;
    Row clone = (Row)table.FirstRow.Clone(true);
    row.RemoveAllChildren();
    for (int i = 0; i < 6; i++)
        row.Cells.Add(new Cell(doc));
    row.Cells[1].CellFormat.HorizontalMerge = CellMerge.First;
    row.Cells[2].CellFormat.HorizontalMerge = CellMerge.Previous;
    row.Cells[3].CellFormat.HorizontalMerge = CellMerge.Previous;
    row.Cells[4].CellFormat.HorizontalMerge = CellMerge.First;
    row.Cells[5].CellFormat.HorizontalMerge = CellMerge.Previous;
    row.Cells[0].RemoveAllChildren();
    row.Cells[0].AppendChild(clone.Cells[0].FirstParagraph);
    row.Cells[1].RemoveAllChildren();
    row.Cells[1].AppendChild(clone.Cells[1].FirstParagraph);
    row.Cells[4].RemoveAllChildren();
    row.Cells[4].AppendChild(clone.Cells[2].FirstParagraph);
    foreach (Cell cell in row)
    {
        cell.CellFormat.Shading.BackgroundPatternColor = clone.FirstCell.CellFormat.Shading.BackgroundPatternColor;
        cell.CellFormat.VerticalAlignment = clone.FirstCell.CellFormat.VerticalAlignment;
    }
}
doc.Save(MyDir + @"17.1.0.docx");

Hope, this helps.

Best regards,