Android Question Setting Typeface to PDFDocument and MemoFiles

Gianni Sassanelli

Active Member
Licensed User
Longtime User
hi,
i'm use PDF library to create PDF from Erel post
https://www.b4x.com/android/forum/threads/printing-and-pdf-creation.76712/#content

I need to use a my TypeFace for TXTMultiLine Memo String in PDF
Is this possible?

Thank's

My code is:

B4X:
Sub MakePDF
    Dim PDF1                     As PDFDocument
    Dim PDF_PAGEINFO      As PageInfo   
    Dim PDF_RECT            As Rect
    Dim ptLeft, ptwidth     As Int
    Dim ptTop, ptHeight    As Int
    Dim Fsize                 As Int
    ptLeft     = 5
    ptTop     = 5
    ptwidth  = 200
    ptHeight  = 50
    Fsize     = 10 / GetDeviceLayoutValues.Scale
    
    PDF_RECT.Initialize(10,10,595-20,842-20) 'bordo di 10
    PDF_PAGEINFO.Initialize(595,842,1,PDF_RECT)
    Dim PDF_Page As Page = PDF1.startPage(PDF_PAGEINFO)
    Dim Txtmemo As String
    Txtmemo = $"
        Questo è un esempio di stringa multi riga
            ${DateTime.Date(DateTime.Now)} ${DateTime.Time(DateTime.Now)}
        ----------------------------------------------
        Riga 1
        Riga 2
        Riga 3
        ==============================================
        Fine"$   
    Dim PDF_TextRec     As TextRec
    PDF_TextRec.Initialize(Txtmemo,ptwidth,822,ptHeight,Colors.DarkGray)
    'I NEED HERE to USE a MY Typeface for TextRec Object
    ' IS this possible??
    
    PDF_TextRec.draw(PDF_Page,0,0)
    PDF1.finishPage(PDF_Page)
    PDF1.writeTo(File.DirRootExternal,"TestPdfDocument.pdf")
End Sub
 

Gianni Sassanelli

Active Member
Licensed User
Longtime User
Sorry Erel,
I have added this code but unsuccessfull
can you help me?

B4X:
   'PDF_TextRec.draw(PDF_Page,0,0)
Dim v As EditText
    v.Initialize("")
    v.SingleLine = False
    v.Wrap = True
    v.Text = $"
    linea 1
    Linea 2
    Linea 3
    "$
    Dim cjo As JavaObject
    cjo = newPDF_Page.Canvas
    Dim jv As JavaObject = v
    jv.RunMethod("draw", Array(cjo.GetFieldJO("canvas")))[

PDF1.finishPage(PDF_Page)
    PDF1.writeTo(File.DirRootExternal,"TestPdfDocument.pdf")

/CODE]
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Different approach with XUI:
SS-2018-08-15_10.09.15.png


Complete code:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
   Dim rp As RuntimePermissions
   rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
   Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
   If Result Then
       Dim pdf As PdfDocument
       pdf.Initialize
       pdf.StartPage(595, 842) 'A4 size
       pdf.Canvas.DrawLine(2, 2, 593 , 840, Colors.Blue, 4)
       pdf.Canvas.DrawText("Hello", 100, 100, Typeface.DEFAULT_BOLD, 30 / GetDeviceLayoutValues.Scale , Colors.Yellow, "CENTER")
       Dim lbl As Label
       lbl.Initialize("")
       lbl.SetLayout(10dip, 10dip, 300dip, 300dip)
       lbl.SingleLine = False
       Dim xlbl As B4XView = lbl
       xlbl.Text = $"Dim r As Rect
r.Initialize(10, 10, 110, 110)
pdf.Canvas.DrawBitmap(xlbl.Snapshot, Null, r)
pdf.FinishPage
Dim out As OutputStream = File.OpenOutput(File.DirRootExternal, "2.pdf", False)
pdf.WriteToStream(out)
out.Close
pdf.Close"$
       Dim r As Rect
       r.Initialize(210, 210, 510, 510)
       pdf.Canvas.DrawBitmap(xlbl.Snapshot, Null, r)
       pdf.FinishPage
       Dim out As OutputStream = File.OpenOutput(File.DirRootExternal, "2.pdf", False)
       pdf.WriteToStream(out)
       out.Close
       pdf.Close
   End If
   
End Sub
 
Upvote 0

Gianni Sassanelli

Active Member
Licensed User
Longtime User
Thank's Erel, it works fine.
I wish I had your skills,
but only for study and personal culture, could you explain me how to use the canvas?
For example how to place them in my preferred position.

In Italy today we celebrate "Ferragosto", for us it is a public holiday, although I am at work.
:)
 
Upvote 0
Top