Measure and wrap the text
Normalize file line endings before measuring. Preserve paragraph breaks and join only breaks inside the same sentence. WrapPdf takes one paragraph at a time and uses GetTextWidth to test whether the prefix fits.
View code and commands · Wrap a paragraph by width
Static Function WrapPdf(oPrinter, oFont, cText, nWidth)
Local aLines := {}
Local cRest := AllTrim(cText)
Local nCut := 0
Local nSpace := 0
While !Empty(cRest)
nCut := Len(cRest)
// Consume at least one character when wrapping a long word.
While nCut > 1 .And. oPrinter:GetTextWidth(Left(cRest, nCut), oFont) > nWidth
nCut--
EndDo
// If the cut falls inside a word, try the previous space.
If nCut < Len(cRest) .And. SubStr(cRest, nCut + 1, 1) != " "
nSpace := RAt(" ", Left(cRest, nCut))
If nSpace > 1
nCut := nSpace
EndIf
EndIf
AAdd(aLines, AllTrim(Left(cRest, nCut)))
cRest := AllTrim(SubStr(cRest, nCut + 1))
EndDo
Return aLinesThe helper returns lines in reading order to calculate the block height. Pass one paragraph and use the same width unit that GetTextWidth returns.
Reduce the width and check that the line count increases. Try a long unbroken word and verify that wrapping consumes all the text.

Comments
There are no approved comments yet.
Sign in with Google or Microsoft to comment.