Example usage for android.print PrintAttributes.Builder setMinMargins

List of usage examples for android.print PrintAttributes.Builder setMinMargins

Introduction

In this page you can find the example usage for android.print PrintAttributes.Builder setMinMargins.

Prototype

public void setMinMargins(Margins margins) 

Source Link

Document

Sets the minimal margins.

Usage

From source file:de.appplant.cordova.plugin.printer.Printer.java

/**
 * Creates the web view client which sets the print document.
 *
 * @param props/* ww  w .ja v  a  2s . com*/
 *      The JSON object with the containing page properties
 */
private void setWebViewClient(JSONObject props) {
    final String docName = props.optString("name", DEFAULT_DOC_NAME);
    final boolean landscape = props.optBoolean("landscape", false);
    final boolean graystyle = props.optBoolean("graystyle", false);

    view.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
        }

        @Override
        public void onPageFinished(WebView webView, String url) {
            // Get a PrintManager instance
            PrintManager printManager = (PrintManager) cordova.getActivity()
                    .getSystemService(Context.PRINT_SERVICE);

            // Get a print adapter instance
            PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();

            // Get a print builder instance
            PrintAttributes.Builder builder = new PrintAttributes.Builder();

            // The page does itself set its own margins
            builder.setMinMargins(PrintAttributes.Margins.NO_MARGINS);

            builder.setColorMode(
                    graystyle ? PrintAttributes.COLOR_MODE_MONOCHROME : PrintAttributes.COLOR_MODE_COLOR);

            builder.setMediaSize(landscape ? PrintAttributes.MediaSize.UNKNOWN_LANDSCAPE
                    : PrintAttributes.MediaSize.UNKNOWN_PORTRAIT);

            // Create a print job with name and adapter instance
            PrintJob job = printManager.print(docName, printAdapter, builder.build());

            invokeCallbackOnceCompletedOrCanceled(job);

            view = null;
        }
    });
}