none
OpenXml adding table to the middle of document. RRS feed

  • Question

  • I copy by this code a table and i add it to document.

    Using Doc As WordprocessingDocument = WordprocessingDocument.Open("D:\Projekty\Visual Basic 2013\Zdrojáky\OpenXML skuska\OpenXML skuska6\Sablona2.docx", True)
    
                For poc = 0 To 1
                    Dim table As New Table(Doc.MainDocumentPart.Document.Body.Descendants(Of Table)().FirstOrDefault().CloneNode(True))
    
                    For Each tr As TableRow In table.Descendants(Of TableRow)()
                        For Each tc As TableCell In tr.Descendants(Of TableCell)()
                            Dim run As New Run
                            run.Append(New Text("Hello"))
                            tc.Descendants(Of Paragraph)().FirstOrDefault().Append(run)
                        Next
                    Next
    
                    Doc.MainDocumentPart.Document.Body.Append(table)
                    Doc.MainDocumentPart.Document.Body.Append(New Paragraph())
                Next
    

    But i have one problem. Table that i copping is in the middle of document.  And need to place other tables right behind it.  But now it place them in the bottom side of document. How to add tables to the middle of document.

    Thank you for your answer.


    Wednesday, May 20, 2015 6:11 PM

Answers

  • At last i solve my problem this way:
            Using Doc As WordprocessingDocument = WordprocessingDocument.Open("D:\Projekty\Visual Basic 2013\Zdrojáky\OpenXML skuska\OpenXML skuska6\Sablona2.docx", True)
                Dim tableDel As Table
                tableDel = Doc.MainDocumentPart.Document.Body.Elements(Of Table)().ElementAt(0)
    
                For poc = 0 To 10
    
                    Dim table As New Table(Doc.MainDocumentPart.Document.Body.Descendants(Of Table)().FirstOrDefault().CloneNode(True))
    
                    Dim tc As TableCell = table.Descendants(Of TableCell)().ElementAt(1)
                    tc.Descendants(Of Paragraph)().FirstOrDefault().Remove()
                    tc.Append(New Paragraph)
                    Dim run As New Run
                    run.Append(New Text("text zápis 1 - " & poc))
                    tc.Descendants(Of Paragraph)().FirstOrDefault().Append(run)
    
                    Dim tc1 As TableCell = table.Descendants(Of TableCell)().ElementAt(3)
                    tc1.Descendants(Of Paragraph)().FirstOrDefault().Remove()
                    tc1.Append(New Paragraph)
                    Dim run1 As New Run
                    run1.Append(New Text("text zápis 2 - " & poc))
                    tc1.Descendants(Of Paragraph)().FirstOrDefault().Append(run1)
    
                    Dim tc2 As TableCell = table.Descendants(Of TableCell)().ElementAt(5)
                    tc2.Descendants(Of Paragraph)().FirstOrDefault().Remove()
                    tc2.Append(New Paragraph)
                    Dim run2 As New Run
                    run2.Append(New Text("text zápis 3 - " & poc))
                    tc2.Descendants(Of Paragraph)().FirstOrDefault().Append(run2)
    
                    Dim runNull As New Run(New Text(" "))
                    Dim newParagraph As Paragraph = New Paragraph(runNull)
                    Doc.MainDocumentPart.Document.Body.Descendants(Of Paragraph)().ElementAt(37 + (7 * poc)).InsertAfterSelf(newParagraph)
                    Doc.MainDocumentPart.Document.Body.Descendants(Of Paragraph)().ElementAt(37 + (7 * poc)).InsertAfterSelf(table)
    
                Next
    
                tableDel.Remove()
    
            End Using
    I dont know if this is best way but it works.
    Friday, May 22, 2015 6:22 PM

All replies

  • Hi Vladimir

    The table is coming at the bottom of the document because you use Body.Append. "Body" is the entire main text "story" of the Word document. In order to place the table at a different location you need to get that "target", which would be an existing (empty) or new paragraph. If a new paragraph, then that would need to be appended to an existing paragraph.

    So the first thing you need to do is have a way to identify that target paragraph in the middle of the document. Does it have any special characteristics? Or is there a bookmark or, even better, a content control at that position?


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, May 20, 2015 6:56 PM
    Moderator
  • On that position is table that i copy. And i need to place those new tables right behind it.
    How do i found out it position? And after that how do i place tables to document based on position that i found?

    Here is link to file, for you to know what is point of conversation.

    http://www.suarpsystems.sk/Download/Sablona2.zip

    thank you


    Thursday, May 21, 2015 7:33 AM
  • At last i solve my problem this way:
            Using Doc As WordprocessingDocument = WordprocessingDocument.Open("D:\Projekty\Visual Basic 2013\Zdrojáky\OpenXML skuska\OpenXML skuska6\Sablona2.docx", True)
                Dim tableDel As Table
                tableDel = Doc.MainDocumentPart.Document.Body.Elements(Of Table)().ElementAt(0)
    
                For poc = 0 To 10
    
                    Dim table As New Table(Doc.MainDocumentPart.Document.Body.Descendants(Of Table)().FirstOrDefault().CloneNode(True))
    
                    Dim tc As TableCell = table.Descendants(Of TableCell)().ElementAt(1)
                    tc.Descendants(Of Paragraph)().FirstOrDefault().Remove()
                    tc.Append(New Paragraph)
                    Dim run As New Run
                    run.Append(New Text("text zápis 1 - " & poc))
                    tc.Descendants(Of Paragraph)().FirstOrDefault().Append(run)
    
                    Dim tc1 As TableCell = table.Descendants(Of TableCell)().ElementAt(3)
                    tc1.Descendants(Of Paragraph)().FirstOrDefault().Remove()
                    tc1.Append(New Paragraph)
                    Dim run1 As New Run
                    run1.Append(New Text("text zápis 2 - " & poc))
                    tc1.Descendants(Of Paragraph)().FirstOrDefault().Append(run1)
    
                    Dim tc2 As TableCell = table.Descendants(Of TableCell)().ElementAt(5)
                    tc2.Descendants(Of Paragraph)().FirstOrDefault().Remove()
                    tc2.Append(New Paragraph)
                    Dim run2 As New Run
                    run2.Append(New Text("text zápis 3 - " & poc))
                    tc2.Descendants(Of Paragraph)().FirstOrDefault().Append(run2)
    
                    Dim runNull As New Run(New Text(" "))
                    Dim newParagraph As Paragraph = New Paragraph(runNull)
                    Doc.MainDocumentPart.Document.Body.Descendants(Of Paragraph)().ElementAt(37 + (7 * poc)).InsertAfterSelf(newParagraph)
                    Doc.MainDocumentPart.Document.Body.Descendants(Of Paragraph)().ElementAt(37 + (7 * poc)).InsertAfterSelf(table)
    
                Next
    
                tableDel.Remove()
    
            End Using
    I dont know if this is best way but it works.
    Friday, May 22, 2015 6:22 PM