Paragram alignment does no work for last column in a looping

Hi there,

I found something weird:
I try to align three columns, but the last column cannot be align, it is always left. Other columns- first and second works properly:

for (int i = 0; i < titles.Length; i++)
{
    bool rancamaya2 = true;

    var mycell = builder.InsertCell();

    color = Color.FromArgb(217, 217, 217);
    mycell.CellFormat.Borders.Top.Color = color;
    mycell.CellFormat.Borders.Bottom.Color = color;
    mycell.CellFormat.Borders.Left.Color = color;
    mycell.CellFormat.Borders.Right.Color = color;

    if (rancamaya2)
    {
        if (i == 0)
        {
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
            builder.Write(titles[i] + "-08");
        }
        else if (i == 1)
        {
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
            builder.Write(titles[i] + "-09");
        }
        else
        {
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Right; // No Effect, no work
            builder.Write(titles[i] + "-10");
        }
    }
}

and after this loop there is a statement:

builder.ParagraphFormat.ClearFormatting();

if i take out it, the column three works correctly, but the TOC changes to include bullets.

I tried from yesterday with many combinations, with no luck.

How to workaround?
regards dan thanks

@ibox Unfortunately, I cannot reproduce the problem. I have used the following simple code for testing:

DataTable dt = new DataTable();
dt.Columns.Add("First");
dt.Columns.Add("Second");
dt.Columns.Add("Third");
for (int i = 0; i < 10; i++)
    dt.Rows.Add($"First_{i}", $"Second_{i}", $"Third_{i}");

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.StartTable();
foreach (DataColumn col in dt.Columns)
{
    builder.InsertCell();
    builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
    builder.Font.Bold = true;
    builder.Write(col.ColumnName);
}
builder.EndRow();
builder.Font.Bold = false;
foreach (DataRow row in dt.Rows)
{
    foreach (DataColumn col in dt.Columns)
    {
        builder.InsertCell();
        switch (dt.Columns.IndexOf(col))
        {
            case 0: 
                builder.ParagraphFormat.Alignment = ParagraphAlignment.Left; 
                break;
            case 1:
                builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
                break;
            case 2:
                builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
                break;
        }
        builder.Write(row[col].ToString());
    }
    builder.EndRow();
}
builder.EndTable();

doc.Save(@"C:\Temp\out.docx");

out.docx (7.4 KB)

If it is required to clear paragraph formatting for the cell, do this just after inserting the new cell. If you call builder.ParagraphFormat.ClearFormatting(); before starting a new cell, the current document builder’s paragraph format will be cleared.

thank u, Sir.

I attach the whole program.

regards
no work paragraph alignment.zip (11.6 KB)

The statement;
builder.ParagraphFormat.ClearFormatting();
after the loop.

regards

@ibox You call builder.ParagraphFormat.ClearFormatting(); after ending the row, but before starting a new cell. So DocumentBuilder’s cursor is still in the last cell of the finished row. That is why paragraph format of the last cell in the row is cleared. This is an expected behavior.

thank u, Sir.

But i do not understand. I call the insert cell before the alignment:

var mycell = builder.InsertCell(); // HERE....

color = Color.FromArgb(217, 217, 217);
mycell.CellFormat.Borders.Top.Color = color;
mycell.CellFormat.Borders.Bottom.Color = color;
mycell.CellFormat.Borders.Left.Color = color;
mycell.CellFormat.Borders.Right.Color = color;



if (rancamaya2)
{
    if (i == 0) {
        builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
        builder.Write(titles[i] + "-08");
    }
    else if (i == 1)
    {
        builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
        builder.Write(titles[i] + "-09");
    }
    else
    {
        builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
        builder.Write(titles[i] + "-10");

    }

regards

@ibox Yes, but you clear formatting after ending the row:

else
{
    builder.EndRow();
}

//builder.CellFormat.Width = 50;
bool rancamaya = true;
if (rancamaya)
{
    //builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
    builder.ParagraphFormat.ClearFormatting();
}
else {
    builder.ParagraphFormat.ClearFormatting();
}

Clear formatting after inserting a cell, like this:

var mycell = builder.InsertCell(); // HERE....
builder.ParagraphFormat.ClearFormatting();

Thank u, Sir

It works by putting that ClearFormatting statement after every Insert Statement.
if i miss the Clear Formatting,
why the TOC add many entries - bullets, etc?

regards

@ibox Unfortunately, it is difficult to say using screenshot. Most likely paragraphs are formatted with a style, which is used to build the TOC.