android+4.4+打印功能,如何使用Android 4.4打印框架打印PDF

Dinesh T A..

73

在google上花了几个小时后我找到了解决方案.

PrintManager printManager = (PrintManager) this.getSystemService(Context.PRINT_SERVICE);

String jobName = this.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 to print);

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();

}

}

}

@Override

public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras){

if (cancellationSignal.isCanceled()) {

callback.onLayoutCancelled();

return;

}

PrintDocumentInfo pdi = new PrintDocumentInfo.Builder("Name of file").setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).build();

callback.onLayoutFinished(pdi, true);

}

};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值