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

思路就是,以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()
            {

            @Override
public String savePicture(byte[] arg0, PictureType arg1,
                  String arg2) {
               return savePath + "/"+ arg2;
            }
            } );
         wordToHtmlConverter.processDocument(wordDocument);
         //save pictures
List<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
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值