Android Example PDFab - a default PDF viewer app based on DonManfred's PDFium wrap

I wanted a freeware pdf viewer without ads, tracking, data collection, and what have you. Besides, it wasn't obvious (to me at least) how to make an app that would be offered as default to open a certain type of file, especially a PDF. Time for some R&D fun, and here you go.

Prerequisites: download these files provided by @DonManfred (thank you very much for those):
- from https://www.b4x.com/android/forum/threads/pdfium-pdfview2.102756/ : click the big red link that says 'Download HERE', download the zip (I fetched PDFiumV1.04.zip) and extract its files to your B4A Additional Libraries folder
- from https://www.b4x.com/android/forum/threads/android-pdf-viewer-aar-missing.128982/#post-825649 : download the aar file to your B4A Additional Libraries folder

The icon was generated with https://icon.kitchen/ (thank you @Mashiane for your post, that's a brilliant site). You'll find it in the attached 'icon.zip' file; put folder 'icon' inside the project folder.

If you'd copy the code, make sure you
- have a look at the Manifest too as it contains information without which this won't work
- add the '#AdditionalRes' (for the icon) and '#AdditionalJar' lines you'll find in the Main module (and probably some other code too :))
- take into account the PFDium license:
Copyright 2017 Bartosz Schiller

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Tested on a Xiaomi Redmi Note 10, MIUI 14.0.5, Android 12.

Enjoy!
 

Attachments

  • icon.zip
    92.1 KB · Views: 725
  • PDFab.zip
    10.2 KB · Views: 802

Mashiane

Expert
Licensed User
Longtime User
Hi, im trying this example and it just starts and closes. Any ideas?
Removed all the intent based and runtime permission stuff as I needed it to work with a file from DirAssets.

Works Perfectly, thanks for this!

B4X:
' Based on DonManfred's PDFium example app at https://www.b4x.com/android/forum/threads/pdfium-pdfview2.102756
#Region  Project Attributes
    #ApplicationLabel: PDFab
    #VersionCode: 1
    #VersionName: 
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
    #BridgeLogger: True
    ' This next line is for the Adaptive Icon that was created with https://icon.kitchen (see https://www.b4x.com/android/forum/threads/adaptive-icons-simple-instructions-and-tips.123843/)
    #AdditionalRes: ../icon
#End Region

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

#AdditionalJar: android-pdf-viewer.aar
#AdditionalJar: com.android.support:support-v4
#AdditionalJar: Pdfium.aar

Sub Process_Globals
    Private xui As XUI
End Sub

Sub Globals
    Dim pdf As PdfiumCore
    Private PDFView1 As PDFView
    Private lblPages As Label
    Private glPages As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout1")
    pdf.Initialize("PDFium")
    '
    File.Copy(File.DirAssets, "chapter_5.pdf", xui.DefaultFolder, "chapter_5.pdf")
    Dim cfg As Configurator = PDFView1.fromFile(xui.DefaultFolder, "chapter_5.pdf")
    cfg.SetEventname("PDFium")
    cfg.autoSpacing(True)
    cfg.enableSwipe(True)
    cfg.pageSnap(True)
    cfg.swipeHorizontal(False)
    cfg.addOnErrorListener
    cfg.addOnLoadCompleteListener
    cfg.addOnPageChangeListener
    cfg.addOnPageErrorListener
    cfg.addOnTapListener
    cfg.load
End Sub

Sub Activity_Resume
End Sub

Sub PDFium_OnTap(o As Object)
    Log("PDFium_OnTap")
End Sub

#Region Unchanged (almost) code from DonManfred's example app at https://www.b4x.com/android/forum/threads/pdfium-pdfview2.102756
Sub PDFium_loadComplete(pages As Int)
    Log($"PDFium_loadComplete(${pages})"$)
    glPages = pages
    lblPages.Text = $"${glPages}"$
End Sub

Sub PDFium_onInitiallyRendered(page As Int)
    Log($"PDFium_onInitiallyRendered(${page})"$)
End Sub

Sub PDFium_onPageChanged(page As Int, TotalPages As Int)
    Log($"PDFium_onPageChanged(${page},${TotalPages})"$)
    lblPages.Text = $"${page + 1}/${glPages}"$ ' Changed {page-1} to {page + 1}
End Sub

Sub PDFium_PageNum(page As Int)
    Log($"PDFium_PageNum(${page})"$)
End Sub

Sub PDFium_Show()
    Log($"PDFium_Show()"$)
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub btnFirst_Click
    PDFView1.jumpTo2(0,False)    
End Sub

Sub btnPrev_Click
    PDFView1.jumpTo2(PDFView1.CurrentPage-1,False)
End Sub

Sub lblPages_Click
End Sub

Sub btnNext_Click
    PDFView1.jumpTo2(PDFView1.CurrentPage+1,False)
End Sub

Sub btnLast_Click
    PDFView1.jumpTo2(glPages-1,False)
End Sub
#End Region ' Unchanged (almost) code from DonManfred's example app at https://www.b4x.com/android/forum/threads/pdfium-pdfview2.102756
 

Mashiane

Expert
Licensed User
Longtime User
16KB PDF Libraries available here...

 
Top