Android Question Android Printing Framework

luke2012

Well-Known Member
Licensed User
Longtime 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
Longtime User
It should be quite easy to create a library for this code. You can also use JavaObject to run this code (though it is a bit more cumbersome). It will only work with Android 4.4+.

So I can create a B4A library using the java object?
I'm not familiar with java object but I can follow the tuturial available...
 
Upvote 0

luke2012

Well-Known Member
Licensed User
Longtime User
http://www.b4x.com/search?query=JavaObject

You do not need to create a library to use JavaObject. However it will be easier to create a small library with the code.

I'm trying to arrange the code using JavaObject (but with several doubt :)

B4X:
sub createWebPrintJob (webView as object)
    Dim jPrintManager As JavaObject:jPrintManager.InitializeStatic("android.print.PrintManager")
    Dim jPrintService As JavaObject = jPrintManager.RunMethod("getSystemService", Context.PRINT_SERVICE)
   
    Dim jPrintAdapter As JavaObject = webView.RunMethod("createPrintDocumentAdapter", null)
    dim jobName as String = getString(R.string.app_name) + " Document"; ' ?

    dim printJob as JavaObject:jPrintManager.RunMethod("print", jobName)
    PrintJob printJob = printManager.print(jobName, jPrintAdapter, new PrintAttributes.Builder().build());
end sub

@Erel Please, could you help me to write the code (is the first time that I'm trying a porting from java to B4A with JavaObject)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try 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
 
Upvote 0

luke2012

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

@Erel I confirm you that it works! Thanks!
Is it possible to configure the print service in order to get the print parameters (ex. default printer and so on..) in background without show the UI to the user ?
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
Just one question about that code posted above, using a webview works like a charm, to send a file (jpg, pdf, etc, etc) I believe it takes a InputStream, so, can that code be modified to do that?, Java is not my forte.

Thanks.
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
I rather wait for you too see if there's a simple solution, I believe there is, the CloudPrint app takes an input stream to send files, and this is the same just integrated into the OS.
 
Upvote 0

luke2012

Well-Known Member
Licensed User
Longtime User
@Erel I confirm you that it works! Thanks!
Is it possible to configure the print service in order to get the print parameters (ex. default printer and so on..) in background without show the UI to the user ?

Hi @Erel.

The Good:
I'm testing the code with a sample app (using a webview) and it works fine (handling the above code within Main Activity).

The Bad:
I'm integrating the tested code within an existing app but within an activity different from the Main Activity.
The print UI window appear but than is closed automatically without let the user to start the print job and the activity is closed and the activity_resume event of the main activity is raised.

I'm investigating about this issue.
Do you confirm that calling the code within a different activity instead of Main activity the issue is confirmed?

Thanks in advance.
 
Upvote 0

luke2012

Well-Known Member
Licensed User
Longtime User
This code should work with any activity.

I'll check and let you k
As Erel wrote: "It will only work with Android 4.4+".
I think that many devices are excluded.
(there are statistics on WEB about this)

I understood that the print feature run on Android 4.4+ using the HP plugin and on previous version with Google Print Cloud. Right ?
 
Upvote 0

luke2012

Well-Known Member
Licensed User
Longtime User
The PrintManager API is only available on Android 4.4+: http://developer.android.com/reference/android/print/PrintManager.html

@Erel in order to let print also previous version of android, it's possible to use Google cloud print ?

UPDATE:

I found a Google document that explain the steps to integrate the Google Cloud Print for run the print feature on Android 4.3 and below.
In few words, the solution proposed by Google need two files : print_dialog.xml and PrintDialogActivity.java
The UI is raised using an intent.

Link : https://developers.google.com/cloud-print/docs/android
 
Last edited:
Upvote 0
Top