Reputation: 482
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 same.
For evaluation purposes this is the text i am trying to print:
मराठी ग्रीटींग्स, मराठी शुभेच्छापत्रे
Here is the source code:
package pdftest;
import java.io.FileOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
public class CPDFTest
{
private static String FILE = "c:/will/FirstPdf.pdf";
public static void main(String[] args)
{
try
{
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(FILE));
document.open();
addMetaData(document);
addTitlePage(document);
document.close();
}
catch (Exception e)
{
}
}
private static void addMetaData(Document document)
{
document.addTitle("My first PDF");
}
private static void addTitlePage(Document document)
throws DocumentException
{
Paragraph preface = new Paragraph();
FontFactory.registerDirectory("C:\\WINDOWS\\Fonts");
Font marFont = FontFactory.getFont("arial unicode ms",BaseFont.IDENTITY_H,true);
// Lets write a big header
preface.add(new Paragraph("मराठी ग्रीटींग्स, मराठी शुभेच्छापत्रे", marFont));
document.add(preface);
}
}
Please check the following image for error details:
I think the issue maybe with the encoding or something but am not able to figure it out as of now. Any help will be appreciated.
Upvotes: 3
Views: 8308
Reputation: 11
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);
Upvotes: 0
Reputation: 1
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();
}
}
Upvotes: 0
Reputation: 34323
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.
Upvotes: 2