Floating shapes (textbox)

Hi,

We are actually “trying” Aspose Words. It’s great for the major part of our business needs.

What we are trying to achieve is to render “text boxes” in an absolute position. Our customers can define fields and their position on the final report.

We’ve been able to do so, by adding shapes (textbox type). We can set top and left position so the textbox is displayed where we want it.

The problem we are facing now is that no pagebreak is done if a textbox is drawed “outside” the bouds of the page. For example, if the page has 500 pixels height and we add a textbox with a top position of 550, the textbox just doesn’t appear. We would like this textbox to appear on the second page at top position 50.

So we would like to know if there is a way to force page break? Or is there another way to insert floating text boxes in our report.

Note that we use aspose.words (some parts of the report work with docx template) and we save it as a .PDF.

Thanks a lot,

Jsr

CODE:

Shared Sub Main()

    Dim exeDir As String = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
    Dim dataDir As String = Path.GetFullPath(Path.Combine(exeDir, "…\Data"))
    Dim doc As New Document()
    Dim builder As New DocumentBuilder(doc)

    builder.MoveToDocumentEnd()

    Dim goodShape As Drawing.Shape = GetTextBox(builder, 50, 250, 100, 20)
    Dim goodParag As New Paragraph(doc)


    ' Will appear
    goodParag.AppendChild(New Run(doc, "Good textbox"))
    goodShape.AppendChild(goodParag)
    builder.InsertNode(goodShape)

    ' Won’t appear
    Dim badShape As Drawing.Shape = GetTextBox(builder, 50, 800, 100, 20)
    Dim badParag As New Paragraph(doc)

    badParag.AppendChild(New Run(doc, "Bad textbox"))
    badShape.AppendChild(badParag)
    builder.InsertNode(badShape)

    doc.Save(dataDir & "TestFile Out.pdf", SaveFormat.Pdf)


End Sub

Private Shared Function GetTextBox(ByVal builder As DocumentBuilder, ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer) As Drawing.Shape
    Dim txt As New Aspose.Words.Drawing.Shape(builder.Document, Drawing.ShapeType.TextBox)

    txt.Left = x
    txt.Top = y
    txt.WrapType = Drawing.WrapType.None
    txt.TextBox.TextBoxWrapMode = Drawing.TextBoxWrapMode.None
    txt.Width = width
    txt.Height = height
    txt.AllowOverlap = True


    Return txt
End Function

Hi
Thanks for your request. When you insert a floating shape into MS Word document it is anchored to the paragraph where you insert it. So if the paragraph is on first page, the shape will appear on the first page too even if its position is outside the page.
You can resolve this by comparing position of the shape and size of page, if position is outside the page, you can insert a page break:
https://reference.aspose.com/words/net/aspose.words/documentbuilder/insertbreak/
Then recalculate position of the shape on the next page and then insert it.
Best regards,