pdfRenderer v0.10 (beta) - Create your own PDF viewer

DonManfred

Expert
Licensed User
pdfRenderer
Author: @DonManfred
Version:
2.0

With this library you can open a PDF-File and then you can get page x from this PDF as Bitmap.

The Library is a wrapper for the new SDK Api https://developer.android.com/reference/android/graphics/pdf/PdfRenderer.html

Exampleproject here: http://basic4android.de/PdfRenderer2Ex.zip

PDFRenderer2
  • PDFRenderer
    • Functions:
      • close
        Close the PDF
      • closePage
      • Initialize (EventName As String, path As String, filename As String)
      • openpage (index As Int)
        Open the page with the given index.
        You now can access PageHeight and Width, Render the Page...
      • renderPageforDisplay (pageNo As Int) As android.graphics.Bitmap
      • renderPageforPrint (pageNo As Int) As android.graphics.Bitmap
    • Properties:
      • PageCount As Int [read only]
        Return the PageCount from the PDF
      • PageHeigth As Int [read only]
      • PageIndex As Int [read only]
      • PageWidth As Int [read only]
      • shouldScaleForPrinting As Boolean [read only]
 

Attachments

Last edited:

DonManfred

Expert
Licensed User
Example 2

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim pdfr As PdfRenderer
    Dim pdfPages As Int
    Dim activepage As Int
    Private btnPrev As Button
    Private btnNext As Button
    Private lblPage As Label
    Private img As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("main")
    File.Copy(File.DirAssets,"modbau14.pdf",File.DirRootExternal,"modbau14.pdf")
End Sub

Sub Activity_Resume
    pdfr.Initialize("pdf",File.DirRootExternal,"modbau14.pdf", File.DirRootExternal&"/PDFs")
    pdfPages = pdfr.PageCount
    activepage = 0
    pdfr.renderPage(activepage)
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub pdf_pagerendered (page As Int, pagecount As Int, image As Object)
    activepage = page
    If activepage = pagecount-1 Then
        btnNext.Enabled = False
    Else
        btnNext.Enabled = True
    End If
    If activepage = 0 Then
        btnPrev.Enabled = False
    Else
        btnPrev.Enabled = True
    End If
    lblPage.Text = (page+1)&"/"&pagecount
    Log("b4a pagerendered ("&page&","&pagecount&")")
    img.Bitmap = image
End Sub
Sub btnPrev_Click
    If activepage > 0 Then
        pdfr.renderPage(activepage-1)
    End If
End Sub
Sub btnNext_Click
    If activepage < pdfPages Then
        pdfr.renderPage(activepage+1)
    End If
End Sub
 

Peter Simpson

Expert
Licensed User
I'm gutted @DonManfred.
This would be the perfect solution for my new business app, but obviously I'll have customers that are not running on Android 5.0 Lollipop.

I've just looked at your php script solution, but I'm looking for an all in one built in solution :(
 

DonManfred

Expert
Licensed User
Im sorry Peter... Yes it only works in lollipop. If You find something similar for older Android versions (javasource) i can look whether im able to combine both!?
 

shashkiranr

Active Member
Licensed User
Hi @DonManfred ,

I have a question but it might sound stupid. Is it possible to copy all the pdf classes in andriod and write it as a seperate library and use it for previous versions? . The Andriod code is open source right? so we can copy the entire code and use it in the previous versions?

Regards,
SK
 

DonManfred

Expert
Licensed User
Probably no. As Android has changed in the versions you cannot use code for newer versions on old ones. You need to rewrite part of android to make them useable.

Most probably you will not work...

BUT: This is NOT related to this library. You should start a new thread for that
 

Baris Karadeniz

Active Member
Licensed User
DonManfred, please forgive me for my simple question, I downloaded library and simple example. I can't see pdf file when I run the example. What kind of view should I add to the layout?
 

DonManfred

Expert
Licensed User
The renderer wil deliver Bitmap. See examplecode. It is using a imageview and show the bitmap of a page each time you change the page...
Note that this lib will only work on Android 5+
It will not work on older devices.
 

Baris Karadeniz

Active Member
Licensed User
I added imageview to the layout as you said. When I try, I receice an error log given below. What can be the reason?

Installing file.
PackageAdded: package:b4a.example
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
java.lang.NoClassDefFoundError: android.graphics.pdf.PdfRenderer
at de.donmanfred.pdfRenderer.Initialize(pdfRenderer.java:77)
at b4a.example.main._activity_resume(main.java:380)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:702)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:336)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at b4a.example.main.afterFirstLayout(main.java:108)
at b4a.example.main.access$000(main.java:17)
at b4a.example.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
 

Juan Marrero

Active Member
Licensed User
Hi DonManfred. Just curious. Does all PDFs files open correctly? My issue is that all PDFs created by MuseScore 2 open dark. Every other PDFs open bright as is supposed to. I created an app to manage music scores (all apps that i found on the play store either don't do what i need or are not free) but all pdf scores created by MuseScore 2 (the software to create music scores in Windows) open very dark. They open, but not bright. Attached the screenshots. The dark score was made with MuseScore 2.

Just want to know if someone has experienced this.
 

Attachments

evbedp

Member
Licensed User
need help, this libary very nice. thanks

can i get best quality PNG?
because after pdf convert to PNG. text on the PNG becoming less clear.

Please help me

this is Original PDF
Screenshot_2017-01-07-11-15-33.jpg


After to PNG

Screenshot_2017-01-07-11-14-45.jpg
 
Top