Android Question Draw panel views programatically on PDF Canvas

Discussion in 'Android Questions' started by mcvburen, Jul 3, 2018.

  1. mcvburen

    mcvburen Member Licensed User


    I have seen examples in the forum of taking screen shots of panels and saving them to a canvas and bitmap image. My trouble is I am trying to draw views to a PDF canvas (or any canvas) programmatically with no views displayed on a layout at the time. I was able to solve this in B4i using Canvas.DrawView as shown below in a sampling of B4i code. Note I do not want to use the canvas.drawtext method since I need column Labels for alignment, style etc..., that are much easier to work with than raw text alone.

    Has anyone been able to draw views on a canvas programmatically without the views being visible at the time? I tried Erel's example sub 'DrawView' but with no luck (blank screen) because I think the view must be showing at the time it is placed on the canvas but I can't be sure. I may be missing a step. Any help in pointing me in the right direction would be greatly appreciated.

    ** Below is B4i pseudocode, not B4A snippet

    '**** B4I IOS Snippet ****
    Private Sub CreatePDFPage
    End Sub

    Private Sub CreateMotherPanel
      MotherPanel.Width = PDFWidth
      MotherPanel.Height= PDFLength
    End Sub

    'Short Example showing column labels added to panels, panels being added to page as rows
    Private Sub AddRowsToMother
    Dim Col1,Col2,Col3,Col4  as Label
    Dim PanelRow1,PanelRow2,PanelRow3,PanelRow4 as Panel
      Col1.text = 
      Col2.text = 

      PanelRow1.AddView(Col1, …
      PanelRow1.AddView(Col2, …

      MotherPanel.AddView(panelRow1, …
      MotherPanel.AddView(panelRow2, ...

    End Sub

    Private Sub SaveMotherToPDFCanvas

    'Copy all views (row panels) to the PDF canvas
      Dim PDFCanvas as Canvas

    For Each v As View In MotherPanel.GetAllViewsRecursive
    If v Is Panel Then
    Dim Rect As Rect
    Dim pRow As Panel
    Rect.Initialize(pRow.Left, pRow.Top, pRow.Left+pRow.Width, pRow.Top+pRow.Height)
    End If


    End Sub
    Pardon if any misspellings

    Last edited: Jul 3, 2018
  2. Erel

    Erel Administrator Staff Member Licensed User

    The simplest way to get a bitmap from a view is with XUI library:
    Dim x As B4XView = YourPanel
    Dim bmp As Bitmp = x.Snapshot
    It does work with off screen views as demonstrated with this code:
    Dim p As B4XView = xui.CreatePanel("")
    p.Color = 
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice