B4J Question Printing to a specific printer

Juan Marrero

Active Member
Licensed User
Longtime User
Hi. I can get a list of all installed printers using this instruction Printer_Static.GetAllPrinters. But all the examples I've found in the forums only use the default printer or the dialog. I want to print either the content of a text file or a list using jFX8Print. I tried to use Printer_Static.MappedPrinter instruction but it errors. If I use the dialog with the list it errors too. Not sure what I'm doing wrong. The error using dialog is:
B4X:
java.lang.RuntimeException: Method: printPage not matched.
The error using MappedPrinter is:
B4X:
java.lang.NullPointerException

Here is the code:
B4X:
     Dim orderList As List
     orderList.Initialize
     
     If ModMain.APP_LANGUAGE = "Spanish" Then
       orderList.add("Nombre: " & ord.CNameh)
       orderList.Add( "Teléfono: " & ord.PNumb)
       orderList.add("Fecha: " & ord.ODate & " " & ord.OTime)
     Else
       orderList.Add("Name: " & ord.CNameH)
       orderList.Add("Phone: " & ord.PNumb)
       orderList.Add("Date: " & ord.ODate & " " & ord.OTime)
     End If
     
     orderList.Add("---" & ord.OType & "---")
     orderList.Add(" ")
     
     For i = 0 To ordC.RowCount - 1
       Dim currItm As String = ""
       Dim currQty As Int
       
       currItm = rs.GetString("Item")
       currQty = rs.GetInt("Quantity")
       
       orderList.Add(currQty & " - " & currItm)
     Next
     
     File.WriteList(ModMain.APP_PATH, ModMain.CURR_DB_NAME & ".txt", orderList)
'     Dim writer As TextWriter
'     writer.Initialize(File.OpenOutput(ModMain.APP_PATH, ModMain.CURR_DB_NAME & ".txt", False))
'     
'     For j = 0 To orderList.Size - 1
'       writer.WriteLine(orderList.Get(j))
'     Next
'     
'     writer.close
     
     If ModMain.SELECTED_PRINTER <> "" Then
       Dim p As Printer = Printer_Static.MappedPrinter(ModMain.SELECTED_PRINTER)
       Dim pj As PrinterJob = PrinterJob_Static.CreatePrinterJob2(p)
       
       pj.PrintPage(orderList)
       pj.EndJob
     Else
       Dim pj As PrinterJob = PrinterJob_Static.CreatePrinterJob
       
       pj.ShowPageSetupDialog(Null)
      pj.ShowPrintDialog(Null)
      pj.PrintPage(orderList)
       pj.EndJob
     End If
 

stevel05

Expert
Licensed User
Longtime User
JFX8 printer module requires a Node to print, it will not print a list directly. You will need to load it into a Node, a listview or textview of some type. It's a while since I've used it, but if I remember correctly you may not need to display the Node on a page, it just has to be one for it to print.
 
Upvote 0

Juan Marrero

Active Member
Licensed User
Longtime User
stevel05 Your suggestion solved one of my issues. I was able to print filling a listview from my list (orderList) but only if i select the printer via dialog.
B4X:
     Dim listV as ListView
     listV.Initialize("LV")

     For j = 0 To orderList.Size - 1
          listV.Items.Add(orderList.Get(j))
     Next

     Dim pj As PrinterJob = PrinterJob_Static.CreatePrinterJob
      
     pj.ShowPageSetupDialog(Null)
     pj.ShowPrintDialog(Null)
     pj.PrintPage(listV)
     pj.EndJob
But this line returns a null value when I try to print to a specific printer.
B4X:
     Dim p As Printer = Printer_Static.MappedPrinter(ModMain.SELECTED_PRINTER)
Note: I used the following code to obtain all the installed printers.
B4X:
     Dim prList As List = Printer_Static.GetAllPrinters
Then I populate a combobox with all the values of prList. Then if I select any value from the combobox i assing that value to the variable ModMain.SELECTED_PRINTER. But because p is null the following line errors.
B4X:
     Dim pj As PrinterJob = PrinterJob_Static.CreatePrinterJob2(p)

I know I'm doing something wrong. Please enlighten me.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
This code works:

B4X:
Dim PrList As List = Printer_Static.GetAllPrinters
  
    For Each Pr As Printer In PrList
        Log(Pr.GetName)
        If Pr.GetName.StartsWith("Foxit") Then Exit 'Foxit is a PDF printer on my system
    Next
  
    Dim PJ As PrinterJob = PrinterJob_Static.CreatePrinterJob2(Pr)
    PJ.PrintPage(lblTest)

Without seeing your code, I can only guess really, but it seems that you are not passing a valid printer object to the CreatePrinterJob2 function.

If you are using the result of the combobox select as the printer, then it will fail as you will have populated the combobox with a string representation of the object rather than the object itself.

If this doesn't help, please post an example project so I don't have to reinvent it.
 
Upvote 0

Juan Marrero

Active Member
Licensed User
Longtime User
Here it is. If you leave the combobox in blank it shows the printer dialog which is working. If you select a printer then it shows a null error.
 

Attachments

  • PrintExample.zip
    1.6 KB · Views: 249
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Try this one.

I've created the prlist as a global variable so that the printer objects are available to the print subroutine. The index is -1 as you have added a blank entry at the beginning of the list.
 

Attachments

  • PrintExampleWorking.zip
    1.6 KB · Views: 297
Upvote 0

Juan Marrero

Active Member
Licensed User
Longtime User
Yep it worked!!!! It would be interesting though an example using the MappedPrinter function for the knowledge of us all.
Many many thanks stevel05!!!!!
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
The MappedPrinter Map is used by the library to return the Wrapped printer from the lower level unwrapped printer object returned by one call in the PrinterJob Module, it has no other useful purpose.
 
Upvote 0
Top