android usb连接打印机,打印各种格式

来源:互联网 发布:聚合数据接口 编辑:程序博客网 时间:2024/06/09 01:01

思路就是,以word为模板,生成.doc文件,转.html,转.pdf文件,转jpg文件。

一。word模板

二。生成需要打印的.doc文件

// 获取模板文件File demoFile = new File(Environment.getExternalStorageDirectory()      + "/aa.doc");// 创建生成的文件File newFile = new File(Environment.getExternalStorageDirectory()      + "/bb.doc");
//给生成的.doc文件赋值
Map<String, String> map = new HashMap<String, String>();map.put("name", name.toString());map.put("sex", sex.toString());map.put("family", family.toString());map.put("birth", birth.toString());map.put("address", address.toString());map.put("idcard",idcard.toString());map.put("InDate", inDate.toString());map.put("jiguan", jiguan.toString());map.put("Phone", Phone.toString());map.put("zhuanzhang", zhuanzhang.toString());map.put("Duanxin", Duanxin.toString());writeDoc(demoFile, newFile, map);
三。通过word转html
public class WordToHtml {   private static final String encoding = "UTF-8";      private static void writeFile(String content, String path) {                 FileOutputStream fos = null;                 BufferedWriter bw = null;                 try {                     File file = new File(path);                     fos = new FileOutputStream(file);                     bw = new BufferedWriter(new OutputStreamWriter(fos,encoding));                     bw.write(content);                 } catch (FileNotFoundException fnfe) {                    fnfe.printStackTrace();                 } catch (IOException ioe) {                     ioe.printStackTrace();                 } finally {                     try {                         if (bw != null)                             bw.close();                         if (fos != null)                             fos.close();                     } catch (IOException ie) {                     }                 }         }          public static void convert2Html(String fileName, String outPutFile)            throws TransformerException, IOException,            ParserConfigurationException {         HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(fileName));//WordToHtmlUtils.loadDoc(new FileInputStream(inputFile));WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(               DocumentBuilderFactory.newInstance().newDocumentBuilder()                     .newDocument());         File file = new File(fileName);         final String savePath = file.getParentFile().getAbsolutePath();          wordToHtmlConverter.setPicturesManager( new PicturesManager()            {            @Overridepublic String savePicture(byte[] arg0, PictureType arg1,                  String arg2) {               return savePath + "/"+ arg2;            }            } );         wordToHtmlConverter.processDocument(wordDocument);         //save picturesList<Picture> pics=wordDocument.getPicturesTable().getAllPictures();         if(pics!=null){            for(int i=0;i<pics.size();i++){               Picture pic = (Picture)pics.get(i);               System.out.println();               try {                  pic.writeImageContent(new FileOutputStream(                        savePath + "/" + pic.suggestFullFileName()));               } catch (FileNotFoundException e) {                  e.printStackTrace();               }              }         }         Document htmlDocument = wordToHtmlConverter.getDocument();         ByteArrayOutputStream out = new ByteArrayOutputStream();         DOMSource domSource = new DOMSource(htmlDocument);         StreamResult streamResult = new StreamResult(out);         TransformerFactory tf = TransformerFactory.newInstance();         Transformer serializer = tf.newTransformer();         serializer.setOutputProperty(OutputKeys.ENCODING, encoding);         serializer.setOutputProperty(OutputKeys.INDENT, "yes");         serializer.setOutputProperty(OutputKeys.METHOD, "html");         serializer.transform(domSource, streamResult);         out.close();         writeFile(new String(out.toByteArray()), outPutFile);      }}
四。html转转pdf
public boolean createPDF(String rawHTML, String fileName, ContextWrapper context){   File file = new File(fileName);   try{      Document document = new Document();      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));      document.open();      //  HTML      String htmlText = Jsoup.clean( rawHTML, Whitelist.relaxed() );      InputStream inputStream = new ByteArrayInputStream( htmlText.getBytes() );      //  PDF      XMLWorkerHelper.getInstance().parseXHtml(writer, document,            inputStream, null, Charset.defaultCharset(), new MyFont());      document.close();      return true;   } catch (FileNotFoundException e) {      e.printStackTrace();      return false;   } catch (DocumentException e) {      e.printStackTrace();      return false;   } catch (IOException e) {      e.printStackTrace();      return false;   }}
五。pdf转jpg
public void saveWebviewPic() {   Picture picture = webview.capturePicture();   Bitmap bmp = Bitmap.createBitmap(picture.getWidth(), picture.getHeight(), Bitmap.Config.RGB_565);   Canvas c = new Canvas(bmp);   picture.draw(c);   savePic("/sdcard/html.jpg", bmp, 5);   Toast.makeText(ShowWordActivity.this,"生成图片",Toast.LENGTH_SHORT).show();}// 保存文件public static boolean savePic(String path, Bitmap bmp, int quality) {   if (bmp == null || bmp.isRecycled()) {      return false;   }   File myCaptureFile = new File(path);   try {      BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));      if (quality == 1) {// jpg         bmp.compress(Bitmap.CompressFormat.JPEG, 85, bos);      } else if (quality == 2) {// png         bmp.compress(Bitmap.CompressFormat.PNG, 100, bos);      } else if (quality == 3) {// 发微薄用         bmp.compress(Bitmap.CompressFormat.JPEG, 75, bos);      } else if (quality == 5) {// jpg         bmp.compress(Bitmap.CompressFormat.JPEG, 100, bos);      }      try {         bos.flush();         bos.close();         // writeEixf(path);      } catch (IOException e) {         // TODO Auto-generated catch block         e.printStackTrace();      }   } catch (FileNotFoundException e) {      // TODO Auto-generated catch block      e.printStackTrace();   }   return true;}
demo下载地址http://download.csdn.net/my

阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 回到地球二次元 浆果儿超市视频哪里看 幼儿系列1页 xy17app下载 杭州到江西旅游 西窝羌寨 和平古镇的风景介绍 红猫大夲营永久地址 牧夜宵和千九九的小说 沈阳全健水世界怎么样 12岁女孩又嫩又紧 欧来欧去旅游网官网app IQ00行为 徐州智慧团建 19岁女生又嫩又紧 www.3284hucom 康明斯发动机2.8致命弱点 囚笼半人间 国产k频道9 无锡太湖景区免费 安利海外旅游不去还要交钱 亲亲影视网4K 三区中文字幕 徐州公安局副局长名单 郁仲骁叶和欢完整番外 南岳雾凇上去看要钱吗 华侨城大峡谷游玩顺序 本不卡高清免v一道本 h http://xbmm31. com 观光路铜山段何时贯通 国产 20页 伊在人线4 徐州工程兵学院招生 俄罗斯白穴 国产1区2区3区4区p站 偷怕自怕第三页 xp1204核厂入口 禽兽播放 夏青 喜种田 华星光电京东方谁是老大 浆果儿 mp4