首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过SD卡在android中编程打印MS-Office文档?

如何通过SD卡在android中编程打印MS-Office文档?
EN

Stack Overflow用户
提问于 2016-08-02 22:37:01
回答 1查看 322关注 0票数 1

我尝试了下面的代码打印PDF文件,但它不能在MS-Office文档中工作!

代码语言:javascript
复制
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文件显示空白文档。

求求你我需要你的帮助。

提前感谢

EN

回答 1

Stack Overflow用户

发布于 2016-08-02 23:02:48

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38723462

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档