Print Arabic Characters In Android Thermal Printer
Printer is GoojPRT portable printer PT-210 (thermal printer) the same code is work on another thermal printer POS but not work on this printer for Arabic characters the English cha
Solution 1:
Try to add ISO-8859-6 encode for arabic text.
Solution 2:
I had the same problem, after two days of searching, I found out that, the simple way to print a multilingual text like Arabic is to draw it on a canvas and print it as a normal image, as shown here :
public Bitmap getMultiLangTextAsImage(String text, Paint.Align align, float textSize, Typeface typeface) {
Paintpaint=newPaint();
paint.setAntiAlias(true);
paint.setColor(Color.BLACK);
paint.setTextSize(textSize);
if (typeface != null) paint.setTypeface(typeface);
// A real printlabel width (pixel)floatxWidth=385;
// A height per text line (pixel)floatxHeight= textSize + 5;
// it can be changed if the align's value is CENTER or RIGHTfloatxPos=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.floatyPos=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 = newArrayList<PrintData>();
// if '\n' character included in the original string
String[] tmpSplitList = text.split("\\n");
for (inti=0; i <= tmpSplitList.length - 1; i++) {
StringtmpString= tmpSplitList[i];
// calculate a width in each split string item.floatwidthOfString= paint.measureText(tmpString);
// If the each split string item's length is over the width of print label,if (widthOfString > xWidth) {
StringlastString= tmpString;
while (!lastString.isEmpty()) {
StringtmpSubString="";
// retrieve repeatedly until each split string item's length is// under the width of print labelwhile (widthOfString > xWidth) {
if (tmpSubString.isEmpty())
tmpSubString = lastString.substring(0, lastString.length() - 1);
elsetmpSubString= 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 alignif (align == Paint.Align.CENTER) {
if (widthOfString < xWidth) {
xPos = ((xWidth - widthOfString) / 2);
}
} elseif (align == Paint.Align.RIGHT) {
if (widthOfString < xWidth) {
xPos = xWidth - widthOfString;
}
}
printDataList.add(newPrintData(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(newPrintData(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);
}
} elseif (align == Paint.Align.RIGHT) {
if (widthOfString < xWidth) {
xPos = xWidth - widthOfString;
}
}
printDataList.add(newPrintData(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.Bitmapbm= Bitmap.createBitmap((int) xWidth, (int) xHeight, Bitmap.Config.ARGB_8888);
Canvascanvas=newCanvas(bm);
canvas.drawColor(Color.WHITE);
for (PrintData tmpItem : printDataList)
canvas.drawText(tmpItem.text, tmpItem.xPos, tmpItem.yPos, paint);
return bm;
}
staticclassPrintData {
float xPos;
float yPos;
String text;
publicPrintData(float xPos, float yPos, String text) {
this.xPos = xPos;
this.yPos = yPos;
this.text = text;
}
publicfloatgetxPos() {
return xPos;
}
publicvoidsetxPos(float xPos) {
this.xPos = xPos;
}
publicfloatgetyPos() {
return yPos;
}
publicvoidsetyPos(float yPos) {
this.yPos = yPos;
}
public String getText() {
return text;
}
publicvoidsetText(String text) {
this.text = text;
}
}
If you want more details, check this
Post a Comment for "Print Arabic Characters In Android Thermal Printer"