Android Question Android Printing Framework

luke2012

Well-Known Member
Licensed User
Hi to all,
my app's users are asking a direct print feature in order to print a txt that generate my app.
I think that this feature could be very interesting for many apps.

I have found this document that talk about official "Android Printing Framework" :

http://www.techotopia.com/index.php/Printing_with_the_Android_Printing_Framework

More in detail I think there are two interesting section :

  • Creating and Printing HTML Content (Easy)
    • To enable HTML printing, the WebView class has been extended in Android 4.4 to include support for printing with minimal coding requirements.
  • Printing a Custom Document (more complicated)
@Erel it is possible to integrate this feature within B4A (wrapper library?)
If is not possible, can we use the "Simple Library Compiler" to use the java code show in the attached article ?

P.S.
For the Web printing method the sample java code is the following :

B4X:
private void createWebPrintJob(WebView webView) {

PrintManager printManager = (PrintManager) this
.getSystemService(Context.PRINT_SERVICE);

PrintDocumentAdapter printAdapter =
webView.createPrintDocumentAdapter();
String jobName = getString(R.string.app_name) + " Document";

PrintJob printJob = printManager.print(jobName, printAdapter,
new PrintAttributes.Builder().build());
}

* this function should be called within the "PageFinished" event of a webview.
 
Last edited:

luke2012

Well-Known Member
Licensed User
I never worked with this library.

@Erel probably there was I missunderstanding. I'm don't talking about CloudPrint - Library but the new print framework.

I'm talk about this :

B4X:
Sub CreateWebPrintJob (wv As WebView)
    DIm jwv As JavaObject = wv
    Dim jPrintManager As JavaObject = jwv.RunMethodJO("getContext", null).RunMethod("getSystemService", Array As Object("print"))
   
    Dim jPrintAdapter As JavaObject = jwv .RunMethod("createPrintDocumentAdapter", null)
    dim jobName as String = "Document"
   jPrintManager.RunMethod("print", Array As Object(jobName, jPrintAdapter, null))
End Sub

The print window by default is set to ISO A4 (paper format).
It's possibile to set another default (supported) value like ISO A6?
 
Upvote 0

Erel

Administrator
Staff member
Licensed User
Try this:
B4X:
Sub CreatePrinterAttributes As JavaObject
   Dim builder As JavaObject
   builder.InitializeNewInstance("android.print.PrintAttributes.Builder", Null)
   Dim mediaSize As JavaObject
   mediaSize = mediaSize.InitializeStatic("android.print.PrintAttributes.MediaSize").GetField("ISO_A6")
   Return builder.RunMethod("build", Null)
End Sub

Sub CreateWebPrintJob (wv As WebView)
    DIm jwv As JavaObject = wv
    Dim jPrintManager As JavaObject = jwv.RunMethodJO("getContext", null).RunMethod("getSystemService", Array As Object("print"))
   
    Dim jPrintAdapter As JavaObject = jwv .RunMethod("createPrintDocumentAdapter", null)
    dim jobName as String = "Document"
   jPrintManager.RunMethod("print", Array As Object(jobName, jPrintAdapter, CreatePrinterAttributes ))
End Sub
 
Upvote 0

luke2012

Well-Known Member
Licensed User
Try this:
B4X:
Sub CreatePrinterAttributes As JavaObject
   Dim builder As JavaObject
   builder.InitializeNewInstance("android.print.PrintAttributes.Builder", Null)
   Dim mediaSize As JavaObject
   mediaSize = mediaSize.InitializeStatic("android.print.PrintAttributes.MediaSize").GetField("ISO_A6")
   Return builder.RunMethod("build", Null)
End Sub

Sub CreateWebPrintJob (wv As WebView)
    DIm jwv As JavaObject = wv
    Dim jPrintManager As JavaObject = jwv.RunMethodJO("getContext", null).RunMethod("getSystemService", Array As Object("print"))
  
    Dim jPrintAdapter As JavaObject = jwv .RunMethod("createPrintDocumentAdapter", null)
    dim jobName as String = "Document"
   jPrintManager.RunMethod("print", Array As Object(jobName, jPrintAdapter, CreatePrinterAttributes ))
End Sub

Thanks @Erel!
I'll try your code.
Only a question :

InitializeNewInstance = instantiate a new java object of the class PrintAttributes.Builder.
InitializeStatic = which is the purpose of the InitializeStatic method ?
 
Upvote 0

luke2012

Well-Known Member
Licensed User
InitializeStatic is used when you want to get a static field or run a static method.
Try this:
B4X:
Sub CreatePrinterAttributes As JavaObject
   Dim builder As JavaObject
   builder.InitializeNewInstance("android.print.PrintAttributes.Builder", Null)
   Dim mediaSize As JavaObject
   mediaSize = mediaSize.InitializeStatic("android.print.PrintAttributes.MediaSize").GetField("ISO_A6")
   Return builder.RunMethod("build", Null)
End Sub

Sub CreateWebPrintJob (wv As WebView)
    DIm jwv As JavaObject = wv
    Dim jPrintManager As JavaObject = jwv.RunMethodJO("getContext", null).RunMethod("getSystemService", Array As Object("print"))
 
    Dim jPrintAdapter As JavaObject = jwv .RunMethod("createPrintDocumentAdapter", null)
    dim jobName as String = "Document"
   jPrintManager.RunMethod("print", Array As Object(jobName, jPrintAdapter, CreatePrinterAttributes ))
End Sub

Hi @Erel,
I tried your code the good is that is no errors the bad is the paper size spinner that is empty.

Normally in the paper size spinner I have this values :

A4
4x8 inch (instead of A6)
and so on ...
 
Last edited:
Upvote 0

scsjc

Well-Known Member
Licensed User
HELLO,
IS POSSIBLE ROTATE PAGE ??? >>> ORIENTATION PORTRAIT ... TO PRINT ??? WITH THIS METODE?

THANKS.
 
Upvote 0

Bryan

Member
Licensed User
I implemented this and it works very nicely on my Galaxy S5 and a Note 8. The only question I have is how to test if device is capable of printing? What happens if you run this code and the device does not have a printing capability? I targeted my app to run on devices running Android 4.0 and higher.
 
Upvote 0
Top