Enhance Text quality using paint

238 views Asked by At

I need to generate Hindi PDFs and I am using iTextg for this purpose. To "type" in Hindi, I am using Android's static Layout to convert text to Bitmap. However I am getting really poor text quality.

My Code is as follows

public Bitmap textAsBitmap(String text, float textSize, float stroke, int color, int align) {
//All these flags were added after desperate attempts
    TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.HINTING_ON | Paint.LINEAR_TEXT_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
    paint.setTextSize(textSize);
    paint.setColor(color);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeWidth(stroke);
    paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.NORMAL));
    float baseline = (int) (-paint.ascent() + 3f); // ascent() is negative
    Layout.Alignment mAlignment;
    if (align == Element.ALIGN_CENTER)
        mAlignment = Layout.Alignment.ALIGN_CENTER;
    else if (align == Element.ALIGN_RIGHT)
        mAlignment = Layout.Alignment.ALIGN_OPPOSITE;
    else mAlignment = android.text.Layout.Alignment.ALIGN_NORMAL;

    StaticLayout staticLayout = new StaticLayout(text, 0, text.length(),
            paint, 435, mAlignment, 1.0f, 1.0f, false);

    Bitmap image = Bitmap.createBitmap(staticLayout.getWidth(),
            staticLayout.getHeight() + 2, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(image);
    canvas.drawColor(Color.WHITE);

    canvas.drawBitmap(image, 5, 5, paint);

    staticLayout.draw(canvas);
    return image;
}

The Result Result

Help me out as these PDFs are later required to be printed.

0

There are 0 answers