Table column width problem

I’m using a fairly complicated Word template which I fill with various html table strings (see attachment 1, the beggining of the word document). I fill the document by moving to bookmarks and by replacing the bookmark with html code:

If builder.MoveToBookmark("HB75604") Then
    builder.InsertHtml(htmlTableString)
    builder.CurrentParagraph.Remove()
End If

There is another ‘hard coded’ table under the inserted table to emphasise a ‘goal value’. E.g. see the table cell to the right of ‘Vakava harkitsevainen’. Works fine except the following (see attachment 2):

  1. The underneath table is indented a bit and not therefore aligned to the inserted table in the same way
  2. Table column (cell) withs are not evenly distributed anymore

How can I get around this problem?

Hi

Thanks for your inquiry. There is a mistake in your HTML. Here is HTML from bookmarks HB:

<A name=HB><table style='text-align: center; width: 220px; border-left-style: solid; border-left-width: 1px; border-right: 1px solid #C0C0C0; border-top-style: solid; border-top-width: 1px; border-bottom: 1px solid #C0C0C0;'><tr><td style='width: 10%;'>%1</td><td style='width: 10%;'>%2</td><td style='width: 10%;'>%3</td><td style='width: 10%;'>%4</td><td style='width: 10%;'>%5</td><td style='width: 10%;'>%6</td><td style='width: 10%;'>%7</td><td style='width: 10%;'>%8</td><td style='width: 10%;'>%9</td><td style='width: 10%;'%10</td></tr></table><br /></A>

I highlighted the problematic part. It should be the following:

<td style=’width: 10%;’>%10</td>

Best regards.

That was my error, sorry. The problem still remains the same: Cannot align the two tables in the same way. Tried the whole thing with an html editor (except from the predefined word table which i don’t know how to get as html) and everything seems ok there.

Hi

Thanks for your inquiry. You can try using the following code to resolve this problem:

if (builder.MoveToBookmark("HB75604"))
{
    builder.InsertHtml(html2);
    // Get parent node
    CompositeNode parentNode = builder.CurrentParagraph.ParentNode;
    // Remove current paragraph
    builder.CurrentParagraph.Remove();
    // Get collection of rows
    NodeCollection rows = parentNode.GetChildNodes(NodeType.Row, true);
    foreach(Row row in rows)
        row.RowFormat.ClearFormatting();
}

Hope this helps.
Best regards.

Thanks, solved the table cell alignments but naturally removed my text alignment. Still minor issues with the tables (check the picture I have attached). The cell border does not end at the cell corner. I can live with this but need to solve the alignment

Hi

Thanks for your request. Please simplify your source document and provide me code example that allows me to reproduce the problem. I will investigate the issue and provide you more information.
Best regards.

Here a simplified document. the lates code is here:

'open the document attached
'Get the html template:
If builder.MoveToBookmark("HB", True, True) Then
    HBHTML = builder.CurrentParagraph.ToTxt.Replace("’", "'")
    builder.CurrentParagraph.Remove()
End If
'Here I insert the value to the html template by replacing the %x% tag
'Update the word document
If builder.MoveToBookmark("HB75604") Then
    builder.InsertHtml(getWordHTMLTableTemplateResult(HBHTML, dRow))
    Node = builder.CurrentParagraph.ParentNode
    builder.CurrentParagraph.Remove()
    Dim Rows As NodeCollection = Node.ParentNode.GetChildNodes(NodeType.Row, True)

    For Each Row As Tables.Row In Rows
        Row.RowFormat.ClearFormatting()

    Next
End If
'save the document

Hi

Thank you for additional information. There is also getWordHTMLTableTemplateResult method. Could you provide me this method? I cannot reproduce the problem without it.
Best regards.

This is a routine to replace the %x% with data from the database doing e.g. the following:

HTMLTemplate= HTMLTemplate.Replace("%1%", "data")

and then zeroes the remaining %x% tags with:

For j = -11 To 11
    'clear once we have replaced the value
    HTMLTemplate = HTMLTemplate.Replace("%" & j.ToString.Trim & "%", "á")
Next 

Hope this helps

Hi

Thanks you for addition information. I am still unable to reproduce the problem. If it is possible, could you please create simple application that will allow me to reproduce the problem?
Best regards.

Hi, which of the problems are we talking about? a) The original one with the table cells not aligned right or the later b) The cell border line? Maybe it would be easier if you send me your test project that works and I add my original code to that. That would make it possible for me to see which of my code causes the problem.

We are talking about text align problem. It would be easier if you create simple application that will demonstrate the problem.
Here is code I used for testing:

Public Sub Test003()
    Dim doc As Document = New Document("Test003\QAQuestionnaireFormTemplateCustom2.doc")
    Dim builder As DocumentBuilder = New DocumentBuilder(doc)
    Dim HBHTML As String Dim node As Node

'open the document attached
'Get the html template:
If builder.MoveToBookmark("HB", True, True) Then
        HBHTML = builder.CurrentParagraph.ToTxt.Replace("’", "'")
        builder.CurrentParagraph.Remove()
    End If
    'Here I insert the value to the html template by replacing the %x% tag
    'Update the word document
    If builder.MoveToBookmark("HB75604") Then
        builder.InsertHtml(getWordHTMLTableTemplateResult(HBHTML))
        Node = builder.CurrentParagraph.ParentNode
        builder.CurrentParagraph.Remove()
        Dim Rows As NodeCollection = Node.ParentNode.GetChildNodes(NodeType.Row, True)

        For Each Row As Tables.Row In Rows
            Row.RowFormat.ClearFormatting()

        Next
    End If
    doc.Save("Test003\out.doc")

End Sub
Private Function getWordHTMLTableTemplateResult(ByVal HTMLTemplate) As String HTMLTemplate = HTMLTemplate.Replace("%1%", "data")

For j As Integer = -11 To 11
        'clear once we have replaced the value
        HTMLTemplate = HTMLTemplate.Replace("%" & j.ToString.Trim & "%", " ")

    Next
    Return HTMLTemplate
End Function

Best regards.

Hi again
I just tried to used your code and it does exactly the same thing as mine. Replace more than one bookmark (e.g. HA75682 HA75688 HB75677 etc) so that you get a clearer view. I can live with this problem to a later stage (the left align instead of the center align as in the html code is the one I want to have fixed), the real problem is that I cannot get the PDF done properly.

Hi

Thanks for your request. Maybe, you should try specify width of columns in points and text align in paragraph style. See the following HTML:

<table><tr><td style='width:500pt;'><p style='text-align:right;'>1st column</p></td></tr></table>

Hope this helps.
Best regards.

I have now tried a lot of different options and getting tired of acting as a test site. This costs us money now (late project) and you have not met up to the promises. We have paid for an update beleiving we will get the document created properly. We cannot get a Word document and thereby not a PDF document. Both has bugs related to tables. The suggestions / work arounds has not worked and we have no other option than finding another solution if we do not get this fixed rapidly. You clearly have a problem with aligning / table sizes. Please also test inserting / aligning pictures in the table cells inside other tablse so that we do not have to test these because this would maybe be an alternative solution even though we have to do extra programming in this case.

Hi

Thanks for your request. Actually, you have chosen an error prone way to fill you template with data. There are many restrictions in HTML format.
May I suggest you a different way to fill your document with data. Here is my suggestion:
As I can see, you should fill only one column in your document. Data that should be inserted into this column are small tables. Maybe, you should avoid using HTML and build these small tables in your template. Then you will be able to replace placeholders in your template. For example, see the attached template and the following code:

Public Sub Test003()
    Dim doc As Document = New Document("Test003\temp.doc")
    'insert data into the document
    UpdateData(doc, "HA47090", 3, "3")
    UpdateData(doc, "HA75670", 4, "4")
    UpdateData(doc, "HA75688", 5, "5")
    UpdateData(doc, "HB75604", 1, "1")
    UpdateData(doc, "HB75605", 10, "10")
    doc.Save("Test003\out.doc")
    doc.SaveToPdf("Test003\out.pdf")
End Sub

Private Sub UpdateData(ByVal doc As Document, ByVal bookmarkName As String, ByVal colIdx As Integer, ByVal data As String)

    'Check whether bookmark exists
    If (doc.Range.Bookmarks(bookmarkName) Is Nothing) Then

        Return
    End If
    'get table row where bookmark is placed
    Dim row As Node = doc.Range.Bookmarks(bookmarkName).BookmarkStart.GetAncestor(NodeType.Row)

    'Replace placeholders with data
    For j As Integer = 1 To 11
        'Get data
        Dim replacement As String = String.Empty

        If (j = colIdx) Then
            replacement = data
        End If
        'Get placeholder name
        Dim placeholder As String = String.Format("%{0}%", j)

        'Replace placeholders
        row.Range.Replace(placeholder, replacement, False, False)
    Next
End Sub

Hope this could help you.
Best regards.

Ok,thanks for the example but this way requires that our users must define a table for each result. These can be over 1000 … :-(. Inserting a 1000 tables and aligning them right at a bookmark with Word is something I would suggest only for our competitors. We do not know in which of the e.g. table cell the result will be placed so therefore we had the replacement of tags in a loop but true your way of replacing is probably more efficient. I rewrote the whole thing to get around ths problem and now use pictures inserted at the bookmark instead of the table with text. Just tell us when you get this fixed so that we can use the table method also in the future.

Ok, fine. I have another suggestion. Let’s do not insert anything in the table column except bookmarks, then build tables on fly, and insert them into the template. Please see the following code and attached document:

Public Sub Test004()
    Dim doc As Document = New Document("Test004\in.doc")
    'insert data into the document
    InsertData(doc, "HA47090", New String() {"", "", "3", "", "", ""}, New Integer() {4, 5})
    InsertData(doc, "HA75670", New String() {"", "", "", "4", "", ""}, New Integer() {})
    InsertData(doc, "HA75688", New String() {"", "", "", "", "5", ""}, New Integer() {})
    InsertData(doc, "HB75604", New String() {"", "2", "", "", "", "", "", "", "", ""}, New Integer() {6, 7, 8, 9})
    InsertData(doc, "HB75605", New String() {"", "", "", "", "", "", "", "8", "", ""}, New Integer() {})
    doc.Save("Test004\out.doc")
    doc.SaveToPdf("Test004\out.pdf")

End Sub

''' 
''' Generates tabel and insert it at bookmark
''' 
''' Destination document
''' Bookmark name
''' Widht of the table
''' Values
''' indexes of undelined cells
''' 
Private Sub InsertData(ByVal doc As Document, ByVal bookmarkName As String, ByVal data As String(), ByVal underlinedCells As Integer())
    'Check whether bookmark exists
    If (doc.Range.Bookmarks(bookmarkName) Is Nothing) Then

        Return
    End If
    'get cell where bookmark is placed
    Dim bkCell As Tables.Cell = CType(doc.Range.Bookmarks(bookmarkName).BookmarkStart.GetAncestor(NodeType.Cell), Tables.Cell)

    'Calculate width of table cells
    Dim cellWidth = bkCell.CellFormat.Width / data.Length
    'Build table based on data
    Dim tab As Tables.Table = New Tables.Table(doc)
    Dim row As Tables.Row = New Tables.Row(doc)
    tab.AppendChild(row)
    For i As Integer = 0 To data.Length - 1
        Dim cell As Tables.Cell = New Tables.Cell(doc)
        cell.CellFormat.Width = cellWidth
        'Insert text into teh cell
        Dim par As Paragraph = New Paragraph(doc)
        par.ParagraphFormat.Alignment = ParagraphAlignment.Right
        Dim run As Run = New Run(doc, data(i))
        par.AppendChild(run)
        cell.AppendChild(par)
        row.AppendChild(cell)

    Next
    'Set bottom border where it is needed
    For j As Integer = 0 To underlinedCells.Length - 1
        If (row.Cells.Count > underlinedCells(j)) Then
            row.Cells(underlinedCells(j)).CellFormat.Borders.Bottom.LineStyle = LineStyle.Single

        End If
    Next
    'Insert table ta bookmark
    Dim bkNode As Node = doc.Range.Bookmarks(bookmarkName).BookmarkStart.ParentNode
    bkNode.ParentNode.InsertAfter(tab, bkNode)

End Sub

Best regards.