如何在Android打印类中更改默认打印选项 [英] How to change default print option in Android print class

查看:627
本文介绍了如何在Android打印类中更改默认打印选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android应用程序中编写了一个打印类,我想在旋转之前保存printAttribute并使用以前的printAttribute重新连接到打印机,但是当我传递oldPrintAttribute无效时,打印对话框仍显示默认选项,这是我的代码.

I write a print class in Android app, and I want to save the printAttribute before rotation and reconnect to the printer using the former printAttribute, but when I pass the oldPrintAttribute it does not work, the print dialog still shows the default option, this is my code.

PrintManager printManager = (PrintManager) context.getSystemService(Context.PRINT_SERVICE);
String jobName = PdfFragment.sProjectPrefix + " " + getFileNameFromPath(mPdfDocumentName);
printManager.print(jobName, new PdfFragmentPrintDocumentAdapter(), printAttributes);

或者我可以在程序中一一设置打印选项吗?

Or can I set the print option in my program one by one?

推荐答案

在传递给打印功能之前,先设置您的 PrintAttributes .这对我有用.

Set your PrintAttributes before passing to print function. This worked worked for me.

PrintManager printManager = (PrintManager) context.getSystemService(PRINT_SERVICE);
    PrintAttributes newAttributes = new PrintAttributes.Builder().
            setMediaSize(PrintAttributes.MediaSize.ISO_A4).
            setMinMargins(PrintAttributes.Margins.NO_MARGINS).
            build();
    printManager.print(context.getString(R.string.print_job_name),
            new PdfFragmentPrintDocumentAdapter(context, view), newAttributes);

PdfFragmentPrintDocumentAdapter.java

PdfFragmentPrintDocumentAdapter.java

public class PdfFragmentPrintDocumentAdapter extends PrintDocumentAdapter {

private PrintedPdfDocument document;
private Context context;
private View view;

public PdfFragmentPrintDocumentAdapter(Context context, View view) {
    this.context = context;
    this.view = view;
}

@Override
public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes,
                     CancellationSignal cancellationSignal,
                     LayoutResultCallback callback, Bundle extras) {
    document = new PrintedPdfDocument(context, newAttributes);
    if (cancellationSignal.isCanceled()) {
        callback.onLayoutCancelled();
        return;
    }

    PrintDocumentInfo.Builder builder = new PrintDocumentInfo
            .Builder(context.getString(R.string.pdf_file_name)+".pdf")
            .setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
            .setPageCount(1);

    PrintDocumentInfo info = builder.build();
    callback.onLayoutFinished(info, true);
}

@Override
public void onWrite(PageRange[] pages, ParcelFileDescriptor destination,
                    CancellationSignal cancellationSignal,
                    PrintDocumentAdapter.WriteResultCallback callback) {
    cancellationSignal.setOnCancelListener(new CancellationSignal.OnCancelListener() {
        @Override
        public void onCancel() {
            Toast.makeText(context, context.getString(R.string.printing_cancel), Toast.LENGTH_SHORT).show();
        }
    });

    PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(view.getWidth(),view.getHeight(), 1).create();
    PdfDocument.Page page = document.startPage(pageInfo);
    view.draw(page.getCanvas());
    document.finishPage(page);

    try {
        document.writeTo(new FileOutputStream(
                destination.getFileDescriptor()));
    } catch (IOException e) {
        String exception = e.toString();
        Toast.makeText(context, context.getString(R.string.printing_failed)+"\n" + exception, Toast.LENGTH_SHORT).show();
        callback.onWriteFailed(exception);
        return;
    } finally {
        document.close();
        document = null;
    }
    callback.onWriteFinished(new PageRange[]{new PageRange(0, 0)});
}

@Override
public void onFinish() {
    super.onFinish();
}

}

这篇关于如何在Android打印类中更改默认打印选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆