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,152
  • 1672644931064.png
    1672644931064.png
    48.8 KB · Views: 2,753
  • pdfgen.zip
    284.4 KB · Views: 672
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: 87

Radisk3

Member
Licensed User
Longtime User
Hello everyone!

I'm trying to create a PDF and I've come across a situation that I haven't been able to resolve.

When using the pdf.outtextflow method, can I write in columns?

I would like to divide some parts of the text into columns, like a table.

This way, I can align a part of the text to the left, right and center, practically creating a table.

Thanks everyone!
 

Setlodi

Active Member
Hello everyone!

I'm trying to create a PDF and I've come across a situation that I haven't been able to resolve.

When using the pdf.outtextflow method, can I write in columns?

I would like to divide some parts of the text into columns, like a table.

This way, I can align a part of the text to the left, right and center, practically creating a table.

Thanks everyone!
I haven't used outtextflow but I am using outtext to make columns. To create 2 columns I write 2 lines of code:

B4X:
pdf.outtext(15,285,"Name:")
pdf.outtext(30,285,Name)

pdf.outtext(15,279,"Surname: ")
pdf.outtext(30,279,Surname)

As you can see, for "Name", I have 2 lines of code, both on the same y-axis - 285, but on different x-axis - 15 and 30. So for every row on the table, you keep the same y-axis and the x-axis determines the column position.

If I were to do create 4 columns with the following headings:
B4X:
Name | Surname | Age | Gender
then my code will be:

B4X:
pdf.outtext(10,300,Name)
pdf.outtext(20,300,Surname)
pdf.outtext(30,300,Age)
pdf.outtext(40,300,Gender)

Notice that the y-axis is always the same because they're on the same row - 300, and the x-axis is changing because of different positions of the columns 10, 20, 30, 40

I hope this helps...
 

Radisk3

Member
Licensed User
Longtime User
I haven't used outtextflow but I am using outtext to make columns. To create 2 columns I write 2 lines of code:

B4X:
pdf.outtext(15,285,"Name:")
pdf.outtext(30,285,Name)

pdf.outtext(15,279,"Surname: ")
pdf.outtext(30,279,Surname)

As you can see, for "Name", I have 2 lines of code, both on the same y-axis - 285, but on different x-axis - 15 and 30. So for every row on the table, you keep the same y-axis and the x-axis determines the column position.

If I were to do create 4 columns with the following headings:
B4X:
Name | Surname | Age | Gender
then my code will be:

B4X:
pdf.outtext(10,300,Name)
pdf.outtext(20,300,Surname)
pdf.outtext(30,300,Age)
pdf.outtext(40,300,Gender)

Notice that the y-axis is always the same because they're on the same row - 300, and the x-axis is changing because of different positions of the columns 10, 20, 30, 40

I hope this helps...

Hello my friend!

Thanks for the explanation.

This is exactly how I have been doing it, controlling by positions and, in the case of numbers aligned to the right, I use a Courier text font and control by spaces.
What I intended to do was use "outtextflow", which would make the text aligned with more quality and simplify the code.
My ignorance has prevented me from being able to do it using it so far, so I continue to do it using X, Y positions.

If you have any alternative, it would be welcome.

Again, thanks for the explanation.
 

aviario

Active Member
Licensed User
Longtime User
Hi.

Sorry for my bad english

I am trying the b4a example downloaded from this post, but when I run it it gives me several errors.

I was solving some but I have others that I don't know how to solve.

The iif statements give me an error, so I converted them into if else and that's it.

But I have others such as

Dim c() As Byte=fPages.Get(apage).As(TPDFPage).fBuffer.GetBytes(fTextEncoding)

What do they tell me:
Undeclared variable 'tpdffontinfo' is used before any value has been assigned.

I don't know how to solve it. I don't know if I'm missing a library or if it's because I'm using b4a instead of b4x.

I hope you can help me.

Thanks in advance
 

aviario

Active Member
Licensed User
Longtime User
Hi.

sorry for the previous post.
I was doing the tests with b4a v10. I decided to try B4x v13 and the example worked correctly.

I would like to know if cpdf.bas can be modified to work without the b4x dependencies.

for example what would be sb.Append(fPages.Get(apage).As(TPDFPage).fbuffer)
in B4A v 9.5 0 v10
 
Top