How to Calculate the Position of Objects Relative to Other Objects with the PrintDocument Control
The C1PrintDocument RenderDirect method allows you to render an object (text, image, etc) at a specific location on the page using X and Y coordinates. But how can you calculate the desired position for some objects to be rendered relative to others? For this you can use the CalcSize method to return an objects rendered height and width, for which you can use those values to help position additional objects. The example below renders two lines of text. The second line is positioned below the first by using the first line's calculated height, plus an additional 10mm for spacing.
' Create a non-flowable document
C1PrintDocument1.AllowNonReflowableDocs = True
' Set ResolvedUnit to determine what unit will be used by CalcSize
C1PrintDocument1.ResolvedUnit = UnitTypeEnum.Mm
' Determine page width used to calculate height needed for text
Dim ps As C1PageSettings
ps = C1PrintDocument1.PageLayout.PageSettings
Dim pageWidth As Double
pageWidth = ps.Width.Value - ps.LeftMargin.Value - ps.RightMargin.Value
' Start document creation
C1PrintDocument1.StartDoc()
' Create first block of text
Dim text1 As New C1.C1Preview.RenderText()
text1.Text = "Test line 1"
' Add the text temporarily to calculate its size
C1PrintDocument1.Body.Children.Add(text1)
Dim sz As SizeD
sz = text1.CalcSize(New Unit(pageWidth, ps.Width.Units), Unit.Auto)
'remove the object added temporarily
C1PrintDocument1.Body.Children.Remove(text1)
' Render first block of text to document
C1PrintDocument1.RenderDirect("10mm", "10mm", text1)
' Create second block of text
Dim text2 As New C1.C1Preview.RenderText()
text2.Text = "Test line 2"
' Render second block of text below the first using the first ones calculated height
C1PrintDocument1.RenderDirect("10mm", "sz.Height + 10mm", text2)
Hunter Haaf