Android Question how to display pdf format

Makumbi

Well-Known Member
Licensed User
Please help below is some code i was trying out to show PDF document in my Layout

B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private rp As RuntimePermissions
    Private xui As XUI

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    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
        Dim bmp As B4XBitmap = TextToBitmap(200dip, xui.CreateDefaultBoldFont(20), xui.Color_Black, _
        $"s dflkj sdfkls
dffwje klfj wlkef jwlke fjlwkef jklwe jflkwe jflkwe jflkwe jflkwe jfl
sdf
we
 gfkwe lg;kw e;lg wklelg; weg wel;g"$)
        Dim rect As Rect
        rect.Initialize(30, 30, 0, 0)
        rect.Width = bmp.Width / bmp.Scale
        rect.Height = bmp.Height / bmp.Scale
        pdf.Canvas.DrawBitmap(bmp, Null, rect)
        pdf.FinishPage
        Dim out As OutputStream = File.OpenOutput(File.DirRootExternal, "sula.pdf", False)
        pdf.WriteToStream(out)
        out.Close
        pdf.Close
    End If
End Sub

Sub Activity_Resume

End Sub
Sub TextToBitmap (Width As Int, Fnt As B4XFont, Clr As Int, Text As String) As B4XBitmap
    Dim lbl As Label
    lbl.Initialize("")
    Dim x As B4XView = lbl
    x.Font = Fnt
    x.TextColor = Clr
    x.SetLayoutAnimated(0, 0, 0, Width, 0)
    Dim su As StringUtils
    x.Height = su.MeasureMultilineTextHeight(x, Text)
    x.Text = Text
    Return x.Snapshot
End Sub
Sub Activity_Pause (UserClosed As Boolean)

End Sub
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0
Top