如何使用Android 4.4打印框架打印PDF

 花语白荷 发布于 2023-02-08 18:22

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

我查看了开发人员文档.但没有运气.任何例子都会有所帮助

1 个回答
  • 在google上花了几个小时后我找到了解决方案.

    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);
        }
    };
    

    2023-02-08 18:25 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有