Android Question multiline text on pdf / dirty hack

FrankDev

Active Member
Licensed User
Longtime User
hello

I was looking for a solution to convert multi-line text to a Pdf.
The solution to convert a text field into a graphic was qualitatively no option.

here a short 'dirty hack' maybe someone can do something with it.
(or improve with it :)

the original approach
https://www.b4x.com/android/forum/threads/draw-multiline-text-on-canvas.42933/

B4X:
Sub Globals
 Dim xpos As Float = 0
 Dim ypos As Float = 0
 Dim lineHeight As Float = 0
End Sub

Dim c As Canvas = pdf.Canvas
 Dim txt As String = "This is the first line here there is still a lot to do. at least it's possible. sometimes a bad solution is better than none." & CRLF & "This is the second line here of course you can also do some things" & CRLF & "..." & CRLF & "This is the last line"
 DrawMultilineText(c, txt, 150dip, Typeface.DEFAULT_BOLD, 5, Colors.Black, "LEFT", 5dip)

Sub DrawMultilineText(Canv As Canvas, Text As String, width As Float, Typeface1 As Typeface, textSize As Float, Color As Long, Align1 As String, Space As Float)
 lineHeight = Canv.MeasureStringHeight(Text, Typeface1, textSize)
 For Each line As String In Regex.Split(CRLF, Text)
  Do While line <> ""
 
   line = Messen(Canv,line, Typeface1, textSize, width,Color, Align1, Space )
 
  loop
 Next
End Sub

Sub Messen(Canv As Canvas,line As String,Typeface1 As Typeface,textSize As Float, width As Float,Color As Long, Align1 As String, Space As Float) As String
 
 Dim length As Int = pdf.Canvas.MeasureStringWidth(line, Typeface1 , textSize) 'line fits
 If length < width Then
  Canv.DrawText(line, xpos, ypos, Typeface1, textSize, Color, Align1)
  ypos = ypos + lineHeight + Space
  Return ""
 End If
 
 Dim Printline As String = ""
 Dim newline As String = ""
 Dim pos As Int = 0
 line = line & " "
 
 For i = 0 To line.Length - 1
  Dim c As Char = line.CharAt(i)
  If c = " " Then
   Dim Teststring = glib.Left(line,i) 'vb6 left :)
   Dim length As Int = pdf.Canvas.MeasureStringWidth(Teststring, Typeface1 , textSize)
   If length < width Then
    pos = i
   else
    Printline = glib.Left( Teststring,i)
    Canv.DrawText(Printline, xpos, ypos, Typeface1, textSize, Color, Align1)
    ypos = ypos + lineHeight + Space
    newline = glib.Mid(line,i+1,-1) 'vb6 mid$(-1 = rest of sting) Exit
   End If
  
  End If
 Next
 Return newline.trim
 
End Sub


Translated with www.DeepL.com/Translator
 
Top