[英]How do Print Hindi Text On Itext Pdf Report using Java From Database?
I am using itext liberary for pdf generation in mine JavaFX project.Now i want's to print hindi , punjabi text from database inside report , How can i do this. 我正在JavaFX project.pdf中使用itext库生成pdf文件,现在我想从报表中的数据库中打印印地文,旁遮普文,我该怎么做。 However IText library is not recognise hindi and punjabi unicode ???
但是IText库不能识别印地文和旁遮普文Unicode ??? here is mine code
这是我的代码
out = new ByteArrayOutputStream();
try {
// initialize document with page size and margins
document = new Document(PageSize.A4, 60, 40, 60, 60);
this.setAlignmentOfCompanyInfo(Common.comp
.getReportsHeaderAlignment());
writer = PdfWriter.getInstance(document, out);
writer.setPageEvent(new PageEventForAddressBookReport());
document.open();PdfContentByte cb = writer.getDirectContent();
PdfTemplate tp = cb.createTemplate(w, h);
@SuppressWarnings("deprecation")
Graphics2D g2 = tp.createGraphicsShapes(w, h);
g2.drawString("वर्तमान शेष", 200, 100);
g2.dispose();
g2.setColor(Color.GREEN);
cb.addTemplate(tp, 50, 400);
BufferedImage bi = new BufferedImage(100, 20, BufferedImage.TYPE_INT_ARGB);
Graphics2D ig2 = bi.createGraphics();
ImageIO.write(bi, "PNG", new File("ourImageName.PNG"));
Image image = Image.getInstance(tp);
document.add(new Chunk(image,5,5));
document.close();
It's never a good idea to add the text in a notation that isn't Unicode, but that's not the main problem. 以非Unicode的符号添加文本绝不是一个好主意,但这不是主要问题。 The main problem is that you aren't providing the right font.
主要问题是您没有提供正确的字体。 You need a font that knows how to draw Indic glyphs.
您需要一种知道如何绘制印度字形的字体。 Arial Unicode MS is such a font:
Arial Unicode MS就是这样的字体:
String text = "\u0936\u093e\u0902\u0924\u093f";
PdfContentByte cb = writer.getDirectContent();
PdfTemplate tp = cb.createTemplate(100, 50);
Graphics2D g2 = tp.createGraphicsShapes(100, 50);
java.awt.Font font = new java.awt.Font(
"Arial Unicode MS", java.awt.Font.PLAIN, 12);
g2.setFont(font);
g2.drawString("Graphics2D: " + text, 0, 40);
g2.dispose();
cb.addTemplate(tp, 36, 750);
Note that this assumes that Java has access to the font arialuni.ttf
. 请注意,这假设Java可以访问字体
arialuni.ttf
。
i am using hindi text to be appear in PDf as below 我正在使用印地文文本出现在PDf中,如下所示
BaseFont unicode = BaseFont.createFont("/home/mani/Documents/Back up (works)/Devanagari/Devanagari.ttf",
BaseFont.COURIER, BaseFont.EMBEDDED);
Font font=new Font(unicode,12,Font.NORMAL,new BaseColor(50,205,50));
table = new PdfPTable(new float[] { 10, 60, 30 });
table.setWidthPercentage(100);
PdfPCell customerLblCell = new PdfPCell(new Phrase("karent balance",
font));
Its working fine in mine case . 在我的情况下它的工作很好。
As itext
is not supporting vernacular language, convert text to bitmap and set as image. 由于
itext
不支持本地语言,请将文本转换为位图并设置为图像。 Use below method for conversion: 使用以下方法进行转换:
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.setAntiAlias(true);
paint.setColor(color);
// paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeWidth(20f);
paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
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);
// int linecount = staticLayout.getLineCount();
//
// int height = (int) (baseline + paint.descent() + 3) * linecount + 10;
Bitmap image = Bitmap.createBitmap(staticLayout.getWidth(),
staticLayout.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(image);
// canvas.drawARGB(100, 100, 100, 100);
staticLayout.draw(canvas);
return image;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.