I am evaluating iText as a PDFGenerator for java swing application. The output is supposed to be in \"Marathi\", which is a local Indian langauge similar to hindi but not sa
The following worked for me.
import java.awt.Graphics2D;
import java.io.*;
import com.lowagie.text.*;
public class Test {
/** Path to the resulting PDF file. */
public static final String RESULT
= "/home/test.pdf";
/**
* Creates a PDF file: test.pdf
* @param args no arguments needed
*/
public static void main(String[] args)
throws DocumentException, IOException {
Document document = new Document();
PdfWriter writer =
PdfWriter.getInstance(document, new FileOutputStream(RESULT));
document.open();
int w = 400;
int h = 150;
PdfContentByte cb = writer.getDirectContent();
PdfTemplate tp = cb.createTemplate(w, h);
Graphics2D g2 = tp.createGraphicsShapes(w, h);
g2.drawString("मराठी ग्रीटींग्स, मराठी शुभेच्छापत्रे", 20, 100);
g2.dispose();
cb.addTemplate(tp, 50, 400);
document.close();
}
}
Unless included in one of the most recent versions, iText does not support the Devanāgarī writing system.
In some writing systems, there is no one-to-one relation between the actual letter and the correct glyph, but the glyph shape differs depending on e.g. the surrounding glyphs or its position within a word. To render the text correctly, the type setting software needs to implement these rules and AFAIK, iText implements such rules only for Arabic.
As itext is not supporting vernacular language,Convert text to bitmap and set as image.Use Below Method for conversion:
Step 1:
public Bitmap textAsBitmap(String text, float textSize, float stroke, int color) {
TextPaint paint = new TextPaint();
paint.setTextSize(textSize);
paint.setAntiAlias(true);
// paint.setTextAlign(Paint.Align.LEFT);
paint.setColor(Color.BLACK);
// paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeWidth(20f);
paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.NORMAL));
float baseline = (int) (-paint.ascent() + 3f); // ascent() is negative
StaticLayout staticLayout = new StaticLayout(text, 0, text.length(),
paint, 435, android.text.Layout.Alignment.ALIGN_NORMAL, 1.0f,
1.0f, false);
Bitmap image = Bitmap.createBitmap(staticLayout.getWidth(),
staticLayout.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(image);
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(image, 5, 5, null);
staticLayout.draw(canvas);
return image;
}
Step 2:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap bitmap = Bitmap.createBitmap(Utils.textAsBitmap(""+yourString,14,2,200));
bitmap.compress(Bitmap.CompressFormat.JPEG, 100 , stream);
Image myImg = Image.getInstance(stream.toByteArray());
document.add(myImg);