Example usage for android.print PrintManager print

List of usage examples for android.print PrintManager print

Introduction

In this page you can find the example usage for android.print PrintManager print.

Prototype

public @NonNull PrintJob print(@NonNull String printJobName, @NonNull PrintDocumentAdapter documentAdapter,
        @Nullable PrintAttributes attributes) 

Source Link

Document

Creates a print job for printing a PrintDocumentAdapter with default print attributes.

Usage

From source file:jp.mydns.sys1yagi.android.printingframeworksample.custom.CustomPrintActivity.java

private void printWithAdapter(String jobName, PrintDocumentAdapter adapter) {
    if (PrintHelper.systemSupportsPrint()) {
        PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
        printManager.print(jobName, adapter, null);
    } else {//from w w w  . ja  va2s  .  com
        Toast.makeText(this, "???????????", Toast.LENGTH_SHORT)
                .show();
    }
}

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.  jav a  2s .  co m*/
        Toast.makeText(this, "???????????", Toast.LENGTH_SHORT)
                .show();
    }
}

From source file:com.shinobicontrols.printshop.PrintDemoFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.fragment_print, container, false);

    // Wire up some button handlers
    rootView.findViewById(R.id.print_image_button).setOnClickListener(new View.OnClickListener() {
        @Override//from   w  ww  . j  ava 2s . co  m
        public void onClick(View v) {
            PrintHelper printHelper = new PrintHelper(getActivity());
            printHelper.setScaleMode(PrintHelper.SCALE_MODE_FIT);
            // Get the image
            Bitmap image = getImage();
            if (image != null) {
                // Send it to the print helper
                printHelper.printBitmap("PrintShop", image);
            }

        }
    });

    final ImageAndTextContainer imageAndTextContainer = this;

    rootView.findViewById(R.id.print_page_button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Create a PrintDocumentAdapter
            PrintShopPrintDocumentAdapter adapter = new PrintShopPrintDocumentAdapter(imageAndTextContainer,
                    getActivity());
            // Get the print manager from the context
            PrintManager printManager = (PrintManager) getActivity().getSystemService(Context.PRINT_SERVICE);
            // And print the document
            printManager.print("PrintShop", adapter, null);
        }
    });

    return rootView;
}

From source file:com.android.mail.ui.HelpFragment.java

@TargetApi(19)
private void print() {
    // pick a name for the print job we will create
    final String title = getActivity().getActionBar().getTitle().toString();
    final String jobName = getString(R.string.print_job_name, title);

    // ask the webview for a print adapter
    final PrintDocumentAdapter pda = mWebView.createPrintDocumentAdapter();

    // ask the print manager to print the contents of the webview using the job name
    final PrintManager pm = (PrintManager) getActivity().getSystemService(Context.PRINT_SERVICE);
    pm.print(jobName, pda, new PrintAttributes.Builder().build());
}

From source file:com.tct.mail.ui.HelpFragment.java

@TargetApi(19)
private void print() {
    // pick a name for the print job we will create
    final String title = getActivity().getActionBar().getTitle().toString();
    final String jobName = getString(R.string.print_job_name, title);

    // ask the webview for a print adapter
    final PrintDocumentAdapter pda = mWebView.createPrintDocumentAdapter();

    // ask the print manager to print the contents of the webview using the job name
    final PrintManager pm = (PrintManager) getActivity().getSystemService(Context.PRINT_SERVICE);
    //TS: rong-tang 2016-03-08 EMAIL BUGFIX-1139663 MOD_S
    try {//from   w w w . j av a  2 s.c o  m
        pm.print(jobName, pda, new PrintAttributes.Builder().build());
    } catch (RuntimeException e) {
        LogUtils.e(LogUtils.TAG, e, "happened exception when print");
    }
    //TS: rong-tang 2016-03-08 EMAIL BUGFIX-1139663 MOD_E
}

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);//from www.j  a va  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();
}

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);//  w ww. jav  a  2 s.  c om
    } else {
        showToast(mContext.getString(R.string.mytools_printing_not_supported), 1);
    }
}

From source file:de.treichels.hott.ui.android.MdlViewerActivity.java

/**
 * Create a PDF version of the document. Only support on Android 4.4 and newer.
 *//*w  ww  .j av  a  2  s .c  o  m*/
@SuppressWarnings("deprecation")
@TargetApi(19)
public void print() {
    final PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
    final String documentName = "HoTTMdlViewer - " + modelName;
    // deprecated method is needed for backward compatibility with Android 4.4 (api level 19)
    printManager.print(documentName, webView.createPrintDocumentAdapter(), null);
}

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

/**
 * Creates the web view client which sets the print document.
 *
 * @param props//from www. j av  a  2  s  . co  m
 *      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  a2  s .  c o m
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());
}