Android Question Better approach in creating a pdf from a clv content.

ProjectGroup19

Active Member
Licensed User
Greetings Community,

I'm trying to create a pdf file from a clv content. I have followed guides on this forum and arrived with the attached but i still need help because the image is squeezed when the content is alot.

In my approach, i save the clv content as an bitmap and insert it in the pdf document. I would be glad if there is a better approach to be shared.

B4X:
Private Sub btnSavePDF_Click
  
    rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
    Wait For activity_permissionresult(permission As String,res As Boolean)
    If res=True Then
      
      
       Dim cs As CSBuilder
        cs.Initialize.Underline.Append("'"&lblElection.text.ToUpperCase &"' ELECTION RESULT").pop
        Dim bmpElection As B4XBitmap = TextToBitmap(400dip, xui.CreateDefaultBoldFont(20), xui.Color_RGB(56,96,178), cs)
     
      
        Dim x2 As B4XView = Panel2
        Dim b2 As Bitmap = x2.Snapshot
        Sleep(1)
        Dim x As B4XView = CustomListView1.sv.ScrollViewInnerPanel
        Dim b As Bitmap = x.Snapshot
      
      
        Dim pdf As PdfDocument
        pdf.Initialize
      
        Dim DestRectResultLogo As Rect
        Dim DestRectResultInstitution As Rect
        Dim DestRectElection As Rect
        Dim DestRectResult As Rect
        Dim DestRectResultStatistic As Rect
      
        DestRectResultLogo.Initialize(10dip, 10dip, 40dip, 40dip)
      
        DestRectResultInstitution.Initialize(45dip, 20dip, 200dip, 40dip)
        DestRectResultInstitution.Width=bmpInstitution.Width / bmpInstitution.Scale
        DestRectResultInstitution.Height=bmpInstitution.Height / bmpInstitution.Scale
      

      
        DestRectElection.Initialize(70dip,45dip,280dip,60dip)
        DestRectElection.Width=bmpElection.Width/bmpElection.Scale
        DestRectElection.Height=bmpElection.Height/bmpElection.Scale
      
        DestRectResultStatistic.Initialize(10dip, 70dip, 280dip, 110dip)
        DestRectResult.Initialize(10dip, 120dip, 280dip, 390dip)
      
     
  
        pdf.StartPage(595, 842) 'A4 size
        pdf.Canvas.DrawBitmap(B4XImageView1.Bitmap,Null,DestRectResultLogo)
        pdf.Canvas.DrawBitmap(bmpInstitution,Null,DestRectResultInstitution)
         pdf.Canvas.DrawBitmap(bmpElection,Null,DestRectElection)
       pdf.Canvas.DrawBitmap(b2,Null,DestRectResultStatistic)
        pdf.Canvas.DrawBitmap(b,Null,DestRectResult)
        
        pdf.FinishPage
        Dim out As OutputStream
        out = File.OpenOutput(File.Combine(File.DirRootExternal,"Download"), lblElection.Text&" Result.pdf", False)
     
        'Dim out As OutputStream = File.OpenOutput(File.DirInternal, "1.pdf", False)
        pdf.WriteToStream(out)
        out.Close
        pdf.Close
      
  
     
        Dim Answ As Object
        Dim Txt As String
        Txt = $"File Successfully Saved.${CRLF}${CRLF}Do you want to open it now?"$
        Answ = Msgbox2Async(Txt, "A T T E N T I O N", "Yes", "", "No",  Null,False)    'LoadBitmap(File.DirAssets,"SVoteMain.png"), False) ' MessageBox
        Wait For (Answ) msgbox_result(resul As Int)
        If resul = DialogResponse.POSITIVE Then
            rp.CheckAndRequest(rp.PERMISSION_READ_EXTERNAL_STORAGE)
            Wait For activity_permissionresult(permission As String,res1 As Boolean)
            If res1=True Then
                Intent_OpenFile(File.Combine(File.DirRootExternal,"Download"),lblElection.Text&" Result.pdf")
            Else
  
                ToastMessageShow("Storage permission not granted",True)
            End If
        Else
            ToastMessageShow($"You can find saved file in your downloads folder as "${lblElection.Text} Result.pdf"$,True)
      
        End If
      
  
   Else
  
        ToastMessageShow("Storage permission not granted",True)
    End If
  
  
  
  
 
End Sub

Sub TextToBitmap (Width As Int, Fnt As B4XFont, Clr As Int, Text As Object) 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
 

Attachments

  • Screenshot_20211230-222548_1.jpg
    Screenshot_20211230-222548_1.jpg
    229.7 KB · Views: 188

Mahares

Expert
Licensed User
Longtime User
I'm I not giving much information?
I have not worked with the pdf library to be of much help, but let me tell you perhaps why you are not getting answers:
1. You deal with File.DirRootExternal, which has become a no no lately, although several are still using it.
2. You posted a lot of code, which is hard to follow. Perhaps, your best bet is to include a small project that illustrates the issue.
3. Expain the problem better instead of simply saying the image is squeezed.
4. Have you thought about the fact that maybe your approach is the best and no one has anything to offere.
5. When you post a thread, try not to bump other threads by asking members to respond. The more you push people to answer, the less reluctant people become to respond.
 
Upvote 0

ProjectGroup19

Active Member
Licensed User
I have not worked with the pdf library to be of much help, but let me tell you perhaps why you are not getting answers:
1. You deal with File.DirRootExternal, which has become a no no lately, although several are still using it.
2. You posted a lot of code, which is hard to follow. Perhaps, your best bet is to include a small project that illustrates the issue.
3. Expain the problem better instead of simply saying the image is squeezed.
4. Have you thought about the fact that maybe your approach is the best and no one has anything to offere.
5. When you post a thread, try not to bump other threads by asking members to respond. The more you push people to answer, the less reluctant people become to respond.
Well noted with thanks @Mahares
 
Upvote 0
Top