B4J Library PDF Creator and viewer

spsp

Member
Licensed User
Hello,

Here are 3 classes :
- PdfCreator : create PDF
- PDFViewer : render PDF
- clsFormPDF : form to preview PDF (Page navigation, Zoom, Print, Save)

PdfCreator and PDFViewer wraps PDFBox
You have to download pdfbox-app-2.0.20.jar and copy it in the additional folder
You have to download icu4j-67_1.jar and copy it in the additional folder for right to left and connected arabic characters
B4X:
    #AdditionalJar: pdfbox-app-2.0.20.jar
    #AdditionalJar: icu4j-67_1.jar
For right to left and connected arabic characters, you need to uses the "bidi" method to buld your string
B4X:
    s=pdfcreator.bidi("صباحا")
The zip contains a small project which show how to create PDF and preview them
  • The fist is very simple, just few lines of text
  • The second is a long list with image, table, page footer, text color, background color....
  • The third use differents standard fonts and load a font from a file and display arabic characters
1592157008136.png

1592157047929.png


1592159743160.png

1593780330305.png


To create a PDF :
  1. Create an instance of pdfCreator and initialize it
  2. Create page
  3. Create content
  4. Write on the content (text, image, rect)
  5. Close content
  6. Add page to PDF
  7. repeat 2 to 6
  8. write PDF to stream or file

spsp
 

Attachments

Last edited:

micro

Well-Known Member
Licensed User
Thanks for sharing your work

The form is always empty but if I send to print, the print displays everything

1592229846388.png
 

behnam_tr

Member
i cant print arabic characters
defualt fonts dont support arabic characters
but i find this from pdfbox site

Hello World Using a TrueType Font
This small sample shows how to create a new document and print the text “Hello World” using a TrueType font.


B4X:
// Create a document and add a page to it
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage( page );

// Create a new font object by loading a TrueType font into the document
PDFont font = PDTrueTypeFont.loadTTF(document, "Arial.ttf");

// Start a new content stream which will "hold" the to be created content
PDPageContentStream contentStream = new PDPageContentStream(document, page);

// Define a text content stream using the selected font, moving the cursor and drawing the text "Hello World"
contentStream.beginText();
contentStream.setFont( font, 12 );
contentStream.moveTextPositionByAmount( 100, 700 );
contentStream.drawString( "Hello World" );
contentStream.endText();

// Make sure that the content stream is closed:
contentStream.close();

// Save the results and ensure that the document is properly closed:
document.save( "Hello World.pdf");
document.close();
 

spsp

Member
Licensed User
i cant print arabic characters
defualt fonts dont support arabic characters
but i find this from pdfbox site

Hello World Using a TrueType Font
This small sample shows how to create a new document and print the text “Hello World” using a TrueType font.


B4X:
// Create a document and add a page to it
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage( page );

// Create a new font object by loading a TrueType font into the document
PDFont font = PDTrueTypeFont.loadTTF(document, "Arial.ttf");

// Start a new content stream which will "hold" the to be created content
PDPageContentStream contentStream = new PDPageContentStream(document, page);

// Define a text content stream using the selected font, moving the cursor and drawing the text "Hello World"
contentStream.beginText();
contentStream.setFont( font, 12 );
contentStream.moveTextPositionByAmount( 100, 700 );
contentStream.drawString( "Hello World" );
contentStream.endText();

// Make sure that the content stream is closed:
contentStream.close();

// Save the results and ensure that the document is properly closed:
document.save( "Hello World.pdf");
document.close();
Hello,

Post #1 is updated with loadFont method. In the third example, the tahoma font is loaded from file and used on page 2 to display arabic characters

spsp
 
Last edited:

behnam_tr

Member
thanks man
its great
but still one problem is not solved

characters are disconnected and must Right_to_left

ok : "صباح"
not ok : "ح ا ب ص"

same problem in stackoverflow.com

 

micro

Well-Known Member
Licensed User
I use B4J 8.31, java jdk 11.0.1

Have some information in the log ?
I had imagined that maybe it was a different version of java
My java version is jdk 8.0212
in the log no information and no errors
 

micro

Well-Known Member
Licensed User
Could you download the project again in post #1 and try again. I have changed sub name with underscore for callsub in realease obfuscated mode.

spsp
downloaded
but nothing changes, same problem
 

micro

Well-Known Member
Licensed User
it's a pc problem
on other pc it works well also if the version of java installed is the same :rolleyes:
 

spsp

Member
Licensed User
Hello,

Sorry for the late answer.

I've update the post #1

Take a look at 'Example 3', page 2.

You need to download icu4j-67_1.jar and copy it in your additionnal library folder

in the main module, you need to add
main module:
    #AdditionalJar: icu4j-67_1.jar
when you want to draw text, you need to call "bidi" method from pdfBoxcreator
call bidi method:
    s=pdfcreator.bidi("صباحا")
Tell me if it's OK

SPSP


thanks man
its great
but still one problem is not solved

characters are disconnected and must Right_to_left

ok : "صباح"
not ok : "ح ا ب ص"

same problem in stackoverflow.com

 

moore_it

Active Member
Licensed User
Hi spsp very good work.

I need to display the text in an delimited field and i need to set multiline if necessary (long text).

It's possible ?

Thanks
 
Top