Example usage for android.webkit WebView createPrintDocumentAdapter

List of usage examples for android.webkit WebView createPrintDocumentAdapter

Introduction

In this page you can find the example usage for android.webkit WebView createPrintDocumentAdapter.

Prototype

@Deprecated
public PrintDocumentAdapter createPrintDocumentAdapter() 

Source Link

Usage

From source file:jp.mydns.sys1yagi.android.printingframeworksample.html.HtmlPrintActivity.java

private void printHtml(String fileName, WebView webView) {
    if (PrintHelper.systemSupportsPrint()) {
        PrintDocumentAdapter adapter = webView.createPrintDocumentAdapter();
        PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
        printManager.print(fileName, adapter, null);
    } else {//from w w  w. j  a va 2s .c  o  m
        Toast.makeText(this, "???????????", Toast.LENGTH_SHORT)
                .show();
    }
}

From source file:com.commonsware.android.print.MainActivity.java

private WebView prepPrintWebView(final String name) {
    WebView result = getWebView();//w  w w .j  a  va2  s.com

    result.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            print(name, view.createPrintDocumentAdapter(), new PrintAttributes.Builder().build());
        }
    });

    return (result);
}

From source file:net.olejon.mdapp.MyTools.java

public void printDocument(WebView webView, String title) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        PrintManager printManager = (PrintManager) mContext.getSystemService(Context.PRINT_SERVICE);

        //noinspection deprecation
        PrintDocumentAdapter printDocumentAdapter = webView.createPrintDocumentAdapter();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
            printDocumentAdapter = webView.createPrintDocumentAdapter(title);

        String documentName = mContext.getString(R.string.project_name) + " - " + title;

        PrintJob printJob = printManager.print(documentName, printDocumentAdapter,
                new PrintAttributes.Builder().build());

        List<PrintJob> printJobs = printManager.getPrintJobs();

        printJobs.add(printJob);/*from www. j  a  va 2s .  c o  m*/
    } else {
        showToast(mContext.getString(R.string.mytools_printing_not_supported), 1);
    }
}

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

/**
 * Creates the web view client which sets the print document.
 *
 * @param props/*from  ww w. j av a 2  s. 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;
        }
    });
}

From source file:com.farmerbb.notepad.activity.MainActivity.java

@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.KITKAT)//from   w ww.  j a  v  a 2s. c  om
private void createWebPrintJob(WebView webView) {
    // Get a PrintManager instance
    PrintManager printManager = (PrintManager) getSystemService(PRINT_SERVICE);

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

    // Create a print job with name and adapter instance
    String jobName = getString(R.string.document, getString(R.string.app_name));
    printManager.print(jobName, printAdapter, new PrintAttributes.Builder().build());
}

From source file:de.dreier.mytargets.features.scoreboard.ScoreboardActivity.java

@TargetApi(Build.VERSION_CODES.KITKAT)
private void print() {
    // Get a print adapter instance
    final WebView webViewPrint = new WebView(this);
    webViewPrint.setVisibility(View.INVISIBLE);
    ViewGroup.LayoutParams p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    webViewPrint.setLayoutParams(p);//  w  w  w .java 2 s.  com
    ((ViewGroup) binding.getRoot()).addView(webViewPrint);

    new AsyncTask<Void, Void, String>() {

        @Override
        protected String doInBackground(Void... params) {
            return HtmlUtils.getScoreboard(mTraining, mRound, ScoreboardConfiguration.fromPrintSettings());
        }

        @SuppressWarnings("deprecation")
        @Override
        protected void onPostExecute(String s) {
            webViewPrint.loadDataWithBaseURL("file:///android_asset/", s, "text/html", "UTF-8", "");
            PrintDocumentAdapter printAdapter = webViewPrint.createPrintDocumentAdapter();

            // Create a print job with name and adapter instance
            PrintManager printManager = (PrintManager) getSystemService(PRINT_SERVICE);
            String jobName = getString(R.string.scoreboard) + " Document";
            printManager.print(jobName, printAdapter, new PrintAttributes.Builder().build());
        }
    }.execute();
}