2016-08-02 100 views
1

我已經嘗試下面的代碼來打印PDF文件,但它不是在MS-Office文檔中工作!如何以編程方式在Android中從SD卡打印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文件的空白文檔。

請我需要你的幫助......

在此先感謝

回答

0

PrintDocumentAdapter僅支持PDF文件,因爲Android的打印框架只支持PDF文件。您需要找到一些可以運行的庫或命令,可能在您的服務器上(例如,unoconv)將您的文件轉換爲PDF格式。

相關問題