Android Question Using pdfdocument and adding an barcode font fails

Using pdfdocument with the canvas to create a PDF file.
However we would like to add a barcode font/text at the PDF.

Typeface:
B4X:
Dim Barcode As Typeface

Barcode = Typeface.LoadFromAssets("IDC128L.TTF")

And adding it to the page
B4X:
pdf.Canvas.DrawText("Barcode content",iLeft,iTop,Barcode.DEFAULT,iLettersize,Colors.Black,"LEFT" )

But the line stays blanc, no errors. Any thoughts?
 
You can convert text to bitmap with this code (change the typeface): https://www.b4x.com/android/forum/t...awesome-and-material-icons.73646/#post-467852

This will allow you to draw it with the pdf canvas.

Tried it but yes it now shows in the PDF but isn't a barcode font.

I've used the attached to show if the returned code is a barcode bmp but it isn't.

Any thoughts?

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    
    
End Sub

Sub Activity_Resume
    Dim Barcode As Bitmap
    Barcode.InitializeMutable(150dip, 32dip)
    Barcode=TextToBitmap("12345",18)
    Panel1.SetBackgroundImage(Barcode)
    ImageView1.Bitmap=Barcode 
    'Globaal.ResizeImage(LoadBitmap(File.DirInternal,tFoto.sFilename),oImgFoto1.Width,oImgFoto1.Height)
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub TextToBitmap (s As String, FontSize As Float) As Bitmap
    Dim bmp As Bitmap
    bmp.InitializeMutable(150dip, 40dip)
    Dim cvs As Canvas
    cvs.Initialize2(bmp)
    Dim Barcode As Typeface
    Barcode = Typeface.LoadFromAssets("IDC128L.TTF") ' barcode font
    
    Dim h As Double = cvs.MeasureStringHeight(s, Barcode.DEFAULT, FontSize)
    cvs.DrawText(s, bmp.Width / 2, bmp.Height / 2 + h / 2, Barcode.DEFAULT, FontSize, Colors.Black, "CENTER")
    Return bmp
End Sub
 

Attachments

  • testapp.zip
    47.3 KB · Views: 215
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You should switch to B4XPages. You are not handling the complex activity life cycle correctly.

B4X:
Dim h As Double = cvs.MeasureStringHeight(s, Barcode, FontSize)
cvs.DrawText(s, bmp.Width / 2, bmp.Height / 2 + h / 2, Barcode, FontSize, Colors.Black, "CENTER")

1643097340962.png
 
Upvote 0
Erel, you sugggested to switch to B4XPages buyt thats not an option right now. It takes to much time to do so and the customer won't pay for it.

Currently I do get the barcodes on the page but they cann't be scanned. It looks like they are wider than the image but I can't get them shown smaller.
 
Upvote 0
Top