如何从SD卡以编程方式在Android中打印MS-Office文档?

时间:2016-08-02 14:37:01

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

我已经尝试过下面的代码来打印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文件的空白文档。

我需要你的帮助......

提前致谢

1 个答案:

答案 0 :(得分:0)

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