我已经尝试过下面的代码来打印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文件的空白文档。
我需要你的帮助......
提前致谢
答案 0 :(得分:0)
PrintDocumentAdapter
仅支持PDF文件,因为Android打印框架仅支持PDF文件。您需要找到一些可以运行的库或命令,可能在您的服务器上(例如,unoconv
)将您的文件转换为PDF格式。