【问题标题】:Printing PDF directly using PrintManager Android 4.4使用 PrintManager Android 4.4 直接打印 PDF
【发布时间】:2013-11-12 17:00:52
【问题描述】:

http://developer.android.com/training/printing/index.html 文档告诉如何通过在 PDF 画布上呈现自定义内容并发送由此创建的 PDF 文档进行打印来打印自定义内容。但是没有关于如果我们已经有一个 PDF 文档,如何发送它进行打印的信息?

类似位图打印,有类似printHelper.printPDF的方法吗?

【问题讨论】:

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


    【解决方案1】:

    所以这就是我所做的......

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public void clicked(View view){ //Button To start print
        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()
    {
    
        @RequiresApi(api = Build.VERSION_CODES.KITKAT)
        @Override
        public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback)
        {
            InputStream input = null;
            OutputStream output = null;
            try {
                input = new FileInputStream(new File("/storage/emulated/0/Download/file_name.pdf"));
                output = new FileOutputStream(destination.getFileDescriptor());
                byte[] buf = new byte[1024];
                int bytesRead;
                while ((bytesRead = input.read(buf)) > 0) {
                    output.write(buf, 0, bytesRead);
                }
            }
            catch (Exception e) {
    
            } finally {
                try {
                    input.close();
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            callback.onWriteFinished(new PageRange[]{PageRange.ALL_PAGES});
        }
    
        @RequiresApi(api = Build.VERSION_CODES.KITKAT)
        @Override
        public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras)
        {
            if (cancellationSignal.isCanceled())
            {
                callback.onLayoutCancelled();
                return;
            }
    
            //int pages = computePageCount(newAttributes);
    
            PrintDocumentInfo pdi = new PrintDocumentInfo.Builder("file_name.pdf").setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).build();
            callback.onLayoutFinished(pdi, true);
        }
    };
    }
    

    Android 清单 >>>

    添加权限

        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    

    我的手机不支持 sd 卡,但我还是写了 READ_EXTERNAL_STORAGE

    【讨论】:

      【解决方案2】:

      在你的 onWrite() 方法中使用以下代码片段应该可以做到:

      InputStream input = null;
      OutputStream output = null;
      try {
          input = new FileInputStream(new File("somefile.pdf"));
          output = new FileOutputStream(destination.getFileDescriptor());
          byte[] buf = new byte[1024];
          int bytesRead;
          while ((bytesRead = input.read(buf)) > 0) {
               output.write(buf, 0, bytesRead);
          }
      } catch (Exception e) {
      
      } finally {
          try {
              input.close();
              output.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
      

      【讨论】:

      • 使用上述代码时,打印按钮显示为禁用
      • 忽略我之前的评论,在对 onLayout 方法进行了一些修改后,现在它可以正常工作了
      • @Dinesh 你能帮我看看你在 onLayout 方法中做了什么
      • @sreeraag 一定要看看这个答案stackoverflow.com/a/20719729/1394139
      • 但点击“save pdf”会保存 0 个字节。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-20
      • 2019-01-03
      • 1970-01-01
      • 2015-01-11
      相关资源
      最近更新 更多