Java Question B4A Canvas != android.graphics.Canvas

DonManfred

Expert
Licensed User
Longtime User
Hello,
i´m doing a wrap for the android.graphics.pdf.PdfDocument
Each Document is based on one or more android.graphics.pdf.PdfDocument.Page

The sample-code shown here: https://developer.android.com/reference/android/graphics/pdf/PdfDocument.html

is just the following:

B4X:
// 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

B4X:
    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 ;)
 

DonManfred

Expert
Licensed User
Longtime User
You will need to do something like
Cool. That works.

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")

Thank you!
 

Attachments

  • TestpdfCanvas.pdf
    26.4 KB · Views: 460

DonManfred

Expert
Licensed User
Longtime User
I´ve played around a bit with the canvas and to draw a view (or even the complete activity) on the pdf.

ignore the "text-page"... it is more or less useless. :D

It is working for far but i still did not know if the values of page rect and "clientrect" i am using are rigth or wrong....

At least it is a cheap way to create a pdf just with using android.jar ;-)
 

Attachments

  • TestpdfCanvasScreenshot.pdf
    327.7 KB · Views: 480
Top