B4A Library [Beta] RenderPDF - A Native Android PDF Renderer

This is a wrapper for the android.graphics.pdf.PdfRenderer API introduced in Lollipop . The example code requires the PDFWrapper library.

B4X:
Sub Process_Globals
    Dim sWriteDir As String
    Dim PDFWriter As PDF
    Dim PDFReader As RenderPDF
End Sub
Sub Activity_Create(FirstTime As Boolean)
    Dim b1 As Bitmap
    Dim o1 As OutputStream
    Dim r As Rect
    Dim b As Bitmap
    Dim c As Canvas
    b.InitializeMutable(100,200)
    c.Initialize2(b)
    c.DrawColor(Colors.Blue)
    r.Initialize(100,150,200,300)
    If  File.Exists(File.DirRootExternal,"PDFTest") =  False Then
         File.MakeDir(File.DirRootExternal,"PDFTest")
     End If
    sWriteDir = File.DirRootExternal & "/PDFTest"
    If  File.Exists(sWriteDir,"Test.pdf") = True Then
        File.Delete(sWriteDir,"Test.pdf")
    End If
  
    'Generate PDF Test File
    PDFWriter.Initialize()
    PDFWriter.AddPage(595,842,1)
    PDFWriter.DrawText("Page1",10,72,Typeface.DEFAULT_BOLD,72,Colors.Red,"LEFT")
    PDFWriter.FinishPage
    PDFWriter.AddPage(595,842,2)
    PDFWriter.DrawCircle(100,100,50,Colors.Red,True,0)
    PDFWriter.DrawOval(r,Colors.Red,True,0)
    PDFWriter.FinishPage
    PDFWriter.AddPage(595,842,3)
    PDFWriter.DrawRect(r,Colors.Red,True,0)
    PDFWriter.FinishPage
    PDFWriter.AddPage(595,842,3)
    PDFWriter.DrawBitmap(b,Null,r)
    PDFWriter.FinishPage
    PDFWriter.WritePDF(sWriteDir,"Test.pdf")
  
    'Open PDF for Readding
    PDFReader.Initialize(sWriteDir,"Test.pdf")
    'Render the first page to a PNG file
    PDFReader.RenderPageToFile(0,sWriteDir,"TestFromPDFRenderer.png")
    'Render the first page to a PNG file
    o1 = File.OpenOutput(sWriteDir,"TestFromB4A.png",False)
    b1.Initialize3(PDFReader.RenderPage(0))
    b1.WriteToStream(o1,100,"PNG")
    o1.Close
    'Set activity background to the rendered page
    Activity.SetBackgroundImage(b1)
    'get number of pages in document
    Log("Page Count: " & PDFReader.GetPageCount)
    'get page height
    Log("Page 1 Height: " & PDFReader.GetPageHeight(0))
    'get page width
    Log("Page 1 Width: " & PDFReader.GetPageWidth(0))
    'Should document be scaled for printing?
    Log("Scale For Print:" & PDFReader.GetScaleForPrint)
    'Set render engine to render for printing
    PDFReader.SetRenderForPrint(True)
    'Get whether render engine is set render for print
    Log("Render For Print:" & PDFReader.GetRenderForPrint)
    'Set custom bitmap density (default is 72 ie. 1 pixel = 1 PostScript point)
    PDFReader.SetDensity(160)
    'Get bitmap density (default is 72 ie. 1 pixel = 1 PostScript point)
    Log("Bitmap Density: " & PDFReader.GetDensity())
    'Close the document
    PDFReader.Close

As per the PDFWrapper library the page height and width returned is PostScript points (1/72th of an inch). For display purposes you will probably want to set the density to the device density rather than the default density used which is 72.

RenderPDF.zip contains the files you need to copy into your Additional Libraries folder.
 

Attachments

  • RenderPDF.zip
    3.3 KB · Views: 431
Last edited:

DonManfred

Expert
Licensed User
PDFReader.SetDensity(160)
may i ask how your implement this in your library? Say: can you show me your java-code setting the density please?
 
Top