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

  • b4JPDF.zip
    162.5 KB · Views: 602
Last edited:

behnam_tr

Active Member
Licensed User
Longtime User
thanks

how set unicode to "utf-8" ???
how use other fonts like "tahoma" ??
 

micro

Well-Known Member
Licensed User
Longtime 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

Active Member
Licensed User
Longtime 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();
 

spsp

Active Member
Licensed User
Longtime 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

Active Member
Licensed User
Longtime User
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
Longtime 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
Longtime 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
Longtime User
it's a pc problem
on other pc it works well also if the version of java installed is the same :rolleyes:
 

spsp

Active Member
Licensed User
Longtime 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

Well-Known Member
Licensed User
Longtime 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
 

behnam_tr

Active Member
Licensed User
Longtime User
hi spss
problem with Arabic characters not solved completely

check attached
some characters like
"ی"
"گ"
"پ"
"ژ"
"چ"
"ک"

currect words are :
"گرگینه"
"پژمان"
"پنج"
"هشتادوپنج"
and ........
 

Attachments

  • pdfboxtest.png
    pdfboxtest.png
    62.3 KB · Views: 259
Last edited:

MarcTG

Active Member
Licensed User
Longtime User
Wow this looks great. I have 2 questions:
1- Can you create a pdf from a B4J layouts the same way it is done with JavaFX8
2- Can you then load internal pdfs and combine them with pages created in 1.

Thanks!
 

spsp

Active Member
Licensed User
Longtime User
Wow this looks great. I have 2 questions:
1- Can you create a pdf from a B4J layouts the same way it is done with JavaFX8
2- Can you then load internal pdfs and combine them with pages created in 1.

Thanks!
This class wraps only few methods from pdfbox, so :
1- No
2 -No

If you want to complete this class, you are welcome

spsp
 
Top