【问题标题】:How to print MS-Office documents programmatically in android from SD Card?如何从 SD 卡以编程方式在 android 中打印 MS-Office 文档?
【发布时间】:2016-08-02 14:37:01
【问题描述】:

我已尝试使用以下代码打印 PDF 文件,但它在 MS-Office 文档中不起作用!

PrintManager printManager = (PrintManager) getActivity()
                .getSystemService(Context.PRINT_SERVICE);

 PrintDocumentAdapter printAdapter =
                wView.createPrintDocumentAdapter();
 String jobName = 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);
                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();
                }
            }
        }

它显示 MS-Office 文件的空白文档。

我需要你的帮助...

提前致谢

【问题讨论】:

    标签: android android-studio printing ms-office document


    【解决方案1】:

    PrintDocumentAdapter只支持PDF文件,因为Android打印框架只支持PDF文件。您需要找到一些可以运行的库或命令,可能在您的服务器上(例如,unoconv)将您的文件转换为 PDF 格式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      • 2019-12-17
      • 2014-05-13
      • 2011-06-05
      • 1970-01-01
      相关资源
      最近更新 更多