【问题标题】:How to Print PDF using Android 4.4 Printing framework [closed]如何使用 Android 4.4 打印框架打印 PDF [关闭]
【发布时间】:2013-12-21 08:42:48
【问题描述】:

如何使用 Android 4.4 打印框架打印已下载的​​ PDF?

我查看了开发者文档。但没有运气。任何示例都会有所帮助

【问题讨论】:

    标签: android android-4.4-kitkat android-print-framework


    【解决方案1】:

    在谷歌上花了几个小时后,我找到了解决方案。

    PrintManager printManager = (PrintManager) this.getSystemService(Context.PRINT_SERVICE);
    String jobName = this.getString(R.string.app_name) + " Document";
    printManager.print(jobName, pda, null);
    
    PrintDocumentAdapter pda = new PrintDocumentAdapter(){
    
        @Override
        public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback){
            InputStream input = null;
            OutputStream output = null;
    
            try {
    
                input = new FileInputStream(file to print);
                output = new FileOutputStream(destination.getFileDescriptor());
    
                byte[] buf = new byte[1024];
                int bytesRead;
    
                while ((bytesRead = input.read(buf)) > 0) {
                     output.write(buf, 0, bytesRead);
                }
    
                callback.onWriteFinished(new PageRange[]{PageRange.ALL_PAGES});
    
            } catch (FileNotFoundException ee){
                //Catch exception
            } catch (Exception e) {
                //Catch exception
            } finally {
                try {
                    input.close();
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        @Override
        public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras){
    
            if (cancellationSignal.isCanceled()) {
                callback.onLayoutCancelled();
                return;
            }
    
    
            PrintDocumentInfo pdi = new PrintDocumentInfo.Builder("Name of file").setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).build();
    
            callback.onLayoutFinished(pdi, true);
        }
    };
    

    【讨论】:

    • 支持哪些打印机??
    • 谢谢你,你为我节省了大量时间:)
    • 非常有用..只需从代码中删除 int pages = computePageCount(newAttributes);
    • 感谢您的完美回答,您知道如何从图像 url 或本地设备图像打印图像吗?
    猜你喜欢
    • 1970-01-01
    • 2013-11-12
    • 2013-12-07
    • 2017-05-24
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 2018-06-29
    • 1970-01-01
    相关资源
    最近更新 更多