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: 800
  • 1672644931064.png
    1672644931064.png
    48.8 KB · Views: 2,397
  • pdfgen.zip
    284.4 KB · Views: 129
Last edited:

spsp

Active Member
Licensed User
Longtime User
Great. Please is it able to specify paper sizes example A4 and create multiple pages if the information on a page 1 is more than the page?
Hi,

Yes, there is constants for standards paper size or you can set your own paper size.

You ca add add as many pages you want with differents sizes

spsp
 

spsp

Active Member
Licensed User
Longtime User
Hi,

New version 0.3

Add outRaw method, allow you to enter PDF command directly
https://opensource.adobe.com/dc-acrobat-sdk-docs/standards/pdfstandards/pdf/PDF32000_2008.pdf
chapter 8 explains all the command to draw path, bezier curve.......


draw triangle:
    'start point (command m) : 20 260 m
    'line to (command l) : 60 240 l
    'line to (command l) : 20 220 l
    'close fill and stroke the path (command b)
    pdf.outRaw($"20 260 m 100 260 l 60 220 l b"$)

Draw Bezier Curve:
    'draw a bezier curve
    'start point (command m) : 15 150 m
    'control point 1, control point 2, end point (command c) and close path (command S) : 75 200 135 100 195,150 c S
    pdf.outRaw($"15 150 m 75 200 135 100 195 150 c S"$)


draw 10 randoms lines:
    'draw 10 line with random points and fill the path
    Dim s As String
    For i=0 To 9
        s=s & Rnd(20,190) & " " & Rnd(80,120) & " " & IIf(i=0,"m ","l ")
    Next
    s=s & "b"
    pdf.outRaw(s)

1672737533841.png


pdfGen.zip upated in post #1

spsp
 

bleber

Member
Great work !!!
Some method to merge 2 pages,
  • Base page as template, and add other pdf stuff to merge together.
  • Or generate pdf and add another as backgraud and mege all.
Thanks!
 

spsp

Active Member
Licensed User
Longtime User
Great work !!!
Some method to merge 2 pages,
  • Base page as template, and add other pdf stuff to merge together.
  • Or generate pdf and add another as backgraud and mege all.
Thanks!
Hi,

No, this class doesn't allow that.

spsp
 

Chicao99

Member
Licensed User
Longtime User
Hi, I can't load the attached image, is there any problem? I always get "Insufficient data for an image" error.
 

Attachments

  • image.png
    image.png
    19.4 KB · Views: 137

spsp

Active Member
Licensed User
Longtime User
Hi, I can't load the attached image, is there any problem? I always get "Insufficient data for an image" error.
Hi,

the color space of your image (6) is not supported. I just open your image with 'Irfan view' and save it, now the color space is 2 and it's OK.

I will try to modifiy the class to support this color space.

attached, your new image.

spsp
 

Attachments

  • image.png
    image.png
    17 KB · Views: 133

spsp

Active Member
Licensed User
Longtime User
Hi,

Version 0.4

add style StrikeThrough to font

add constants for style (fontNormal, fontBold, fontItalic, fontUnderline and fontStrikeThrough)

add constants for color (colorBlack, colorWhite, colorBlue, colorGreen, colorRed and colorGray)

add method gMultilineTextSize. support automatic and explicit CR

add method outDrawFlow to write multiline text

pdfgen.zip in post#1 updated

spsp
 

MichalK73

Well-Known Member
Licensed User
Longtime User
Hello.

I'm testing this library right now. It's simple and fun to create pdf.
However, I have a problem to solve.
Namely, I have text for several pages. Now, when I insert a longer text, the page ends, the text as much as possible is on the PDF, the rest is not. How to detect the end of a PDF page so that further text goes to the next page?
 
Top