Hello,
I have below code and using which I am trying to add 3 headers but second and third header alignment is having issue.
FYI - I tried by adjusting width but still facing same issue.
Please have a look on attachment.
Code -
Public Function Replacing(args As ReplacingArgs) As ReplaceAction Implements IReplacingCallback.Replacing
_Replacement = (From r In _Replacement Order By _datautil.GetInt32(r.Type) Ascending).ToList()
Dim builder As New DocumentBuilder(args.MatchNode.Document)
builder.MoveTo(args.MatchNode)
'Add Certificate Header Section Lable
builder.StartTable()
Dim PrvCCDocType As New CompletionCertificateDocType
PrvCCDocType = CompletionCertificateDocType.Header
For Each doc In _Replacement
If doc.Type = CompletionCertificateDocType.Header OrElse PrvCCDocType <> doc.Type Then
If PrvCCDocType <> doc.Type Then
PrvCCDocType = doc.Type
builder.EndTable()
End If
builder.StartTable()
builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.LightGray
builder.Font.Bold = True
builder.Font.Size = 11
builder.RowFormat.Height = 15
builder.CellFormat.TopPadding = 2
builder.InsertCell()
builder.CellFormat.Width = 300
builder.Write(doc.LableCol1)
builder.ParagraphFormat.Alignment = ParagraphAlignment.Left
builder.InsertCell()
builder.CellFormat.Width = 300
builder.Write(doc.LableCol2)
builder.ParagraphFormat.Alignment = ParagraphAlignment.Left
builder.InsertCell()
builder.CellFormat.Width = 300
builder.Write(doc.LableCol3)
builder.ParagraphFormat.Alignment = ParagraphAlignment.Left
builder.EndRow()
builder.CellFormat.ClearFormatting()
builder.ParagraphFormat.ClearFormatting()
End If
builder.InsertCell()
builder.InsertDocument(doc.Document, ImportFormatMode.KeepSourceFormatting)
builder.EndRow()
builder.EndTable()
builder.StartTable()
Next
builder.EndTable()
Return ReplaceAction.Replace
End Function
@shivrajp
Issue Summary
You are building a table that contains three header cells (LabelCol1‑3).
The first header aligns correctly, but the second and third headers are not left‑aligned as expected.
Why the alignment is off
ParagraphFormat.Alignment
is applied to the next paragraph that is created, not to the paragraph that already exists in the cell you have just written to.
In your current flow you:
builder.InsertCell()
builder.Write(doc.LableCol1)
builder.ParagraphFormat.Alignment = ParagraphAlignment.Left ' ← applied to the following paragraph
So the alignment is set after the text is inserted, which leaves the existing paragraph with the default alignment (center/right depending on the previous cell). The same happens for the second and third cells.
How to fix it
Set the paragraph alignment before you write the text (or create a new paragraph) and, if you want the whole table left‑aligned, also set the table alignment.
Updated code (VB.NET)
Public Function Replacing(args As ReplacingArgs) As ReplaceAction Implements IReplacingCallback.Replacing
_Replacement = (From r In _Replacement Order By _datautil.GetInt32(r.Type) Ascending).ToList()
Dim builder As New DocumentBuilder(args.MatchNode.Document)
builder.MoveTo(args.MatchNode)
Dim prvCCDocType As CompletionCertificateDocType = CompletionCertificateDocType.Header
For Each doc In _Replacement
If doc.Type = CompletionCertificateDocType.Header OrElse prvCCDocType <> doc.Type Then
If prvCCDocType <> doc.Type Then
prvCCDocType = doc.Type
builder.EndTable()
End If
'--- Start a new header row -------------------------------------------------
builder.StartTable()
' Cell formatting (shading, font, height, padding)
builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.LightGray
builder.Font.Bold = True
builder.Font.Size = 11
builder.RowFormat.Height = 15
builder.CellFormat.TopPadding = 2
' ---------- Column 1 ----------
builder.InsertCell()
builder.CellFormat.Width = 300
builder.ParagraphFormat.Alignment = ParagraphAlignment.Left ' **set before Write**
builder.Write(doc.LableCol1)
' ---------- Column 2 ----------
builder.InsertCell()
builder.CellFormat.Width = 300
builder.ParagraphFormat.Alignment = ParagraphAlignment.Left ' **set before Write**
builder.Write(doc.LableCol2)
' ---------- Column 3 ----------
builder.InsertCell()
builder.CellFormat.Width = 300
builder.ParagraphFormat.Alignment = ParagraphAlignment.Left ' **set before Write**
builder.Write(doc.LableCol3)
builder.EndRow()
' Clear cell/paragraph formatting for the next rows
builder.CellFormat.ClearFormatting()
builder.ParagraphFormat.ClearFormatting()
End If
'--- Insert the body document into the next cell -----------------------------
builder.InsertCell()
builder.InsertDocument(doc.Document, ImportFormatMode.KeepSourceFormatting)
builder.EndRow()
builder.EndTable()
' Prepare for the next header (if any)
builder.StartTable()
Next
builder.EndTable()
Return ReplaceAction.Replace
End Function
Optional: Align the whole table to the left
If the table itself is being centered on the page, set its alignment after StartTable
:
builder.StartTable()
builder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Left ' or TableAlignment.Left
(See Aspose.Words → Working with tables → Table alignment.)
Related Documentation
Next Steps
- Apply the changes above (set
ParagraphFormat.Alignment
before Write
).
- Run the code and verify that all three header cells are left‑aligned.
- If the whole table still appears offset, use the optional table‑alignment snippet.
If the problem persists, please share:
- A small sample document that reproduces the issue.
- The exact version of Aspose.Words for .NET you are using.
I’ll be happy to help you further!
@shivrajp Could you please attach your input and output documents here for our reference? We will check the issue and provide you more information. A simple console application that will allow us to reproduce the problem will be very useful.
Have you tried calling table.AutoFit(AutoFitBehavior.FixedColumnWidths)
after finishing building the header table?