Android Question Fill a Canvas with text from Database (or textfile) - best practise?

DonManfred

Expert
Licensed User
Longtime User
let´s say i already have a canvas object which i can use to draw to.

Based on https://www.prepressure.com/library/paper-size/din-a4
Dimensions A4 measures 210 × 297 millimeters or 8.27 × 11.69 inches. In PostScript, its dimensions are rounded off to 595 × 842 points.

i assumed that a Din A4 pagesize should be 595x842 point when generating a PDF-Document. The Canvas for each page i want to write to

I know the canvas has a size of the values above.

my code so far is

B4X:
    pdf.Initialize("")
    Dim info As PageInfo
    Dim r As Rect
    r.Initialize(0,0,595,842)
    info.Initialize(595,842,1,r)
  
    Dim p As Page = pdf.startPage(info)
    Dim c As Canvas = p.Canvas
  
    Dim srcrec As Rect
    srcrec.Initialize(0,0,ImageView1.Width,ImageView1.Height)

    Dim dstrec As Rect
    dstrec.Initialize(0,0,ImageView1.Width,ImageView1.Height)

    c.DrawBitmap(ImageView1.Bitmap,srcrec,dstrec)
    pdf.finishPage(p)

This results in a pdf page with a image on it. Great so far...

Image now to have some results in a database and i want to write a "table" based on the databaseresults on this canvas.

I never worked with a canvas honestly said... I´ve no experience in working with it.

How would i go now to draw values from the database result to the canvas in a layout like a table.
 

Attachments

  • TestpdfCanvas6.pdf
    43.3 KB · Views: 385

sorex

Expert
Licensed User
Longtime User
I guess it depends on the library limitations.

Most good pdf tools support html strings/files as source then you can just use html based tables in your document.

You texts will look a lot smoother aswell since it remains vector based fonts and not bitmap data.
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
My two cents (using the "classic" canvas way ;))
  • The only needed canvas methods for text are: DrawText, MeasureStringWidth and MeasureStringHeight
  • As each font is different and also they can be monospaced or not, you may want to know which will be the size of a given text before drawing it, so that it will fit the cell. So you can use MeasureStringWidth and MeasureStringHeight in order to know a text size in pixels. This will allow to adjust font size if needed, or "break" the text into different lines if it is long enough...
  • Also take into account that the Y coordinate in Canvas.DrawText refers to its bottom, not the top as int labels

That said, If you have for instance 4 columns and 10 rows of data, now you need to "plan" a layout for this data in code
(totally untested and some values have to be defined, but will give you an idea)
:)
:D
:p Confirmed! Nested spoilers work like a charm!!
B4X:
Dim x_padding as int = 4dip
Dim y_padding as int=2dip
Dim margin as int = page_width/20
Dim NUM_ROWS as int =10
Dim NUM_COLS as int=4
Dim Y_TOP as int = page_height/10
Dim CELL_WIDTH as Int = (page_width - 2*margin)/NUM_COLS    'or whatever you choose
Dim CELL_WIDTH_ACTIVE as int = CELL_WIDTH - 2*x_padding
Dim CELL_HEIGHT as int = ...

'Here you can adapt your font size so that all texts can fit in the cells, or adapt the texts (trubcate, ...) for a given font size
'...
'...
'...
Dim myCanvas as canvas
myCanvas.initialize(...)
Dim myTextentries(NUM_ROWS*NUM_COLS) as string=Array as String("First text","Second text",......)
Dim myFontSize as int = 25
for k=0 to num_entries-1 
  Dim myText as string = myTextEntries(k)
  for mySize=1 to 100
    Dim ww as int = myCanvas.MeasureStringWidth(myText,myTypeface,mySize)
    if ww>=CELL_WIDTH_ACTIVE then
       'Here you can choose to adapt the font size, truncate the text, .....
    end if

    'Can do something similar with height, if you decide to draw multiline.....
  next
next

for y=0 to NUM_ROWS-1
  for x=0 to NUM_COLS-1
    myText=myTextEntries(y*NUM_COLS+x)  'or whatever order they have
    'Texts aligned to the left for each cell
    'myCanvas.DrawText( myText, LEFT_MARGIN + x*CELL_WIDTH + x_padding, Y_TOP+y*CELL_HEIGHT+y_padding, Typeface.DEFAULT, myFontSize, myTextColor, "LEFT")
    'Horizontally center aligned texts in each cell
    myCanvas.DrawText( myText, LEFT_MARGIN + x*CELL_WIDTH + CELL_WIDTH/2, Y_TOP+y*CELL_HEIGHT+y_padding, Typeface.DEFAULT, myFontSize, myTextColor, "CENTER")
  next
next
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
Manfred, what do you want to have at the end?

1. a PDF which contains a picture and a table (see the pdfjet example. There is an example for both: picture and table. Just combine).
2. a canvas with an image AND the table written on it, too?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I guess it depends on the library limitations
I guess you are right. The android Pdf-Document and Pdf-Page does not have much posssibilities.
BUT with this few i am able to create a pdf with a few lines of code. But with no need of a expensive pdf-library.
The bad thing is that i´m now forced to use the Canvas to fill a pdf-page i want to generate.
Most good pdf tools support html strings/files as source then you can just use html based tables in your document.
You are surely right and i saw similar things too when looking at the professional pdf libs in the net.
Unfortunately Google does not provide such helper methods
See https://developer.android.com/reference/android/graphics/pdf/PdfDocument.html
You texts will look a lot smoother aswell since it remains vector based fonts and not bitmap data.
With the Android.jar i´m limited to use the Canvas i guess.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Also take into account that the Y coordinate in Canvas.DrawText refers to its bottom, not the top as int labels
Ohh, i guess this is the problem i encounter yesterday but did not know about this fact...
Thank you!

Can you confirm this (post #1) or do you recomment other values?
i assumed that a Din A4 pagesize should be 595x842 point when generating a PDF-Document

totally untested and some values have to be defined, but will give you an idea
sp10119.png


sp10120.png


sp10121.png


WAAAAAAHHHHHH... THIS WAS GREAT! ROTFL
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Can you confirm this (post #1) or do you recomment other values?
I'm really bad designing layouts. So I would try to find a printed job which I like and then "translate" the observed dimensions (margins, paddings,...) into code
 
Upvote 0
Top