// create a new document
PdfDocument document = new PdfDocument();
// crate a page description
PageInfo pageInfo = new PageInfo.Builder(new Rect(0, 0, 100, 100), 1).create();
// start a page
Page page = document.startPage(pageInfo);
// draw something on the page
View content = getContentView();
content.draw(page.getCanvas());
// finish the page
document.finishPage(page);
. . .
// add more pages
. . .
// write the document content
document.writeTo(getOutputStream());
// close the document
document.close();
When using my wrap in b4a i now want to do similar... so i defined a document and then i started to create a Page. Then i tried to get the Canvas from the Page-object to draw on the Canvas.
But this does not work.
B4X:
public Canvas getCanvas() {
return getObject().getCanvas();
}
In b4a i got the message
java.lang.ClassCastException: android.graphics.pdf.PdfDocument$PdfCanvas cannot be cast to anywheresoftware.b4a.objects.drawable.CanvasWrapper
pdf.Initialize("")
Dim info As PageInfo
Dim r As Rect
r.Initialize(50,50,450,780) ' Not sure about this values. Just a test
info.Initialize(595dip,842dip,1,r) ' same about the values here
Dim p As Page = pdf.startPage(info) ' startpage(info) returns a new Page object
Dim c As Canvas = p.Canvas
pdf is the pdfDocument object.
How do i get access to the Canvas? Any hint is higly welcome
And it also works to draw on that canvas and then to save the page and then to save the pdfdocument.
B4X:
Activity.LoadLayout("pdfPageLayout1")
pdf.Initialize("")
Dim info As PageInfo
Dim r As Rect
r.Initialize(50,50,450,780)
info.Initialize(595dip,842dip,1,r)
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)
c.DrawBitmap(ImageView1.Bitmap,srcrec,r)
pdf.finishPage(p)
pdf.writeTo(File.DirRootExternal,"TestpdfCanvas.pdf")