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.b4x.com/forum/basic4android-getting-started-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:
Basically the code to create a pdf with some pages (find it attached) is
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...
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.b4x.com/forum/basic4android-getting-started-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
- 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
Last edited: