android 4.4打印功能
来源:互联网 发布:淘宝历史比价 编辑:程序博客网 时间:2024/06/05 18:02
Android 4.4goole提供了打印的统一接口,通过使用Android 的api,加上各个厂商提供的打印插件,实现起来相对比较容易些,因为之前在网上下载了一些打印app,连上打印机后打印不出来,有的就是按照提示下载驱动,同样无法打印,所以,最后选择的是Android api+厂家的的打印插件,
目前Android提供的比较容易的是打印图片和webview,代码相对比较简单
private void printPicture(Bitmap bitmap) {
PrintHelper photoPrinter = new PrintHelper(this);
photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FIT);
photoPrinter.printBitmap("droids.jpg - test print", bitmap);
}
打印webview设置自定义字体,首先将字体库放到assert子目录下:
InputStream in = new FileInputStream(new File(path));
// InputStream in = getAssets().open("beihairen.doc");
int len = in.available();
byte[] buff = new byte[len];
in.read(buff);
String html = new String(buff, "UTF-8");
html = html.replace("@fontPath0", "../font/fs_GBK.TTF");//设置webview中的字体为自定义的字体
html = html.replace("@fontPath1", "../font/fzgfs.TTF");
String baseurl = "file:///android_asset/html/";
mwebView.loadDataWithBaseURL(baseurl, html, "text/html", "UTF-8",
null);
mwebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
createWebPrintJob(mwebView);
}
});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
private void createWebPrintJob(WebView webView) {
// Get a PrintManager instance
PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
// Get a print adapter instance
PrintDocumentAdapter printAdapter = webView
.createPrintDocumentAdapter();
// Create a print job with name and adapter instance
String jobName = getString(R.string.app_name) + " Document";
PrintJob printJob = printManager.print(jobName, printAdapter,
new PrintAttributes.Builder().build());
}
如果需要自定义字体html页面设置的style:
<style>
.text0 { font-family: simsun; line-height:30.0pt;font-size:16.0pt;line-height:26.0pt}
@font-face {
font-family: simsun;
src:url(@fontPath0);
}
.text1 { font-family: fangsong; line-height:30.0pt;font-size:16.0pt;}
@font-face {
font-family: fangsong;
src:url(../font/fangsong.ttf);
}
.text3 {
list-style: outside none none; border-bottom: 1px solid rgb(15, 15, 15);
font-family: fangsong; line-height:30.0pt;font-size:14.0pt;
}
.text4 {
font-family: fangsong; line-height:30.0pt;font-size:14.0pt;
}
.text2 { font-family: fangzheng; line-height:30.0pt;text-align:center;font-size:22.0pt;}
@font-face {
font-family: fangzheng;
src:url(../font/fzgfs.TTF);
}
li{
list-style: outside none none; border-bottom: 1px solid rgb(15, 15, 15);font-size:14.0pt;
font-family: fangsong; line-height:30.0pt;font-size:16.0pt;}
}
</style>
打印pdf文件
打印pdf文档自定义adapter
PrintDocumentAdapter pda = new PrintDocumentAdapter() {
@Override
public void onLayout(PrintAttributes oldAttributes,
PrintAttributes newAttributes,
CancellationSignal cancellationSignal,
LayoutResultCallback callback, Bundle extras) {
if (cancellationSignal.isCanceled()) {
callback.onLayoutCancelled();
return;
}
PrintDocumentInfo info = new PrintDocumentInfo.Builder(
"printer_qr_code.pdf")
.setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
.setPageCount(1).build();//setPageCount(1)不设置打印所有页
callback.onLayoutFinished(info, true);
}
@Override
public void onWrite(PageRange[] pages,
ParcelFileDescriptor destination,
CancellationSignal cancellationSignal,
WriteResultCallback callback) {
// TODO Auto-generated method stub
InputStream input = null;
OutputStream output = null;
try {
input = getAssets().open("beihairen.pdf");
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();
}
}
}
};
参考:http://blog.csdn.net/sahadev_/article/details/51313384
- android 4.4打印功能
- 使用Android系统打印功能
- Android-WiFi打印功能-实现
- Android 各层中日志打印功能的应用
- Android 各层中日志打印功能的应用
- Android 各层中日志打印功能的应用
- Android 各层中日志打印功能的应用
- Android 各层中日志打印功能的应用
- android连接蓝牙打印机实现打印功能
- Android 各层中日志打印功能的应用
- Android各层日志打印功能
- Android 各层中日志打印功能的应用
- Android 日志打印功能及项目
- Android通过系统打印功能实现PDF预览打印
- 打印功能
- 打印功能
- 打印功能
- 打印功能
- 《Motion Design for iOS》(二十四)
- 关于C++中如何判断文件,目录存在的若干方法
- 电脑系统网络一直正在识别的状态无法上网
- 立项疑惑
- 在eclipse中使用安卓模拟器GENYMOTION
- android 4.4打印功能
- stdin流、缓冲区
- join的用法
- 再回首—SQLhelper的编写
- python 安装Pillow
- 数据查询优化法则
- 如何用 Python 实现 Web 抓取?
- java线程池参数说明及队列拒绝策略
- dataGridView绑定数据