Android Question View pdf extracted from the database.

WebQuest

Active Member
Licensed User
Hi guys how do I initialize the PdfReader with the blob data taken from the database. I explain, I'm making an app that saves the pdf files on the device in a database as blobs, the problem is that reconverting from byte to bitmap I can not initialize the Pdfreader with the bitmap obtained. I'm trying this way.


B4X:
Sub ListViewPdf_ItemClick (Position As Int, Value As Object)
 
    Dim Cursor3 As Cursor
    Dim Name As String
    Dim Buffer3() As Byte
    Dim IpSt3 As InputStream
    Dim Bitmap3 As Bitmap
    Dim out As OutputStream
 
 
   Cursor3=Sql.ExecQuery("SELECT Name,Pdf,Data,ID FROM FilePdf ORDER BY ID DESC")
 
    For i = 0 To ListViewPdf.Size-1
        Cursor3.Position =Position
    Next
 
 
    Name = Cursor3.GetString("Name")
    Buffer3 = Cursor3.GetBlob("Pdf")
 
 
    IpSt3.InitializeFromBytesArray(Buffer3,0, Buffer3.Length)
    IpSt3.Close
    Bitmap3.Initialize2(IpSt3)
 
 
    pdf.Initialize("",File.DirDefaultExternal,Name&".pdf") 
    LPages2.Text=pdf.PageCount
    LPages2.Tag=pdf.PageCount
    LPage2.Text= 1
    LPage2.Tag=1
 
 
    ImageViewP.Gravity=Gravity.FILL
 
    PanelViewPDF.Visible=True
 
    LabelName.Text=Name
    ImageViewP.SetBackgroundImage(Bitmap3)
 
    Cursor3.Close
End Sub
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
I'm making an app that saves the pdf files on the device in a database as blobs
if i understand correctly you are not storing the PDF file, you are storing bitmaps you got from the old PDF.
I would store the PDF file, not the images. and later sdame the pdf to disc and then use the pdfrenderer again.
The renderer needs PDF Files as input, no Images.
 
Upvote 0

WebQuest

Active Member
Licensed User
I initially open the pdf file on the device with the PDFReader, then convert the Pdfreader bitmap to byte and save it in the database as a blob. The problem is that I can not do the opposite, from the database to .pdf. In theory I should save the bitmap as a pdf and then initialize the PdfReader with the path of the saved file. But I do not know how to save a bitmap on the device.
 
Upvote 0

WebQuest

Active Member
Licensed User
Hello DonManfred at the end I followed your advice. The intention is to import pdf files into the app. I solved by making a copy of the pdf in the dirinternal and save the name of the pdf in the database then in the listview at the click of the upload I see the pdf. What I can not do is give the possibility to view the pdf in a webview to have the possibility to zoom how can I solve?


Here I select the file I want to save in the app.
B4X:
Sub ImageViewAggPDF_Click
    Chooser.Initialize("chooser")
    Chooser.Show("application/pdf", "Select an Pdf")
    ToastMessageShow("Select PDF",True)
    PanelOpzioniPDF.Visible=False
End Sub


Sub chooser_Result ( Success As Boolean, Dir As String, FileName As String)
    Dim Nrpdf As Int
    Nrpdf=1
If Success Then
    If File.Exists(File.DirInternal,"Pdf"&Nrpdf&".pdf")=True Then
        LoadListPDF
        For i =0 To ListViewPdf.Size - 1
            Nrpdf = ListViewPdf.Size
        If File.DirInternal.Contains("Pdf" & Nrpdf & ".pdf")=True Then
            Nrpdf=Nrpdf+1
        End If
        Next
    End If
        File.Copy(Dir,FileName,File.DirInternal,"Pdf"&Nrpdf&".pdf")
       
       
        pdf.Initialize("",File.DirInternal,"Pdf"& Nrpdf &".pdf")
     
       
        LPages.Text=pdf.PageCount
        LPages.Tag=pdf.PageCount
       
        LPage.Text= 1
        LPage.Tag=1
       
        For i =0 To pdf.PageCount -1
            Page =  pdf.renderPageforDisplay(0)
            ImageViewResult.Bitmap = Page
        Next
       
        PanelHomePDF.Visible=False
        PanelInsertPDFText.Visible=True
        LinkDir= "Pdf"&Nrpdf&".pdf"
       
Else
        ToastMessageShow("No PDF not selected", True)
    End If
   
   
End Sub


Here I save the date name of the pdf I want to save in the app.

B4X:
Sub BtInsertPDF_Click
    If EditTextTitle.Text<>"" Then
        Dim PdfData() As Byte = ImageToBytes(Page)
        Dim Orario As String
        Dim text As String
        Dim date As String
        Dim Ora As String
       
         text = EditTextTitle.Text
        'Variabili Orario
        Dim now As Long
        Dim M, H As String

        DateTime.DateFormat = "dd MMM yyyy"
        date = DateTime.Date(DateTime.now)
       
        H = DateTime.GetHour(DateTime.Now)
        M = DateTime.GetMinute(DateTime.Now)
        Ora = H & ":" & M
       
        Orario = date &"    "& Ora
    Sql.ExecNonQuery2("INSERT INTO FilePdf (Name,Pdf,Data,Link) VALUES(?,?,?,?)", Array(text,PdfData,Orario,LinkDir))
    Cursor=Sql.ExecQuery("SELECT Name,Pdf,Data,Link FROM FilePdf ORDER BY ID DESC")
    For i = 0 To Cursor.RowCount-1
        Cursor.Position=i
        Log(Cursor.GetString("Name"))
        Log(Cursor.GetString("Data"))
        Log(Cursor.GetString("Link"))
    Next
    Cursor.Close
    PanelHomePDF.Visible=True
    PanelInsertPDFText.Visible=False
    EditTextTitle.Text=""
    Else
        Msgbox("Enter a Name or Descrition","Info")
        End If
End Sub

Here I recall the link and the pdf file for the visualization.

B4X:
Sub ListViewPdf_ItemClick (Position As Int, Value As Object)
   
    Dim Cursor3 As Cursor
   

    ListViewPdf.Enabled=True
    Cursor3=Sql.ExecQuery("SELECT Name,Pdf,Data,ID,Link FROM FilePdf ORDER BY ID DESC")

    For i = 0 To ListViewPdf.Size-1
        Cursor3.Position =Position
    Next
   
    LoadLink = Cursor3.GetString("Link")
    Log(LoadLink)

    pdf.Initialize("",File.DirInternal,LoadLink)
 
    LPages2.Text=pdf.PageCount
    LPages2.Tag=pdf.PageCount
    LPage2.Text= 1
    LPage2.Tag=1
   
    ImageViewP.Gravity=Gravity.FILL
   
    PanelViewPDF.Visible=True
    PanelHomePDF.Visible=False
   
    ImageViewP.SetBackgroundImage(pdf.renderPageforDisplay(0))

    Cursor3.Close
    LoadListPDF
End Sub
 
Upvote 0
Top