Android Question to Print a Panel!

MroBurk

Member
Licensed User
Hi There! I'm new in B4A. I need to print a Panel, I want to trasform it into PDF, but I read that is really difficult to do. So I would like to try to make a screenshoot or turn it (Panel) in .bmp and then save it for printing. I found this code-line but if I copy this in to button_click does not work. Can you help me please!!! (I'm sorry for my bad English)

B4X:
Dim v As B4XView = Panel1
    Dim bmp As Bitmap = v.Snapshot 'use this as you want

    'this is to store the snapshot as a file
    Dim Out As OutputStream
    Out = File.OpenOutput(File.DirInternal, "snapshot.png", False)
    bmp.WriteToStream(Out, 100, "PNG")
    Out.Close
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Why difficult???

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private provider As FileProvider
End Sub

Public Sub Initialize
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    provider.Initialize
End Sub

Private Sub PanelToPdf(Panel As B4XView, FileName As String)
    Dim pdf As PdfDocument
    pdf.Initialize
    pdf.StartPage(595, 842) 'A4 size
    Dim bmp As B4XBitmap = Panel.Snapshot
    Dim dest As Rect
    dest.Initialize(10, 10, 0, 0)
    dest.Width = bmp.Width / bmp.Scale
    dest.Height = bmp.Height / bmp.Scale
    pdf.Canvas.DrawBitmap(bmp, Null, dest)
    pdf.FinishPage
    Dim out As OutputStream = File.OpenOutput(provider.SharedFolder, FileName, False)
    pdf.WriteToStream(out)
    out.Close
    pdf.Close
    Dim in As Intent
    in.Initialize(in.ACTION_VIEW, "")
    provider.SetFileUriAsIntentData(in, FileName)
    in.SetType("application/pdf")
    Try
        StartActivity(in)
    Catch
        Log("PDF viewer is not installed.")
        Log(LastException)
    End Try
End Sub

Private Sub Button1_Click
    PanelToPdf(Root, "1.pdf")
End Sub

Don't miss FileProvider manifest code.
More information about FileProvider: https://www.b4x.com/android/forum/threads/class-fileprovider-share-files.97865/#content
 

Attachments

  • Project.zip
    14 KB · Views: 186
Upvote 3

MroBurk

Member
Licensed User
I'm so sorry Erel can you help me! Why doesn't It work? I only added some label and edittext to your project...?
 

Attachments

  • Parenterale12.zip
    13 KB · Views: 170
Upvote 0
Top