B4J Library jPrint V 1.61

Here is a library I am currently working on.
It allows you to print from your B4J program.

At present it can print TextArea, TextField, TableView and text type files.

I am open to suggestions as to what to add, and general feedback on how it is working for you.

A word of warning, don't try to print an HTML file, unless you have an unlimited supply of paper, it wants to print basically 1 character per page. I have tested with .txt and .js and they seem to work ok.

see jPrintTest for how to call the routines.

version V 1.10

1) Added two fields to jPrint - jPrint.startTabRow and jPrint.endTabRow, these control which rows are printed from the table.
eg, printer.startTabRow = 10
printer.endTabRow = 20
will print out rows 10 to 20 when ??.print(table) is called.

if not set entire table printed, they can be set separately ie printer.startTabRow=10 will print from row 10 onwards etc.
2) Can now print the following table datatypes String, Label, Int, Float and Double.

version V1.20
Can now specify which printer when printing a file.

Version V 1.30
Added limit checks on start and end rows. - rows start and end are inclusive now.
Added datatype boolean for tables.
Added header for table print - prints 'Table : tableID' as header for each page.

Version V 1.40
Added image to tableview - it can't print it but wont cause print to fail.(just prints "--Image--" in cell.)
Added ability to print Strings - printer.print("hello")) etc. probably best used in conjunction with a StringBuilder as once string is sent, will print.
Workaround for fileprint not ejecting page. - use printer.print(" ") to same printer, as it forces form feed after print.


Version V 1.50
Added printFile2(filename,header,font)
Added word wrap on long lines.
printFile2 written to use different method to print a file. Use printer dialog to change margins etc.
see jPrintTest(v1.50) for printfile2 parameters.

Version V 1.60
This has a new routine printer.printFileNoThreads(filename,header,font)
It does not use a separate thread to run the file print. That seemed to be the cause of the problem on the HP laserjet printer failing to print.(if only this were true)
printFile(...) does not print headers or allow choice of font.
printFile2(...) allows headers and font selection and prints on a separate thread.
printFileNoThreads(...) allows headers and font choice but will hang the B4J program while printing.

Version V 1.61
Minor change to printFileNoThreads(...) setting header to "" effectively removes headers and footers from output print. (Still trying to find cause of HP laser failing to print a page)
It appears the header/footer was the problem on the HP.
If you don't get a print out from a file try the above method printFileNoThreads(...) with header set to "".
 

Attachments

  • jPrintLib(V1.50 Beta).zip
    10.4 KB · Views: 659
  • jPrintTest(v1.50).zip
    2.2 KB · Views: 907
  • jPrintLib V1.60.zip
    10.6 KB · Views: 683
  • jPrintLibrary(1.61).zip
    10.7 KB · Views: 1,166
  • jPrintSource(1.61).zip
    3.2 KB · Views: 996
Last edited:

William Hunter

Active Member
Licensed User
Longtime User
@William - it does seem odd as I print to an HP printer (photosmart) and the pages all come out. Did you try sending printer.print(" ") to the printer to see if it forces the page out ?
Hello Daestrum – When I attempt to print a file the printer is activated, and would appear to start the printing process. But then, paper is not fed into the printer, and the printer simply goes to rest. The file to print, named “java printing”, is sent to the queue and has a file size of 0 bytes. This file remains listed in the queue, with the message “printing”, until I cancel the print job.

The printer does not seem to see the file “java printing” as being printable, otherwise even though a 0 bytes file, I think paper would have fed through the printer. I wish that I could offer more assistance, but hope this helps.

Regards
 

stevel05

Expert
Licensed User
Longtime User
Just printed the file Daestrum supplied on an Epson Inkjet with no problems. I don't have access to a laser printer to try it.
 

William Hunter

Active Member
Licensed User
Longtime User
@ Daestrum – After reading of Steve105’s success with his Epson inkjet I tried printing to a HP Officejet All-in-One 6310 (Inkjet) using printer.print(" ") to force a form feed after print. That worked and the page was printed OK. This particular device gives me very poor print quality, so I can’t comment on how well the output looked. That’s the reason why I now use the laser. There must be some difference between inkjet and laser drivers that stalls jPrint output to a laser printer.

You have made some good headway. Now, if the next version of jPrint produces good printing from my HP Laser, I will be a very happy B4J user. I have no doubt you will get there. :cool:

Best regards
 

magoandroid

Member
Licensed User
Longtime User
Just print the file Daestrum provided on a laser AFICIO CL400DN (directly) and SAMSUNG ML-3471DN (network) without problems.

Regards
MAgo
 

Daestrum

Expert
Licensed User
Longtime User
@William Hunter here is a library for you to try. (personalised :) )
Change the printer.printFile(......)
to
printer.printFile2(.....)
in your test print B4J code and let me know if it's better, as in it actually prints something on your HP.
 

Attachments

  • jPrintLib (William).zip
    10.3 KB · Views: 691

Daestrum

Expert
Licensed User
Longtime User
Dear All,
How to set Font name of Text file ? I would like set text file to mono space such as droidsans mono or etc.
Best Regard.

I am currently adding font selection and headers/footers to the library for file print, along with word wrap.
 

Theera

Well-Known Member
Licensed User
Longtime User

Daestrum

Expert
Licensed User
Longtime User
V1.50 Beta available
 

William Hunter

Active Member
Licensed User
Longtime User
@ Daestrum - I tried printing to the HP LaserJet 1018 with both your jPrintLib (William) and your new jPrintLib(V1.50 Beta).
In both cases I received the following in the B4J log:
B4X:
Program started.
Line count = 8
Char count = 112
Font req :tahoma-italic-20  Actual :tahoma
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x07783a16, pid=2648, tid=6128
#
# JRE version: Java(TM) SE Runtime Environment (7.0_45-b18) (build 1.7.0_45-b18)
# Java VM: Java HotSpot(TM) Client VM (24.45-b08 mixed mode, sharing windows-x86 )
# Problematic frame:
# C  [sdhp1020.dll+0x33a16]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# C:\Users\BILLSA~1\Desktop\QUICKN~2\Objects\hs_err_pid2648.log
#
# If you would like to submit a bug report, please visit:
#  http://bugreport.sun.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

There was also a Java log file produced which I have attached. I had to change the file name extension from log to txt, otherwise it would not upload.

Best regards

Edit: The test was made from an app created with B4J.
Edit2: uploaded second Java log.
Edit3: uploaded third Java log
Edit4: uploaded fourth Java log - created while testing with jPrintTest(v1.50)
 

Attachments

  • hs_err_pid2648.txt
    26.8 KB · Views: 450
  • hs_err_pid7108.txt
    26.8 KB · Views: 377
  • hs_err_pid5672.txt
    26.8 KB · Views: 390
  • hs_err_pid7744.txt
    26.6 KB · Views: 395
Last edited:

Daestrum

Expert
Licensed User
Longtime User
Will take me a little while to digest that error log, please bear with me.
 

alienhunter

Active Member
Licensed User
Longtime User
Thanks for the lib ,
it is working with a Zebra Label Printer :) (see zebra 3.jpg) too
http://www.zebra.com/us/en.html

:(
the problem is looks like if you have 3 lines in the text file it misses the last one
with "printer.printFileNoThreads( "C:\temp\text.txt","JOBNR :5555" ,"tahoma-italic-12")"
any clues ?

it works also on Laser Printer , but again the 3-rd row in the Text is missing

how can i always use the landscape and margins 0.0.0.0 ? (see zebra2.jpg), is there a option ?


thanks AH
 

Attachments

  • zebra1.jpg
    zebra1.jpg
    40.9 KB · Views: 613
  • zebra2.jpg
    zebra2.jpg
    43.1 KB · Views: 600
  • zebra3.jpg
    zebra3.jpg
    60.1 KB · Views: 624
  • text.txt
    105 bytes · Views: 442

Daestrum

Expert
Licensed User
Longtime User
Sorry for delay in replying, been fooling around with java8 printing. Seems a lots simpler, but , it just wants to print what is laid out on the form.
Been working on a report designer, so you can set out the items where you want them printed on the page. Still a work in progress on the designer, but the actual printing is a doddle, 5 lines of code :)
B4X:
    Dim pj,pr,ps As JavaObject
    pj = pr.InitializeStatic("javafx.print.PrinterJob").RunMethodJO("createPrinterJob",Null)
    pj.RunMethodJO("showPrintDialog",Array As Object(Null))
    pj.RunMethodJO("showPageSetupDialog",Array As Object(Null))
    pj.RunMethodJO("printPage",Array As Object(design.RootPane))
    pj.RunMethodJO("endJob",Null)

How nice is that ?
The printPage line takes one argument - what node you want to print. Be that a single node or the rootpane, it will print all child nodes of the node you supply.
 
Last edited:

jroriz

Active Member
Licensed User
Longtime User
Here is a library I am currently working on.
It allows you to print from your B4J program.

At present it can print TextArea, TextField, TableView and text type files.

I am open to suggestions as to what to add, and general feedback on how it is working for you.

A word of warning, don't try to print an HTML file, unless you have an unlimited supply of paper, it wants to print basically 1 character per page. I have tested with .txt and .js and they seem to work ok.

see jPrintTest for how to call the routines.

version V 1.10

1) Added two fields to jPrint - jPrint.startTabRow and jPrint.endTabRow, these control which rows are printed from the table.
eg, printer.startTabRow = 10
printer.endTabRow = 20
will print out rows 10 to 20 when ??.print(table) is called.

if not set entire table printed, they can be set separately ie printer.startTabRow=10 will print from row 10 onwards etc.
2) Can now print the following table datatypes String, Label, Int, Float and Double.

version V1.20
Can now specify which printer when printing a file.

Version V 1.30
Added limit checks on start and end rows. - rows start and end are inclusive now.
Added datatype boolean for tables.
Added header for table print - prints 'Table : tableID' as header for each page.

Version V 1.40
Added image to tableview - it can't print it but wont cause print to fail.(just prints "--Image--" in cell.)
Added ability to print Strings - printer.print("hello")) etc. probably best used in conjunction with a StringBuilder as once string is sent, will print.
Workaround for fileprint not ejecting page. - use printer.print(" ") to same printer, as it forces form feed after print.


Version V 1.50
Added printFile2(filename,header,font)
Added word wrap on long lines.
printFile2 written to use different method to print a file. Use printer dialog to change margins etc.
see jPrintTest(v1.50) for printfile2 parameters.

Version V 1.60
This has a new routine printer.printFileNoThreads(filename,header,font)
It does not use a separate thread to run the file print. That seemed to be the cause of the problem on the HP laserjet printer failing to print.(if only this were true)
printFile(...) does not print headers or allow choice of font.
printFile2(...) allows headers and font selection and prints on a separate thread.
printFileNoThreads(...) allows headers and font choice but will hang the B4J program while printing.

Version V 1.61
Minor change to printFileNoThreads(...) setting header to "" effectively removes headers and footers from output print. (Still trying to find cause of HP laser failing to print a page)
It appears the header/footer was the problem on the HP.
If you don't get a print out from a file try the above method printFileNoThreads(...) with header set to "".

Hi.
How to select the destination printer?
 

rbghongade

Active Member
Licensed User
Longtime User
Hello ,
can anyone please give me a clear tableview print example with tableview created in the designer and not fxml?

B4X:
TableView1.SetColumns(Array As String("ID", "Value","#1","#2","#3"))
    'fill with data
    For i = 1 To 1000
        Dim row() As Object = CreateRow(Array As String("Item #" & NumberFormat(i, 3, 0), i,i*i,i+2,i+3))
        TableView1.Items.Add(row)
    Next
    
    Log(printer.print(TableView1))


This throws an exception:
B4X:
>>TableView<<
main._appstart (java line: 74)
java.lang.NullPointerException
    at jPrint.jPrint.print(jPrint.java:100)
    at b4j.example.main._appstart(main.java:74)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:77)
    at b4j.example.main.start(main.java:38)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    at java.lang.Thread.run(Thread.java:748)
 
Last edited:

rbghongade

Active Member
Licensed User
Longtime User
Friends please help me out, solution for the above mentioned problem is critical for my project.
 

stevel05

Expert
Licensed User
Longtime User
As a guess I would try putting a sleep(0) before printing to give the table time to finish updating. You may need a larger value.
May be better to start at 1000, if that works reduce it until it stops working.
 

rbghongade

Active Member
Licensed User
Longtime User
Dear stevel05,
Thanks for your solution but I get the same results! The null pointer exception!
 

stevel05

Expert
Licensed User
Longtime User
I don't know the library it was a guess based on the information you gave. If you provide an example working app that shows the problem, you would undoubtedly get a quicker, more accurate response.
 
Top