Android Question Convert a Pdf page into Bitmap

Sergey_New

Well-Known Member
Licensed User
Longtime User
Is there such a solution in B4A?
I found a similar solution on stackoverflow, but my knowledge is not enough to create B4A code.
 
Solution
Take a look

pdfToBMP(Path&"/File.pdf")

It better like this or use the inside print Method the best way

Printer.PrintDiskImagefileBA(pdfToBMP(Path &"/Anula.pdf"))

or

prn3.prnBMP(pdfToBMP(Path&"/Anula.pdf"))



pdf to bmp:
Public  Sub pdfToBMP(pdf_path As String) As Bitmap 'ignore
 
    Try
        Dim parcel As JavaObject
        parcel.InitializeStatic("android.os.ParcelFileDescriptor")
           
        Dim f As JavaObject
        f.InitializeNewInstance("java.io.File",Array As String(pdf_path))
           
        Dim p As Object = parcel.RunMethod("open",Array(f, 268435456))
           
        Dim pdfRenderer As JavaObject
        pdfRenderer.InitializeNewInstance("android.graphics.pdf.PdfRenderer",Array(p))...

Sergey_New

Well-Known Member
Licensed User
Longtime User
peacemaker, thank you!
But this is too complicated for me.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
It's a wrong way to try to become a programmer/developer if you are not prepared to learn new and complicated subjects. I know almost nothing about Java, C# and C++, but by collecting information drop-by-drop, I'm sure that it's possible to understand many complicated things. (I did it with some subjects.) But you just need to be motivated to keep going.
 
Upvote 0

Lucas Siqueira

Active Member
Licensed User
Longtime User




 
Upvote 0

Lucas Siqueira

Active Member
Licensed User
Longtime User
You can use a tool I created, it reads all the PDFs in a folder and converts each page into an image


1742585169038.png
 
Upvote 0

Lucas Siqueira

Active Member
Licensed User
Longtime User
Thanks, but I need B4A.
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User

Tried to use pdfRenderer. Unfortunately, I couldn't find any examples. I installed the PDFRenderer2 library, but even the declaration
B4X:
Dim pdfr As PdfRenderer
gives an error.
 
Upvote 0

Situ LLC

Active Member
Licensed User
Longtime User
Take a look

pdfToBMP(Path&"/File.pdf")

It better like this or use the inside print Method the best way

Printer.PrintDiskImagefileBA(pdfToBMP(Path &"/Anula.pdf"))

or

prn3.prnBMP(pdfToBMP(Path&"/Anula.pdf"))



pdf to bmp:
Public  Sub pdfToBMP(pdf_path As String) As Bitmap 'ignore
 
    Try
        Dim parcel As JavaObject
        parcel.InitializeStatic("android.os.ParcelFileDescriptor")
           
        Dim f As JavaObject
        f.InitializeNewInstance("java.io.File",Array As String(pdf_path))
           
        Dim p As Object = parcel.RunMethod("open",Array(f, 268435456))
           
        Dim pdfRenderer As JavaObject
        pdfRenderer.InitializeNewInstance("android.graphics.pdf.PdfRenderer",Array(p))
           
        Dim page As JavaObject = pdfRenderer.RunMethod("openPage",Array(0))
           
        Dim bmp As Bitmap
        bmp.InitializeMutable(page.RunMethod("getWidth",Null),page.RunMethod("getHeight",Null))


        Dim cv As Canvas
        cv.Initialize2(bmp)
        Dim Rect1 As Rect
        Rect1.Initialize(0, 0, page.RunMethod("getWidth",Null), page.RunMethod("getHeight",Null))
        cv.DrawRect(Rect1, Colors.white, True,5dip)
        page.RunMethod("render",Array(bmp, Null, Null,2))
        page.RunMethod("close",Null)
        pdfRenderer.RunMethod("close",Null)
       [B] Dim Out As OutputStream
        Out = File.OpenOutput(Path , "tiquete.png", False)[/B]
        bmp.WriteToStream(Out, 100, "PNG")
        Out.Close
        Return bmp
    Catch

     '  B4XPages.MainPage.Herramienta.Print_erro("Error en impresion"  ,"AVISE ESTE ERROR","PDF-E-0012")
         
       
    End Try

End Sub
'------------------------------------------------

This lineas
Dim Out As OutputStream
Out = File.OpenOutput(Path , "tiquete.png", False)
bmp.WriteToStream(Out, 100, "PNG")

This wat you create a . bmp, png

salud
 
Last edited:
Upvote 0
Solution

Sergey_New

Well-Known Member
Licensed User
Longtime User
Situ LLC, thank you very much!
Simple and beautiful.
Changed your code a bit for my needs:
B4X:
Public  Sub pdfToBMP (pdf_path As String) As Bitmap 'ignore
    Try
        Dim parcel As JavaObject
        parcel.InitializeStatic("android.os.ParcelFileDescriptor")
        Dim f As JavaObject
        f.InitializeNewInstance("java.io.File",Array As String(pdf_path))
        Dim p As Object = parcel.RunMethod("open",Array(f, 268435456))
        Dim pdfRenderer As JavaObject
        pdfRenderer.InitializeNewInstance("android.graphics.pdf.PdfRenderer",Array(p))
        Dim page As JavaObject = pdfRenderer.RunMethod("openPage",Array(0))
        Dim bmp As Bitmap
        bmp.InitializeMutable(page.RunMethod("getWidth",Null),page.RunMethod("getHeight",Null))
        Dim Rect1 As Rect
        Rect1.Initialize(0, 0, page.RunMethod("getWidth",Null), page.RunMethod("getHeight",Null))
        page.RunMethod("render",Array(bmp, Null, Null,2))
        page.RunMethod("close",Null)
        pdfRenderer.RunMethod("close",Null)
        Return bmp
    Catch
        Log(LastException)
    End Try
End Sub
 
Upvote 0

Situ LLC

Active Member
Licensed User
Longtime User
Any time
 
Upvote 0
Top