如何使用数据库中的 Java 在 Itext Pdf 报告上打印印地文文本?

How do Print Hindi Text On Itext Pdf Report using Java From Database?

我正在使用 itext 库在我的 JavaFX 中生成 pdf project.Now 我想从报告中的数据库中打印印地语、旁遮普语文本,我该怎么做。但是 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();

以非 Unicode 符号添加文本绝不是一个好主意,但这不是主要问题。主要问题是您没有提供正确的字体。您需要一种知道如何绘制印度字形的字体。 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);

请注意,这假设 Java 可以访问字体 arialuni.ttf

我正在使用印地语文本显示在 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));

它在我的情况下工作正常。

由于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;

}