需要将应用中生成的 PDF 直接打印出来

# 构建 PDFDocumentAdapter

public class PDFDocumentAdapter extends PrintDocumentAdapter {
    private static final String TAG = "PDFDocumentAdapter";
    Context context;
    String pdfPath;
    File pdfFile;
    public PDFDocumentAdapter(Context context, String pdfPath) {
        this.context = context;
        this.pdfPath = pdfPath;
    }
    public PDFDocumentAdapter(Context context, File pdfFile) {
        this.context = context;
        this.pdfFile = pdfFile;
    }
    @Override
    public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) {
        if (cancellationSignal.isCanceled()) {
            callback.onLayoutCancelled();
            InsToastUtil.showInBottom("取消打印");
        } else {
            PrintDocumentInfo.Builder builder =
                    new PrintDocumentInfo.Builder(" file name");
            builder.setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
                    .setPageCount(PrintDocumentInfo.PAGE_COUNT_UNKNOWN)
                    .build();
            callback.onLayoutFinished(builder.build(),
                    !newAttributes.equals(oldAttributes));
        }
    }
    @Override
    public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback) {
        InputStream in = null;
        OutputStream out = null;
        try {
            File file;
            if (pdfFile != null) {
                file = pdfFile;
            }else {
                file = new File(pdfPath);
            }
            in = new FileInputStream(file);
            out = new FileOutputStream(destination.getFileDescriptor());
            byte[] buf = new byte[16384];
            int size;
            while ((size = in.read(buf)) >= 0
                    && !cancellationSignal.isCanceled()) {
                out.write(buf, 0, size);
            }
            if (cancellationSignal.isCanceled()) {
                callback.onWriteCancelled();
                InsToastUtil.showInBottom("取消打印");
            } else {
                callback.onWriteFinished(new PageRange[]{PageRange.ALL_PAGES});
                InsToastUtil.showInBottom("打印完成");
            }
        } catch (Exception e) {
            callback.onWriteFailed(e.getMessage());
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

# 打印

/**
     * 调用系统打印功能打印 PDF
     */
    private void print() {
        PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
        try {
            PrintDocumentAdapter printAdapter = new PDFDocumentAdapter(this, Static.getRootFolderPath() + File.separator + "test.pdf");
            printManager.print("Document", printAdapter, new PrintAttributes.Builder().build());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

需要手动添加打印机,即可打印,但是打印过程中存在纸张尺寸不匹配问题

更新于 阅读次数

请我喝[茶]~( ̄▽ ̄)~*

Logan 微信支付

微信支付

Logan 支付宝

支付宝

Logan 贝宝

贝宝