0

我需要生成印地语 PDF,为此我使用 iTextg。要在印地语中“输入”,我使用 Android 的静态布局将文本转换为位图。但是我的文字质量真的很差。

我的代码如下

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;
}

结果 结果

帮帮我,因为这些 PDF 稍后需要打印。

4

0 回答 0