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

DonManfred

Expert
Licensed User
Longtime 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

  • PDFRenderer2V2.0.zip
    2.4 KB · Views: 1,329
Last edited:

DonManfred

Expert
Licensed User
Longtime 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

pdf1.png
pdf2.png
pdf3.png
 

Johan Schoeman

Expert
Licensed User
Longtime User
you are right... i did not know that there was already such a lib...
Like with everything else in life, this is another option that one can choose from to work with. Thanks for this @DonManfred!
 

Johan Schoeman

Expert
Licensed User
Longtime User

Peter Simpson

Expert
Licensed User
Longtime 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 :(
 

shashkiranr

Active Member
Licensed User
Longtime 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
Longtime 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?
 

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)
 

DonManfred

Expert
Licensed User
Longtime User

Juan Marrero

Active Member
Licensed User
Longtime 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

  • Screenshot_2016-09-27-13-35-05[1].png
    Screenshot_2016-09-27-13-35-05[1].png
    248.2 KB · Views: 479
  • Screenshot_2016-09-27-13-35-21[1].png
    Screenshot_2016-09-27-13-35-21[1].png
    96.2 KB · Views: 472

evbedp

Member
Licensed User
Longtime 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