B4J Question [SOLVED] Write snapshot directly to PDF

bdunkleysmith

Active Member
Licensed User
Longtime User
I have the following working code which takes a snapshot of an image (chart) and pastes it into a PDF document:

B4X:
Dim nd As Node = frm.RootPane
Dim bmp As B4XBitmap = nd.Snapshot
Dim out As OutputStream
out = File.OpenOutput(datafolder, "snapshot.png", False)
bmp.WriteToStream(out, 100, "PNG")
out.Close
Dim Image1 As PDFjetImage
Image1.Initialize(PDFjetPDF1, File.OpenInput(datafolder, "snapshot.png"), PDFjetConstants1.ImageType.PNG)
Image1.SetPosition(30, 200)
Image1.DrawOn(PDFPage)
File.Delete(datafolder, "snapshot.png")

This code saves the image to a file which is deleted after its use is served when it is pasted into the PDF document.

I have been scratching my head trying and searching this Forum to work out how I can avoid the step of saving and then deleting the (temporary) file because I thought it would be cleaner code, but can't work out how to write the image stream directly to the PDF initialization statement.

Can it be done and if so, any assistance would be appreciated?
 
Solution
I have the following working code which takes a snapshot of an image (chart) and pastes it into a PDF document:

B4X:
Dim nd As Node = frm.RootPane
Dim bmp As B4XBitmap = nd.Snapshot
Dim out As OutputStream
out = File.OpenOutput(datafolder, "snapshot.png", False)
bmp.WriteToStream(out, 100, "PNG")
out.Close
Dim Image1 As PDFjetImage
Image1.Initialize(PDFjetPDF1, File.OpenInput(datafolder, "snapshot.png"), PDFjetConstants1.ImageType.PNG)
Image1.SetPosition(30, 200)
Image1.DrawOn(PDFPage)
File.Delete(datafolder, "snapshot.png")

This code saves the image to a file which is deleted after its use is served when it is pasted into the PDF document.

I have been scratching my head trying and searching this Forum to work out how I can...

4rdn1_s

Member
I have the following working code which takes a snapshot of an image (chart) and pastes it into a PDF document:

B4X:
Dim nd As Node = frm.RootPane
Dim bmp As B4XBitmap = nd.Snapshot
Dim out As OutputStream
out = File.OpenOutput(datafolder, "snapshot.png", False)
bmp.WriteToStream(out, 100, "PNG")
out.Close
Dim Image1 As PDFjetImage
Image1.Initialize(PDFjetPDF1, File.OpenInput(datafolder, "snapshot.png"), PDFjetConstants1.ImageType.PNG)
Image1.SetPosition(30, 200)
Image1.DrawOn(PDFPage)
File.Delete(datafolder, "snapshot.png")

This code saves the image to a file which is deleted after its use is served when it is pasted into the PDF document.

I have been scratching my head trying and searching this Forum to work out how I can avoid the step of saving and then deleting the (temporary) file because I thought it would be cleaner code, but can't work out how to write the image stream directly to the PDF initialization statement.

Can it be done and if so, any assistance would be appreciated?
Hi bdunkleysmith, i want to ask you will your code work on B4A ?
Thankyou
 
Upvote 0

bdunkleysmith

Active Member
Licensed User
Longtime User
Hi bdunkleysmith, i want to ask you will your code work on B4A ?
Thankyou
I do not know, but I don't think this code will work on B4A.

The first part which saves the snapshot as a PNG file may, but the second part which pastes the PNG into a PDF relies on the jPDFJet library which I don't believe is compatible with B4A.

Sorry I can't be more definitive - perhaps you just have to try it.
 
Upvote 0

teddybear

Well-Known Member
Licensed User
I have the following working code which takes a snapshot of an image (chart) and pastes it into a PDF document:

B4X:
Dim nd As Node = frm.RootPane
Dim bmp As B4XBitmap = nd.Snapshot
Dim out As OutputStream
out = File.OpenOutput(datafolder, "snapshot.png", False)
bmp.WriteToStream(out, 100, "PNG")
out.Close
Dim Image1 As PDFjetImage
Image1.Initialize(PDFjetPDF1, File.OpenInput(datafolder, "snapshot.png"), PDFjetConstants1.ImageType.PNG)
Image1.SetPosition(30, 200)
Image1.DrawOn(PDFPage)
File.Delete(datafolder, "snapshot.png")

This code saves the image to a file which is deleted after its use is served when it is pasted into the PDF document.

I have been scratching my head trying and searching this Forum to work out how I can avoid the step of saving and then deleting the (temporary) file because I thought it would be cleaner code, but can't work out how to write the image stream directly to the PDF initialization statement.

Can it be done and if so, any assistance would be appreciated?
You can do it using bytearray without temporary file, like this:
B4X:
Dim out As OutputStream
  out.initializetobytesarray(0)
  bmp.WriteToStream(out, 100, "PNG")
Dim in as InputStream
  in.initializefrombytesarray(out.tobytesarray, 0, out.tobytesarray.length)
Dim Image1 As PDFjetImage
  Image1.Initialize(PDFjetPDF1, in, PDFjetConstants1.ImageType.PNG)
 
Last edited:
Upvote 0
Solution

bdunkleysmith

Active Member
Licensed User
Longtime User
You can do it using bytearray without temporary file, like this:
B4X:
Dim out As OutputStream
  out.initializetobytesarray(0)
  bmp.WriteToStream(out, 100, "PNG")
Dim in as InputStream
  in.initializefrombytesarray(out.tobytesarray, 0, out.tobytesarray.length)
Dim Image1 As PDFjetImage
  Image1.Initialize(PDFjetPDF1, in, PDFjetConstants1.ImageType.PNG)

Thank you @teddybear, your code works perfectly and is the solution to my original question.
 
Upvote 0

4rdn1_s

Member
I do not know, but I don't think this code will work on B4A.

The first part which saves the snapshot as a PNG file may, but the second part which pastes the PNG into a PDF relies on the jPDFJet library which I don't believe is compatible with B4A.

Sorry I can't be more definitive - perhaps you just have to try it.
Thank you @bdunkleysmith, but you are right, the jPDFJet doesn't compatible with B4A.
 
Upvote 0
Top