Android Question DB to text for pdfcreate

harinder

Active Member
Licensed User
Longtime User
I have the following table in my DB:
B4X:
   Red  Green Blue
     5    6   4
     4    3   2
     4    3   5
     5    3   4

I want to use this data to create a pdf page. Code as follows:


B4X:
    Dim redc As List
    redc.Initialize
    Dim rs As ResultSet=sql.ExecQuery $" Select * from current "$
    Dim cre(3) As Object
    Do While rs.NextRow
        cre(0) =rs.Getstring("red")
        cre(1)=rs.Getstring("green")
        cre(2)=rs.Getstring("blue")
        redc.Addall(Array As String(""&cre(0),""&cre(1),""&cre(2)))
    Loop
    rs.Close
    File.Writelist (File.DirInternal, "mylist.txt", redc)

I am using mylist.txt file in:
B4X:
pdf.Canvas.DrawText(File.ReadString(File.dirinternal,"mylist.txt"), 250, 150, Typeface.DEFAULT_BOLD, 30 / GetDeviceLayoutValues.Scale , Colors.blue, "CENTER")

But I am getting pdf as follows in one line :
B4X:
 5 6 4 4 3 2 4 3 5 5 3 4

How can I make it appear in 4 rows as in DB? Thanks
 

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
I think the reason it is noty working as expected is that you need some Carridge returns in the output.

Reading the definition of Write List the easiest way to do this would be to change the code as follows:

1609171780214.png


B4X:
 Dim redc As List
    redc.Initialize
    Dim rs As ResultSet=sql.ExecQuery $" Select * from current "$
       Do While rs.NextRow
        ' Add a CRLF after every set of values.
        redc.Addall(Array As String(rs.getstring("red"),rs.getstring("green"),rs.getstring("blue"), CRLF))
    Loop
    rs.Close
    File.Writelist (File.DirInternal, "mylist.txt", redc)

BTW, is there a reason you cast the result from a string to an object to a string again?
 
Upvote 0

harinder

Active Member
Licensed User
Longtime User
Now I am getting following using CRLF:
B4X:
 5 6 4  4 3 2  4 3 5  5 3 4
Can you suggest a better way to create pdf from a table and maintain data structure?
 
Last edited:
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Without fully understanding exactly what you are doing, you could remove the need for an external textfile completely.

B4X:
dim outputstr as string = ""
    Dim rs As ResultSet=sql.ExecQuery $" Select * from current "$
       Do While rs.NextRow
        ' Add a CRLF after every set of values.
        outputstr = outputstr & $"${rs.getstring("red")},${rs.getstring("green")},${rs.getstring("blue")}${CRLF}"$
    Loop
    rs.Close
    pdf.Canvas.DrawText(outputstr, 250, 150, Typeface.DEFAULT_BOLD, 30 / GetDeviceLayoutValues.Scale , Colors.blue, "CENTER")

or it could be that you write each line separately.
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Yes, remove the commas.

as far as the spaces go I would have expected that these would have been Carriage returns.

Perhaps the solution is to put the drawtext within the loop.

something such as:
B4X:
dim outputstr as string = ""
dim yoff as float = 0
    Dim rs As ResultSet=sql.ExecQuery $" Select * from current "$
       Do While rs.NextRow
        ' Add a CRLF after every set of values.
        outputstr = $"${rs.getstring("red")} ${rs.getstring("green")} ${rs.getstring("blue")}"$
    pdf.Canvas.DrawText(outputstr, 250, 150+ yoff, Typeface.DEFAULT_BOLD, 30 / GetDeviceLayoutValues.Scale , Colors.blue, "CENTER")   
    yoff = yoff + pdf.Canvas.MeasureStringHeight(outputstr, Typeface.DEFAULT_BOLD, 30 / GetDeviceLayoutValues.Scale)
    Loop
    rs.Close
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Canvas.DrawText ignores the CRLF character and replaces it by an empty character.
DrawText("Test text" and DrawText("Test" & CRLF & "text" give the same result.

You need to write each line in a loop and change the Y coordinate for each line.
 
Upvote 0

harinder

Active Member
Licensed User
Longtime User
Yes, remove the commas.

as far as the spaces go I would have expected that these would have been Carriage returns.

Perhaps the solution is to put the drawtext within the loop.

something such as:
B4X:
dim outputstr as string = ""
dim yoff as float = 0
    Dim rs As ResultSet=sql.ExecQuery $" Select * from current "$
       Do While rs.NextRow
        ' Add a CRLF after every set of values.
        outputstr = $"${rs.getstring("red")} ${rs.getstring("green")} ${rs.getstring("blue")}"$
    pdf.Canvas.DrawText(outputstr, 250, 150+ yoff, Typeface.DEFAULT_BOLD, 30 / GetDeviceLayoutValues.Scale , Colors.blue, "CENTER")  
    yoff = yoff + pdf.Canvas.MeasureStringHeight(outputstr, Typeface.DEFAULT_BOLD, 30 / GetDeviceLayoutValues.Scale)
    Loop
    rs.Close
This helped. Thank you..
 
Upvote 0
Top