3 issues: setting border color to 'automatic'- cell merging not working & font not being set correctly

Hello,

As I’m using an approved template to build the document I need to adhere to the formats consistent throughout; I’ve got a couple of issues with a table I’m dynamically inserting into a Word document:

  1. I need to set the table border color to ‘Automatic’. Not sure how to do this as it is not one of the available values for System.Drawing.Color

  2. I’m trying to merge 2 cells at the table header but it’s not working

  3. I’ve specified the font for the text being written inside the table but it’s coming out in a different font style (it’s odd because when I place the cursor on the text of the generated document MS Word shows the correct font style in the font box but I can clearly see it’s Times New Roman.

Here is the code I’m using:

Dim ProdTable As Table = OFbuilder.StartTable()
OFbuilder.Font.Name = "Calibri (Body)"
OFbuilder.Font.Bold = True
OFbuilder.Font.Size = 11
OFbuilder.InsertCell()
OFbuilder.Write("Current Monthly Recurring Subscriptions")
OFbuilder.InsertCell()
OFbuilder.CellFormat.HorizontalMerge = CellMerge.First
OFbuilder.EndRow()
OFbuilder.InsertCell()
OFbuilder.Write("Product")
OFbuilder.InsertCell()
OFbuilder.Write("Service Type")
OFbuilder.EndRow()
OFbuilder.Font.Bold = False
Do While RowCount > 0
OFbuilder.InsertCell()
OFbuilder.Write(dtProducts.Rows(RowCount - 1).Item(1))
OFbuilder.InsertCell()
OFbuilder.Write(dtProducts.Rows(RowCount - 1).Item(0))
OFbuilder.EndRow()
RowCount -= 1
Loop
ProdTable.SetBorders(LineStyle.Outset, 0.75, System.Drawing.Color.DimGray)
ProdTable.LeftIndent = 5
OFbuilder.EndTable()

Hi Stephen,

Thanks for your inquiry.

stomas:

1. I need to set the table border color to ‘Automatic’.

Please use the Table.ClearBorders method to achieve this requirement.

stomas:

2. I’m trying to merge 2 cells at the table header but it’s not working

Please read the following documentation link for your kind reference.
https://docs.aspose.com/words/net/working-with-columns-and-rows/

stomas:

3. I’ve specified the font for the text being written inside the table but it’s coming out in a different font style (it’s odd because when I place the cursor on the text of the generated document MS Word shows the correct font style in the font box but I can clearly see it’s Times New Roman.

In this case, I suggest you please call ParagraphFormat.ClearFormatting and Font.ClearFormatting methods before inserting the text with new font properties.

The ParagraphFormat.ClearFormatting method resets to default paragraph formatting. Default paragraph formatting is Normal style, left aligned, no indentation, no spacing, no borders and no shading.

The Font.ClearFormatting method to reset to default font formatting. This method removes all font formatting specified explicitly on the object from which Font was obtained so the font formatting will be inherited from the appropriate parent.

If you still face problem, please share following detail for investigation purposes.

  • Please attach your input Word document.
  • Please

create a standalone/runnable simple application (for example a Console
Application Project
) that demonstrates the code (Aspose.Words code) you used to generate
your output document

  • Please attach the output Word file that shows the undesired behavior.
  • Please
    attach your target Word document showing the desired behavior. You can
    use Microsoft Word to create your target Word document. I will
    investigate as to how you are expecting your final document be generated
    like.

Hi Tahir,

Thanks for the response. None of your suggestions resolved the issues.

For 1, inserting Table.ClearBorders() before I’m calling ProdTable.SetBorders(LineStyle.Outset, 0.75, System.Drawing.Color.DimGray) has not effect and calling it after that statement just erases all the borders. Did you mean something else? The Aspose documentation doesn’t indicate that it can be used in any other way. Again, I don’t want to remove the borders, just set the border color to ‘Automatic’.

For 2, I had already referred to the documentation page you linked to and it did not help. When I use the following code:

OFbuilder.InsertCell()
OFbuilder.CellFormat.HorizontalMerge = CellMerge.First
OFbuilder.Write("Current Monthly Recurring Subscriptions")
OFbuilder.InsertCell()
OFbuilder.CellFormat.HorizontalMerge = CellMerge.Previous
OFbuilder.EndRow()
OFbuilder.InsertCell()
OFbuilder.Write("Product")
OFbuilder.InsertCell()
OFbuilder.Write("Service Type")
OFbuilder.EndRow()
OFbuilder.EndTable()

There is only one cell in the header, the second header cell I tried to create disappears. While there are 2 cells underneath.

For 3 the clear formatting method doesn’t work. When I output the created document to .pdf then the fonts are as they should be, it’s only when I output to .docx that the problem pops up.

Hi Stephen,

Thanks for your inquiry. It would be great if you please share input and expected output documents for shared three scenarios. Please manually create your expected Word documents using Microsoft Word and attach it here for our reference. We will investigate as to how you want your final Word output be generated like. We will then provide you more information on this along with code.

Edit:

stomas:

For 1, inserting Table.ClearBorders() before I’m calling ProdTable.SetBorders(LineStyle.Outset, 0.75, System.Drawing.Color.DimGray) has not effect and calling it after that statement just erases all the borders. Did you mean something else? The Aspose documentation doesn’t indicate that it can be used in any other way. Again, I don’t want to remove the borders, just set the border color to ‘Automatic’.

Please use the following code example to set the border color as ‘Automatic’. I have attached the input/output documents with this post for your kind reference.

Document doc = new Document(MyDir + "SetBorders.docx");
Table table = (Table)doc.GetChild(NodeType.Table, 0, true);
// Clear any existing borders from the table.
table.ClearBorders();
table.SetBorders(LineStyle.Outset, 0.75, System.Drawing.Color.DimGray);
foreach (Cell cell in table.GetChildNodes(NodeType.Cell, true))
{
    cell.CellFormat.Borders.Top.Color = Color.Empty;
    cell.CellFormat.Borders.Bottom.Color = Color.Empty;
    cell.CellFormat.Borders.Left.Color = Color.Empty;
    cell.CellFormat.Borders.Right.Color = Color.Empty;
}
doc.Save(MyDir + "Out.docx");

stomas:

*For 2, I had already referred to the documentation page you linked to and it did not help. When I use the following code:

There is only one cell in the header, the second header cell I tried to create disappears. While there are 2 cells underneath.*

Please set the CellFormat.HorizontalMerge as CellMerge.None to get the required output. See the highlighted code below.

builder.InsertCell();
builder.CellFormat.HorizontalMerge = CellMerge.First;
builder.Write("Current Monthly Recurring Subscriptions");
builder.InsertCell();
builder.CellFormat.HorizontalMerge = CellMerge.Previous;
builder.EndRow();
builder.InsertCell();
builder.CellFormat.HorizontalMerge = CellMerge.None;
builder.Write("Product");
builder.InsertCell();
builder.Write("Service Type");
builder.EndRow();
builder.EndTable();

stomas:

For 3 the clear formatting method doesn’t work. When I output the created document to .pdf then the fonts are as they should be, it’s only when I output to .docx that the problem pops up.

Could you please share the input/output documents along with code here for testing? I will investigate the issue on my side and provide you more information.