我也遇到了同样的问题,经过两天的搜索,我发现打印阿拉伯语等多语言文本的简单方法是将其绘制在画布上并将其打印为普通图像,如下所示:
public Bitmap getMultiLangTextAsImage(String text, Paint.Align align, float textSize, Typeface typeface) {
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.BLACK);
paint.setTextSize(textSize);
if (typeface != null) paint.setTypeface(typeface);
// A real printlabel width (pixel)
float xWidth = 385;
// A height per text line (pixel)
float xHeight = textSize + 5;
// it can be changed if the align's value is CENTER or RIGHT
float xPos = 0f;
// If the original string data's length is over the width of print label,
// or '\n' character included,
// it will be increased per line gerneating.
float yPos = 27f;
// If the original string data's length is over the width of print label,
// or '\n' character included,
// each lines splitted from the original string are added in this list
// 'PrintData' class has 3 members, x, y, and splitted string data.
List<PrintData> printDataList = new ArrayList<PrintData>();
// if '\n' character included in the original string
String[] tmpSplitList = text.split("\\n");
for (int i = 0; i <= tmpSplitList.length - 1; i++) {
String tmpString = tmpSplitList[i];
// calculate a width in each split string item.
float widthOfString = paint.measureText(tmpString);
// If the each split string item's length is over the width of print label,
if (widthOfString > xWidth) {
String lastString = tmpString;
while (!lastString.isEmpty()) {
String tmpSubString = "";
// retrieve repeatedly until each split string item's length is
// under the width of print label
while (widthOfString > xWidth) {
if (tmpSubString.isEmpty())
tmpSubString = lastString.substring(0, lastString.length() - 1);
else
tmpSubString = tmpSubString.substring(0, tmpSubString.length() - 1);
widthOfString = paint.measureText(tmpSubString);
}
// this each split string item is finally done.
if (tmpSubString.isEmpty()) {
// this last string to print is need to adjust align
if (align == Paint.Align.CENTER) {
if (widthOfString < xWidth) {
xPos = ((xWidth - widthOfString) / 2);
}
} else if (align == Paint.Align.RIGHT) {
if (widthOfString < xWidth) {
xPos = xWidth - widthOfString;
}
}
printDataList.add(new PrintData(xPos, yPos, lastString));
lastString = "";
} else {
// When this logic is reached out here, it means,
// it's not necessary to calculate the x position
// 'cause this string line's width is almost the same
// with the width of print label
printDataList.add(new PrintData(0f, yPos, tmpSubString));
// It means line is needed to increase
yPos += 27;
xHeight += 30;
lastString = lastString.replaceFirst(tmpSubString, "");
widthOfString = paint.measureText(lastString);
}
}
} else {
// This split string item's length is
// under the width of print label already at first.
if (align == Paint.Align.CENTER) {
if (widthOfString < xWidth) {
xPos = ((xWidth - widthOfString) / 2);
}
} else if (align == Paint.Align.RIGHT) {
if (widthOfString < xWidth) {
xPos = xWidth - widthOfString;
}
}
printDataList.add(new PrintData(xPos, yPos, tmpString));
}
if (i != tmpSplitList.length - 1) {
// It means the line is needed to increase
yPos += 27;
xHeight += 30;
}
}
// If you want to print the text bold
//paint.setTypeface(Typeface.create(null as String?, Typeface.BOLD))
// create bitmap by calculated width and height as upper.
Bitmap bm = Bitmap.createBitmap((int) xWidth, (int) xHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bm);
canvas.drawColor(Color.WHITE);
for (PrintData tmpItem : printDataList)
canvas.drawText(tmpItem.text, tmpItem.xPos, tmpItem.yPos, paint);
return bm;
}
static class PrintData {
float xPos;
float yPos;
String text;
public PrintData(float xPos, float yPos, String text) {
this.xPos = xPos;
this.yPos = yPos;
this.text = text;
}
public float getxPos() {
return xPos;
}
public void setxPos(float xPos) {
this.xPos = xPos;
}
public float getyPos() {
return yPos;
}
public void setyPos(float yPos) {
this.yPos = yPos;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
如果您想了解更多详细信息,请检查this https://github.com/yagoubgrine/AndroidPosBluetoothPrinter