none
Moving a Line and a Textbox shape RRS feed

  • Question

  • Hi

    We have a Word template with various parts which we dynamically fill with data using a WebService and Open XML.

    In some scenarios we need to horisontally move object when some other object has been removed.

    We have problems finding a way to move Lines and Shapes with text content. Se image below.

    Part of Word document

    We have tried the following code to move the Line above the textbox to a location to the right of the current position.

    Try
      Using doc = WordprocessingDocument.Open(docFile, True)
      Dim lines As IEnumerable(Of DocumentFormat.OpenXml.Vml.Line) = doc.MainDocumentPart.Document.Body.Descendants(Of DocumentFormat.OpenXml.Vml.Line)()
      For Each shp As DocumentFormat.OpenXml.Vml.Line In lines
         If shp.Id = "Person1_Line" Then
            shp.From.Value = "148pt,395.45pt"
            shp.To.Value = "283.2pt,395.45pt"
         End If
      Next
      End Using
    Catch ex As Exception
                
    End Try

    When we examine the result the Line-shape has the correct values but the line does not move to that position in the document.

    Whats is it I'm missing here?

    We have similar problems with the textbox in which we change the style-part margin-left of the shape but the textbox does not move.

    All help is appreciated


    Best Regards Peter Karlström Midrange AB, Sweden

    Monday, September 2, 2019 8:32 PM

Answers

  • OK.
    For the benefit of the Community, here is my solution.

    Proces needs 3 parameters:
    docfile - path to the document/template
    shapeName - the name of the Line or the Shape
    leftPos - numerical value in point

            Try
                Using doc = WordprocessingDocument.Open(docFile, True)
                    Dim drws As IEnumerable(Of DocumentFormat.OpenXml.Wordprocessing.Drawing) = doc.MainDocumentPart.Document.Body.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Drawing)()
                    For Each shp As DocumentFormat.OpenXml.Wordprocessing.Drawing In drws
                        If shp.OuterXml.Contains(shapeName) Then
                            Dim anch As DocumentFormat.OpenXml.Drawing.Wordprocessing.Anchor = shp.Anchor
                            Dim newOffset As New PositionOffset
                            newOffset = anch.HorizontalPosition.PositionOffset
                            newOffset.Text = leftPos.ToString
                            anch.HorizontalPosition.PositionOffset = newOffset
                        End If
                    Next
                End Using
            Catch ex As Exception
    
            End Try


    Best Regards Peter Karlström Midrange AB, Sweden

    Thursday, September 5, 2019 5:01 PM

All replies

  • Really! No one?

    Best Regards Peter Karlström Midrange AB, Sweden

    Tuesday, September 3, 2019 7:36 PM
  • OK.
    For the benefit of the Community, here is my solution.

    Proces needs 3 parameters:
    docfile - path to the document/template
    shapeName - the name of the Line or the Shape
    leftPos - numerical value in point

            Try
                Using doc = WordprocessingDocument.Open(docFile, True)
                    Dim drws As IEnumerable(Of DocumentFormat.OpenXml.Wordprocessing.Drawing) = doc.MainDocumentPart.Document.Body.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Drawing)()
                    For Each shp As DocumentFormat.OpenXml.Wordprocessing.Drawing In drws
                        If shp.OuterXml.Contains(shapeName) Then
                            Dim anch As DocumentFormat.OpenXml.Drawing.Wordprocessing.Anchor = shp.Anchor
                            Dim newOffset As New PositionOffset
                            newOffset = anch.HorizontalPosition.PositionOffset
                            newOffset.Text = leftPos.ToString
                            anch.HorizontalPosition.PositionOffset = newOffset
                        End If
                    Next
                End Using
            Catch ex As Exception
    
            End Try


    Best Regards Peter Karlström Midrange AB, Sweden

    Thursday, September 5, 2019 5:01 PM