您的位置:首页 > 移动开发 > Android开发

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

2017-12-25 10:44 1536 查看
思路就是,以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,

acda
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

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: