Android Question RESOLVED: Problem rendering bitmaps to PDF

Lee Gillie CCP

Active Member
Licensed User
Longtime User
I am drawing to PdfDocument.Canvas. Lines, text, rectangles, all rendering fine, producing an incredible PDF document.

I go to add some DrawBitmap, and now AcrobatReader can no longer open the PDF. His error reported is "There was an error opening this document. The file is damaged and could not be repaired." I tried making the source rectangle NULL, but this did not help. Remove ALL calls to DrawBitmap and it produces a PDF file which Acrobat can open just fine.

My bitmap rendering routine is:
B4X:
Public Sub DrawImage( bm As Bitmap, hpos As Float, vpos As Float)
    Dim src As Rect
    Dim dst As Rect
    src.Initialize(0,0,bm.Width,bm.Height)
    dst.Initialize(Orx+(hpos*72),Ory+(vpos*72),Orx+(hpos*72)+bm.Width,Ory+(vpos*72)+bm.Height)
    drw.DrawBitmap(bm,Null,dst)
End Sub

Note: the "drw" object is the Canvas property from my PdfDocument.Canvas property. I have been using drw in this way for lines, text, and rectangles.

PdfDocument defined by "Printing (version: 1.00)" library I obtained through B4A posts here.
 

Lee Gillie CCP

Active Member
Licensed User
Longtime User
Ok, I think this is NOT any issue with the PDF. I have an LG tablet, and using the desktop PC to view files on the internal storage via USB connection. This part seems to be going brain-dead sometimes, and is not seeing up to the moment picture of what is stored on the tablet. When I rewrite the PDF file, then sometimes it reflects the up-to-date version, and sometimes I see it, but what it brings to PC when copying is junk. No errors reported in that process, it just pulls a corrupt file. I resolved this issue by disconnecting the tablet from PC, and reconnecting, THEN pulling the latest file, and these are coming out beautifully. First time to deal with this gotcha that has nothing to do with B4A or ANDROID.
 
Upvote 0

Lee Gillie CCP

Active Member
Licensed User
Longtime User
Don't reuse my code from above for DrawImage. There is a bug there. Notice it uses bm.width and bm.height to compute bottom and right of destination rectangle.

Instead of orx+(hpos*72)+bm.Width, for example, (orx= left margin (origin) in canvas units, hpos = left position relative to margin in inches. Something we DO NOT have here is accounting for the DPI resolution of the bitmap, such as bmrez, so the formula SHOULD be more like: ORX + (HPOS*72) + (bm.Width / bmrez) * 72

problem is, once you load a bitmap, you can see width and height, but the image's horz and vert DPI resolution is not available, that I can tell, so I have to pass as a parameter any time I call my render library DrawImage.
 
Upvote 0
Top