我尝试了下面的代码打印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文件显示空白文档。
求求你我需要你的帮助。
提前感谢
发布于 2016-08-02 23:02:48
PrintDocumentAdapter只支持PDF文件,因为Android打印框架只支持PDF文件。您将需要找到一些库或命令,您可以运行,也许在您的服务器(例如,unoconv),将您的文件转换为PDF格式。
https://stackoverflow.com/questions/38723462
复制相似问题