Android Question [B4A] Save PDF as image

vecino

Well-Known Member
Licensed User
Longtime User
Hello, is there any library or class that allows to save a PDF file as a JPG or PNG image?
Thank you.
 
Upvote 0
Thanks for the information and links.
I don't know if it is outdated, but I find it impossible to even run the example :confused:
 
Upvote 0
PDFium works great.

Here is an example with PDFium.
[CODE lang="b4x" title="B4X"]#Region Project Attributes
#ApplicationLabel: B4A Example
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#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
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.

End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim pdf As PdfiumCore
Private PDFView1 As PDFView
Private btnFirst As Button
Private btnPrev As Button
Private lblPages As Label
Private btnNext As Button
Private btnLast As Button
Private glPages As Int
Private imgPDF 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("Layout1")

pdf.Initialize("PDFium")
File.Copy(File.DirAssets,"sample.pdf",File.DirInternal,"sample.pdf")
Dim cfg As Configurator = PDFView1.fromUri(File.DirInternal,"sample.pdf")
cfg.SetEventname("PDFium")
cfg.autoSpacing(True).enableSwipe(True).pageSnap(True).swipeHorizontal(False).addOnErrorListener.addOnLoadCompleteListener.addOnPageChangeListener.addOnPageErrorListener.addOnTapListener.load

'***** FOR RENDERING ************************
RenderPDFThumbnail("sample.pdf",3) '3 = page 3
'************************************************
End Sub

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}"$

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

Sub Activity_Resume

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

'***** FOR RENDERING *************************************************************************************
Sub RenderPDFThumbnail(TheFilename As String, page As Int)
Dim NewBitmap As Bitmap
Dim bmp As Bitmap
Dim glDoc As Object
glDoc = pdf.newDocument(File.DirInternal,TheFilename, 268435456,"")
bmp.InitializeMutable(600dip,600dip)
pdf.openPage(glDoc,page)

pdf.renderPageBitmap(glDoc,bmp,page,0,0,600dip,600dip)

Dim out As OutputStream = File.OpenOutput(File.DirInternal, "Out.jpg", False)
bmp.WriteToStream(out, 80, "JPEG")
out.Close
NewBitmap = LoadBitmap(File.DirInternal, "Out.jpg")
imgPDF.Bitmap = NewBitmap
End Sub[/CODE]

Tips: Add your pdf in assets folder and chek additional jar
 

Attachments

Last edited:
Upvote 0
Something is not right:

Logger connected to: LENOVO Lenovo TB125FU
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
** Activity (main) Resume **
onError()
java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/util/ArrayMap;
at com.shockwave.pdfium.PdfDocument.<init>(PdfDocument.java:109)
at com.shockwave.pdfium.PdfiumCore.newDocument(PdfiumCore.java:132)
at com.github.barteksc.pdfviewer.source.UriSource.createDocument(UriSource.java:38)
at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:53)
at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:27)
at android.os.AsyncTask$3.call(AsyncTask.java:394)
at java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)
at java.lang.Thread.run(Thread.java:1012)
Caused by: java.lang.ClassNotFoundException: android.support.v4.util.ArrayMap
... 10 more
** Activity (main) Pause, UserClosed = false **
** Service (starter) Destroy (ignored)**
 
Upvote 0
The same

onError()
java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/util/ArrayMap;
at com.shockwave.pdfium.PdfDocument.<init>(PdfDocument.java:109)
at com.shockwave.pdfium.PdfiumCore.newDocument(PdfiumCore.java:132)
at com.github.barteksc.pdfviewer.source.UriSource.createDocument(UriSource.java:38)
at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:49)
at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:25)
at android.os.AsyncTask$3.call(AsyncTask.java:394)
at java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)
at java.lang.Thread.run(Thread.java:1012)
Caused by: java.lang.ClassNotFoundException: android.support.v4.util.ArrayMap
... 10 more
 
Upvote 0
I don't have a solution to this problem.
I installed everything according to the procedures and everything works.
I just added a code.
I suggest you open a new tread for this problem.
 
Upvote 0
It may be a problem with my tablet. I will try another one.
Thank you very much for your help.
 
  • Like
Reactions: zed
Upvote 0
I don't have a solution to this problem.
I installed everything according to the procedures and everything works.
I just added a code.
I suggest you open a new tread for this problem.

It is already solved, the problem was in the pdf file I was using, it would be damaged. I used another one and it worked perfectly.
Thank you very much.
 
  • Like
Reactions: zed
Upvote 0
Back
Top