B4A Class [B4X] PDF Generator - B4X Cross Platform - Class 100% B4X Code

Hi,

This class cPDF.bas (version 0.6 - 2024-01-03) generate PDF File with limited fonctionnalities (but enough for me)
  • Add pages with different paper size (use constants or custom size)
  • PDF standards font (Courier, Helvetica, Times, Symbol and Zapfdingbats), style (Normal, Bold, Italic, Underline and StrikeThrough), encoding cp1252
  • draw text : single line of text at position x, y
  • draw text flow with explicit and automatic CR at current x,y position with alignment (left, center, right, justify)
  • Measure text (width and height)
  • Measure multiline text (width and height)
  • draw line
  • draw rectangle
  • draw images (only PNG, all format that can be loaded with xui.loadbitmap)
  • use pdf commands directly
  • save to file with/without compression
Source full commented, example for B4J and B4A, I don't use B4I (but should be OK)
dependencies :
  • B4XCollections
  • RandomAccessFile

Help welcome to add support for :
  • PNG image wih colorspace = 6
  • JPG image
  • loading font from file

1673250547389.png

1672644892353.png

1672644909961.png

1704277206392.png




spsp
 

Attachments

  • 1672644870818.png
    1672644870818.png
    7.1 KB · Views: 1,090
  • 1672644931064.png
    1672644931064.png
    48.8 KB · Views: 2,693
  • pdfgen.zip
    284.4 KB · Views: 572
Last edited:

Setlodi

Active Member
I've found a workaround for adding images to a PDF document. My app captures signatures and then adds them on a PDF that I'm generating using the cPDF class. My signatures are captures with colorspace = 6 value and I couldn't see them in my PDF. When I use IrFax to change colorspace value to 2, as recommended in one of the posts it worked. However, to programmatically change was a challenge. So what I did is I converted my signature PNG image to JPEG and back to PNG, using the ConvertImage library.


Convert PNG image to JPEG and back to PNG:
'convert to JPEG
    Dim Bitmap1 As Bitmap = LoadBitmap(File.DirInternal, "signtest.png")
    Dim SavePathAndFileName As String = File.Combine(File.DirInternal, "signtest_JPG_conv.jpg")
    ConvertImage1.ConvertBitmapToFile(Bitmap1, SavePathAndFileName, ConvertImage1.TO_JPEG, 100)
    Log("signature.png converted to signature_JPG_conv.jpg")

    'convert to PNG
    Dim Bitmap1 As Bitmap = LoadBitmap(File.DirInternal, "signtest_JPG_conv.jpg")
    Dim SavePathAndFileName As String = File.Combine(File.DirInternal, "signtest_PNG_conv.png")
    ConvertImage1.ConvertBitmapToFile(Bitmap1, SavePathAndFileName, ConvertImage1.TO_PNG, 100)
    Log("signature.png converted back to signature_PNG_conv.png")
 

spsp

Active Member
Licensed User
Longtime User
I've found a workaround for adding images to a PDF document. My app captures signatures and then adds them on a PDF that I'm generating using the cPDF class. My signatures are captures with colorspace = 6 value and I couldn't see them in my PDF. When I use IrFax to change colorspace value to 2, as recommended in one of the posts it worked. However, to programmatically change was a challenge. So what I did is I converted my signature PNG image to JPEG and back to PNG, using the ConvertImage library.


Convert PNG image to JPEG and back to PNG:
'convert to JPEG
    Dim Bitmap1 As Bitmap = LoadBitmap(File.DirInternal, "signtest.png")
    Dim SavePathAndFileName As String = File.Combine(File.DirInternal, "signtest_JPG_conv.jpg")
    ConvertImage1.ConvertBitmapToFile(Bitmap1, SavePathAndFileName, ConvertImage1.TO_JPEG, 100)
    Log("signature.png converted to signature_JPG_conv.jpg")

    'convert to PNG
    Dim Bitmap1 As Bitmap = LoadBitmap(File.DirInternal, "signtest_JPG_conv.jpg")
    Dim SavePathAndFileName As String = File.Combine(File.DirInternal, "signtest_PNG_conv.png")
    ConvertImage1.ConvertBitmapToFile(Bitmap1, SavePathAndFileName, ConvertImage1.TO_PNG, 100)
    Log("signature.png converted back to signature_PNG_conv.png")
Hi,

The class is updated in post #1 (version 0.5). it's support all image format loadable with xui.loadbitmap.


spsp
 

aeric

Expert
Licensed User
Longtime User
PDF standards font (Courier, Helvetica, Times, Symbol and Zapfdingbats), style (Normal, Bold, Italic, Underline and StrikeThrough), encoding cp1252
I hope this class can support unicode or utf8 encoding with Chinese standard fonts such as STHeitiSC-Light and AdobeMingStd-Light in the future.
 

Chicao99

Member
Licensed User
Longtime User
When I add the library to my project it triggers an error on the cPDF module:

B4i line: 210
bb.Append($\
use of undeclared identifier 'GetBytes'

Any ideas?
 

Chicao99

Member
Licensed User
Longtime User
try this non official version.

There we go!
It passed the compilation! I will try to use!
Thank you very much @jahswant !!
 

Schakalaka

Active Member
Licensed User
Longtime User
Hi,

This class cPDF.bas (version 0.6 - 2024-01-03) generate PDF File with limited fonctionnalities (but enough for me)
  • Add pages with different paper size (use constants or custom size)
  • PDF standards font (Courier, Helvetica, Times, Symbol and Zapfdingbats), style (Normal, Bold, Italic, Underline and StrikeThrough), encoding cp1252
  • draw text : single line of text at position x, y
  • draw text flow with explicit and automatic CR at current x,y position with alignment (left, center, right, justify)
  • Measure text (width and height)
  • Measure multiline text (width and height)
  • draw line
  • draw rectangle
  • draw images (only PNG, all format that can be loaded with xui.loadbitmap)
  • use pdf commands directly
  • save to file with/without compression
Source full commented, example for B4J and B4A, I don't use B4I (but should be OK)
dependencies :
  • B4XCollections
  • RandomAccessFile

Help welcome to add support for :
  • PNG image wih colorspace = 6
  • JPG image
  • loading font from file

View attachment 137882
View attachment 137554
View attachment 137555
View attachment 149227



spsp
hello, how can i define/add spage interline in paragraph and
how to create a new page when text is too long for one page?
in my example the text is all in one page, but it needs 2.
Thank for your great work
 

Attachments

  • pdfgen.zip
    227.2 KB · Views: 61
Top