DynaPDF WriteFText last line handling

We recently had a client writing a nice application outputting PDF with our MBS Xojo DynaPDF Plugin. You can use the WriteFText function (or WriteStyledText) to layout text over multiple boxes on multiple pages to do layouts like a newspaper or a magazine. You can continue on a different page (next one, previous one, jump 10 pages forward) and leave room to place images.

Today we have the little trick to avoid having a last line in a paragraph on the next page like in the picture above. As you see we drew a rectangle to show the area we use for WriteFText to draw the text. Since our text doesn’t fit, the PageBreak event gets called and we start a new page to continue drawing there.

Recently we added the GetLastTextOffset function to ask where in the text we are for the page break. We can measure the height of the text going on the next box and just add this text as a line below the block, if it is short. This avoids the one line on the top of the next page. Here is the sample code:

EventHandler Function PageBreak(LastPosX as double, LastPosY as double, PageBreak as boolean) As integer
	If fix Then
		
		// where did text finish?
		Dim LastTextPosY As Double = Me.GetLastTextPosY
		
		// query the text rectangle
		Dim RectX As Double
		Dim RectY As Double
		Dim RectW As Double
		Dim RectH As Double
		Call Me.GetTextRect(RectX, RectY, RectW, RectH)
		
		// now check how much text is left
		Dim pos As Integer = Me.GetLastTextOffset
		Dim textLeft As String = CurrentText.Mid(pos+1)
		
		// measure height
		Dim Heigth As Double = Me.GetTextHeightEx(RectW, Me.ktaJustify, textLeft)
		
		// if small, then add below existing block
		If Heigth < 20 Then
			Call Me.SetTextRect(RectX, LastTextPosY, RectW, 30)
			
			Return Me.kNEW_ALIGN_JUSTIFY
		End If
	End If
	
	// make a new page and continue
	Call Me.EndPage
	Call Me.Append
	
	// continue
	Return Me.kNEW_ALIGN_JUSTIFY
End EventHandler 

Please try and let us know if you have questions. See the example “Create PDF with text block2” coming with 23.4 plugin versions.

If you like to use this, get a DynaPDF Starter (or higher) license, e.g. with OmegeBundle.

from our blog: DynaPDF WriteFText last line handling

1 Like