B4J Question [Solved] How to set printer resolution using - B4j Print JavaFX8

Peter Simpson

Expert
Licensed User
Longtime User
Hello,
I'm using Stevel05 excellent library but I've come across an issue. I need to set a particular printer resolution to the following

B4X:
PrinterResolution(120, 144, ResolutionSyntax.DPI)

Other people on the net using javafx had the same issues and fixed it with the above line of code.

I tried using resolution in the Print library but I'm getting absolutely nowhere when it comes to implementing it correctly...

Laserjet and Injet it works perfect, but this is needed for a carbon copy printer.

Thank you...
 

stevel05

Expert
Licensed User
Longtime User
Hi Peter,

I hadn't added it to the library simply because it isn't documented as part of the API in java 8 and I didn't know about it.

It appears in the documentation for Java 9 and I have tried it in Java 8 and it seems to be available as it didn't crash. I don't have a printer to try it on but you can create the following two classes and try it out.

Class PrinterResolution as a Standard Class:

B4X:
#IgnoreWarnings:12
'Class Module
Sub Class_Globals
    'Private fx As JFX ' Uncomment if required. For B4j only

    Private TJO As JavaObject
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    'Initialize the static sub so that field names will be updated.  If you don't use the standard naming conventions you will need to change the class name
End Sub

'Construct a new printer resolution attribute from the given items.
Public Sub Create(CrossFeedResolution As Int, FeedResolution As Int, Units As Int)
    'If a JavaObject has been passed, you may need to create it here and remove the parameter
    TJO.InitializeNewInstance("javax.print.attribute.standard.PrinterResolution",Array As Object(CrossFeedResolution, FeedResolution, Units))
End Sub

Public Sub AsJavaObject As JavaObject
    Return TJO
End Sub
'Get the name of the category of which this attribute value is an instance.
Public Sub GetName As String
    Return TJO.RunMethod("getName",Null)
End Sub
'Returns the wrapped object as Object
Public Sub GetObject As Object
    Return TJO
End Sub

'Comment if not needed
'Set the underlying Object, must be of correct type
Public Sub SetObject(Obj As Object)
    TJO = Obj
End Sub

PrinterResolution_Static As a Static Class

B4X:
'Code Module
Sub Process_Globals
    'Private fx As JFX ' Uncomment if required. For B4j only
End Sub

'Value to indicate units of dots per centimeter (dpcm).
Public Sub DPCM As Int
    Dim TJO As JavaObject
    TJO.InitializeStatic("javax.print.attribute.standard.PrinterResolution")
    Return TJO.GetField("DPCM")
End Sub

'Value to indicate units of dots per inch (dpi).
Public Sub DPI As Int
    Dim TJO As JavaObject
    TJO.InitializeStatic("javax.print.attribute.standard.PrinterResolution")
    Return TJO.GetField("DPI")
End Sub

'Construct a new printer resolution attribute from the given items.
Public Sub NewPrinterResolution(CrossFeedResolution As Int, FeedResolution As Int, Units As Int) As PrinterResolution
    Dim TObj As PrinterResolution
    TObj.Initialize
    TObj.Create(CrossFeedResolution,FeedResolution,Units)
    Return TObj
End Sub

Then you can create a new printer resolution object like this:
B4X:
Dim PR As PrinterResolution = PrinterResolution_Static.NewPrinterResolution(120,144,PrinterResolution_Static.DPI)

I'm not sure how to apply it, if it doesn't work as expected, please post a link to the sample code you found and I'll take a look.

The PR Object is wrapped, so you will need to use PR.GetObject to apply it. Once I know how it is applied, i will amend the library to unwrap it automatically.

Hopefully that will do the trick.
 
Last edited:
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
Hi Peter,

I hadn't added it to the library simply because it isn't documented as part of the API in java 8 and I didn't know about it.

It appears in the documentation for Java 9 and I have tried it in Java 8 and it seems to be available as it didn't crash. I don't have a printer to try it on but you can create the following two classes and try it out.

Class PrinterResolution as a Standard Class:

B4X:
#IgnoreWarnings:12
'Class Module
Sub Class_Globals
    'Private fx As JFX ' Uncomment if required. For B4j only

    Private TJO As JavaObject
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    'Initialize the static sub so that field names will be updated.  If you don't use the standard naming conventions you will need to change the class name
End Sub

'Construct a new printer resolution attribute from the given items.
Public Sub Create(CrossFeedResolution As Int, FeedResolution As Int, Units As Int)
    'If a JavaObject has been passed, you may need to create it here and remove the parameter
    TJO.InitializeNewInstance("javax.print.attribute.standard.PrinterResolution",Array As Object(CrossFeedResolution, FeedResolution, Units))
End Sub

Public Sub AsJavaObject As JavaObject
    Return TJO
End Sub
'Get the name of the category of which this attribute value is an instance.
Public Sub GetName As String
    Return TJO.RunMethod("getName",Null)
End Sub
'Returns the wrapped object as Object
Public Sub GetObject As Object
    Return TJO
End Sub

'Comment if not needed
'Set the underlying Object, must be of correct type
Public Sub SetObject(Obj As Object)
    TJO = Obj
End Sub

PrinterResolution_Static As a Static Class

B4X:
'Code Module
Sub Process_Globals
    'Private fx As JFX ' Uncomment if required. For B4j only
End Sub

'Value to indicate units of dots per centimeter (dpcm).
Public Sub DPCM As Int
    Dim TJO As JavaObject
    TJO.InitializeStatic("javax.print.attribute.standard.PrinterResolution")
    Return TJO.GetField("DPCM")
End Sub

'Value to indicate units of dots per inch (dpi).
Public Sub DPI As Int
    Dim TJO As JavaObject
    TJO.InitializeStatic("javax.print.attribute.standard.PrinterResolution")
    Return TJO.GetField("DPI")
End Sub

'Construct a new printer resolution attribute from the given items.
Public Sub NewPrinterResolution(CrossFeedResolution As Int, FeedResolution As Int, Units As Int) As PrinterResolution
    Dim TObj As PrinterResolution
    TObj.Initialize
    TObj.Create(CrossFeedResolution,FeedResolution,Units)
    Return TObj
End Sub

Then you can create a new printer resolution object like this:
B4X:
Dim PR As PrinterResolution = PrinterResolution_Static.NewPrinterResolution(120,144,PrinterResolution_Static.DPI)

I'm not sure how to apply it, if it doesn't work as expected, please post a link to the sample code you found and I'll take a look.

The PR Object is wrapped, so you will need to use PR.GetObject to apply it. Once I know how it is applied, i will amend the library to unwrap it automatically.

Hopefully that will do the trick.

Hello Steve,
Thank you for your response, I'll try it out in the morning and I'll let you know how it goes.

I decided to write a warehouse and stock delivery system for a client using B4J. It's gone extremely smoothy, well apart from one printer out of load.

Thank you again.

Peter...
 
Last edited:
Upvote 0
Top