B4A Library PDF - Create PDF using android.jar built-in methods

This is a wrap for the Android PdfDocument and Page as described here.

Requirements: Android Device with Api 19+

The lib is made out of curiosity (and just for fun) to try if this works... It does not have much features. The Main feature (or even you can call it a restriction) is that you just "Draw on a Canvas" to fill a PDF-Page.

It is working so far and i want to share it with you. Maybe it is interesting for you...


PDF
Comment:
This library wraps the <link>PdfDocument|https://developer.android.com/reference/android/graphics/pdf/PdfDocument.html</link> and a more complex example: <link>Flickr Viewer|http://www.basic4ppc.com/forum/basi...rted-tutorials/6646-flickr-viewer.html</link>
This class enables generating a PDF document from native
Android content. You create a new document and then for
every page you want to add you start a page, write content
to the page, and finish the page. After you are done with
all pages, you write the document to an output stream and
close the document. After a document is closed you should
not use it anymore.
Note that pages are created one by one, i.e. you can have
only a single page to which you are writing at any given time.
This class is not thread safe.
Author: DonManfred (wrapper)
Version: 0.25
PageInfo
Methods:

  • IsInitialized As boolean
  • Initialize (ba As anywheresoftware.b4a.BA, pageWidth As int, pageHeight As int, pageNumber As int, contentRect As android.graphics.Rect) As void
Properties:
  • PageNumber As int [read only]
  • ContentRect As android.graphics.Rect [read only]
  • PageHeight As int [read only]
  • PageWidth As int [read only]




Basically the code to create a pdf with some pages (find it attached) is

B4X:
Sub Button1_Click
    pdf.Initialize("")
    Dim info As PageInfo
    Dim r As Rect
    r.Initialize(10,10,575,822)
    info.Initialize(595,842,1,r)

    Dim theight As Int
    Dim trec As TextRec
    theight = trec.Initialize($"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. "$ _
,550,800,14,Colors.Blue)
    Log("Height of text calculated: "&theight)

    Dim p As Page = pdf.startPage(info)

    Dim c As Canvas = p.Canvas



    trec.draw(p,0,0)

    pdf.finishPage(p)
    Dim p As Page = pdf.startPage(info)
    Dim c As Canvas = p.Canvas

    Dim srcrec As Rect
    srcrec.Initialize(0,0,ImageView1.Width,ImageView1.Height)

    Dim dstrec As Rect
    dstrec.Initialize(0,0,ImageView1.Width/2,ImageView1.Height/2)

    Log(ImageView1.Width)

    Dim txt As String = "This is the first line" & CRLF & "This is the second line" & CRLF & "..." & CRLF & "This is the last line"
    DrawMultilineText(c, txt, 70, 10, Typeface.DEFAULT_BOLD, 2, Colors.Black, "LEFT", 0dip)
    'DrawMultilineText(c, txt, 60, 15dip, Typeface.MONOSPACE, 2, Colors.Black, "CENTER", 5dip)

    Dim srcrec2 As Rect
    srcrec2.Initialize(0,0,100%x,100%y)

    p.DrawScreenShot(0,0,100%x,100%y,0,0,575,822)

    'c.DrawBitmap(ImageView1.Bitmap,srcrec,dstrec)
    pdf.finishPage(p)

    Dim p As Page = pdf.startPage(info)
    p.DrawScreenShot(0,0,100%x,100%y,0,0,460,650)
    pdf.finishPage(p)

    Dim p As Page = pdf.startPage(info)
    p.DrawScreenShot(0,0,100%x,100%y,0,0,115,165)
    pdf.finishPage(p)

    pdf.writeTo(File.DirRootExternal,"TestpdfCanvasScreenshot.pdf")
    Dim i As Intent
    i.Initialize(i.ACTION_VIEW, "file://" & File.DirRootExternal & "/TestpdfCanvasScreenshot.pdf")
    i.SetType("application/pdf")
    StartActivity(i)
End Sub
 

Attachments

  • pdfEx.zip
    35.7 KB · Views: 759
  • PDFV0.25.zip
    8.3 KB · Views: 758
  • TestpdfCanvasScreenshot.pdf
    326.1 KB · Views: 736
Last edited:

MarcoRome

Expert
Licensed User
Really Great Library Don +50 ;)
Can i change also layout ?
Type:

B4X:
'Here use another lay
'.....  After change
Activity.LoadLayout("test")
Dim p As Page = pdf.startPage(info)
p.DrawScreenShot(0,0,100%x,100%y,0,0,115,165)
pdf.finishPage(p)

Thank you
 

DonManfred

Expert
Licensed User
Activity.LoadLayout("test")
Dim p As Page = pdf.startPage(info)
p.DrawScreenShot(
0,0,100%x,100%y,0,0,115,165)
pdf.finishPage(p)
i´m pretty sure it will not work...


Try to do this

In you button click:
1. Load the layout
2. Call a sub with CallSubPlus (500ms delay)
3. In this sub you do the PDF-Page generation
 

MarcoRome

Expert
Licensed User
i´m pretty sure it will not work...


Try to do this

In you button click:
1. Load the layout
2. Call a sub with CallSubPlus (500ms delay)
3. In this sub you do the PDF-Page generation

This time you are wrong ;) ( better so :D )
Work, in attachment example and result as pdf
 

Attachments

  • PDF-DonMandfred.zip
    37.5 KB · Views: 428
  • TestpdfCanvasScreenshot185332.pdf
    347.7 KB · Views: 453

tekh2999

Member
Licensed User
I'm getting the same error, Maven artifact not found: com.android/support-v4. I do have B4A v6+ and i do see the said jar file in my library but getting this error. Don't understand why, it's there.
 

SAMER ISSA

Member
Licensed User
Nice Library ;)

Its very good .... :)

But in TextRec.draw any idea to add (Aligns type) like "CENTER" "RIGHT" "LEFT"

Its only "LEFT" align:oops:

Please !!!!!!:(
 
Top