我在我的 JavaFX 项目中使用 itext 库生成 pdf。现在我想从报告内的数据库打印印地语、旁遮普语文本,我该怎么做。但是 IText 库无法识别印地语和旁遮普语 unicode ??? 这是我的代码
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();
最佳答案
由于itext
不支持白话,请将文本转换为位图并设置为图像。使用以下方法进行转换:
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;
}
关于java - 如何使用 Java 从数据库打印 Itext Pdf 报告上的印地语文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33448148/