iOS Question PDF viewing problems

toro1950

Active Member
Licensed User
Greetings to the whole community, in a baxpages app for viewing a PDF, downloaded from one of my sites, I initially used the PDFView class with the following code inserted into a page
B4X:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
    Private Label1 As Label
    Private PDFView1 As PDFView
    Private hd As HUD
End Sub


Public Sub Initialize As Object
    Return Me
End Sub


Private Sub B4XPage_Created (Root1 As B4XView)
    PDFView1.IsInitialized
    Root = Root1
    'load the layout to Root
    Root.LoadLayout("page2layout")
    B4XPages.SetTitle(Me,"My title")
    If File.Exists(File.DirDocuments, "namefile.pdf") Then
        PDFView1.LoadPDF(File.DirDocuments, "namefile.pdf", "Password")
    Else
        hd.ToastMessageShow("File  non trovato",False)
        End If
End Sub
It seemed to work perfectly, but when downloading a new PDF, after removing the existing one, it always displayed the first one, if I closed the app and reopened it then it displayed the downloaded one.
So I tried to view it via a WebView as Erel advised me, using the following code.
B4X:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
    Private Label1 As Label
    Private hd As HUD
    Private WebView1 As WebView
End Sub


Public Sub Initialize As Object
    Return Me
End Sub


Private Sub B4XPage_Created (Root1 As B4XView)

    Root = Root1
    'load the layout to Root
    Root.LoadLayout("page2layout")
    B4XPages.SetTitle(Me,"My title")
    WebView1.RequestFocus
    If File.Exists(File.DirDocuments, "namefile.pdf") Then
    WebView1.LoadUrl(xui.FileUri(File.DirDocuments, "namefile.pdf"))
    
    Else
        hd.ToastMessageShow("File  non trovato",False)
        End If
End Sub
In this case another problem arises, once viewed and returned to the BaxMainpage via Back, if I try to view it again I resume the page with WebView the PDF cannot be seen but the page numbers of the same are displayed, it is as if a command is missing refresh.
How to solve? I would prefer with WebView, simpler. I apologize for my bad English
Mario
 

toro1950

Active Member
Licensed User
Hi Erel, the code on the page containing the WebView posted is complete, there is nothing else. On the BaxMainPage page there is the code to download the pdf file and a button for viewing with the simple code
B4X:
Sub Button3_Click
    B4XPages.ShowPage("page2")
End Sub
Once the PDF has been viewed, returning to the BaxMainPage page with the Back , clicking button3 again displays only the number of pages and each scrolling page. If I exit the app and reopen the PDF it displays correctly
 
Upvote 0

toro1950

Active Member
Licensed User
Hi Erel, while carrying out some tests I noticed that once the page with the code was created
Private Sub B4XPage_Created (Root1 As B4XView)
at the first view, in subsequent views finding it already open the code inside it is no longer executed, so I transferred the code to a sub

B4X:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
    Private Label1 As Label
    Private hd As HUD
    Private WebView1 As WebView
    Private button1 As Button
End Sub


Public Sub Initialize As Object
    Return Me
End Sub


Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    'load the layout to Root
    Root.LoadLayout("page2layout")
    WebView1.Initialize("WebView1")
    Root.AddView(WebView1, 0, 30, 100%x, 100%y)
    B4XPages.SetTitle(Me,"My Title")
    visualizza
End Sub
Sub Button1_Click
    visualizza
End Sub
Sub visualizza
        
    If File.Exists(File.DirDocuments, "namefile.pdf") Then
        
        WebView1.LoadUrl(xui.FileUri(File.DirDocuments, "Namefile.pdf"))
        
    Else
        hd.ToastMessageShow("File  non trovato",False)
    End If
    
End Sub
I added a button to run the sub in subsequent views, and that's how it works.
to solve how to close the page when going back with Back?
or is there a way to activate the sub every time the page is viewed?
Now you have more data to evaluate everything, if you still need the little project I will prepare it
 
Upvote 0

toro1950

Active Member
Licensed User
Hi Erel, I don't understand why deleting line #20 like this doesn't work anymore as line #21. In the attachment I have included a small project that reproduces the error that occurs because the page2 is opened only once. Is there a way to close page2 when returning to the Mainpage? or start the showpdf sub from the mainpage after viewing page2? I don't know if the attached file works, alternatively you can download the full version at the following link https://www.realdieta.it/public/App/Testpdf.zip
 

Attachments

  • Testpdf.zip
    209.3 KB · Views: 168
Upvote 0

toro1950

Active Member
Licensed User
Hi Erel, thank you for your interest, you pointed out some errors, being a novice I can't understand how to obtain the same result differently, can you give me an example?. but you never answered my question, perhaps with my bad English I wasn't able to make myself understood, in any case I solved it myself, I used B4XPage_Appear
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private hd As HUD
    Private WebView1 As WebView
End Sub


Public Sub Initialize As Object
        Return Me
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("page2layout")

End Sub
private Sub B4XPage_Appear
    visualizza
End Sub
Sub visualizza
    WebView1.Initialize("WebView1")
    Root.AddView(WebView1, 0, 35, 100%x, 100%y)
    ' if I delete line 17 line 18 doesn't work
        B4XPages.SetTitle(Me,"Piano Nutrizionale")
    If File.Exists(File.DirDocuments,"Piano Nutrizionale.pdf") Then
    WebView1.LoadUrl(xui.FileUri(File.DirDocuments,"Piano Nutrizionale.pdf"))
    Else
        hd.ToastMessageShow("File  non trovato",False)
    End If
   
End Sub
 
Upvote 0

toro1950

Active Member
Licensed User
Hi Erel, I apologize for my English, but I wanted to tell you that I have read your answers carefully, in fact several times, but I never found the answer to my question, your answers concerned the error on the webview, for convenience I'll give you your answers
1) Please try to reproduce it in a small project and upload it.
after having explained the problem better to you
2) Pages are always created once. Quickly delete line #20 and add the WebView with the designer. Your current code is wrong.
3) Using %x / %y outside of the designer script or Page_Resize is a mistake. It was always like this.
Unfortunately I still haven't understood why it's wrong, given that it works
4) It doesn't look like you are reading my answers so I will stop here. Sorry.
why this answer, I don't understand.
I apologize if I did something wrong about you but I don't understand what
Best regards
Mario
 
Upvote 0
Top