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

时间:2021-12-27 22:18:14

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. However IText library is not recognise hindi and punjabi unicode ??? here is mine code

我在我的JavaFX项目中使用itext liberary进行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();

3 个解决方案

#1


0  

It's never a good idea to add the text in a notation that isn't Unicode, but that's not the main problem. 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:

将文本添加到非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);

Note that this assumes that Java has access to the font arialuni.ttf.

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

#2


0  

As itext is not supporting vernacular language, convert text to bitmap and set as image. Use below method for conversion:

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

} 

#3


-1  

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 .

我的工作正常。

#1


0  

It's never a good idea to add the text in a notation that isn't Unicode, but that's not the main problem. 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:

将文本添加到非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);

Note that this assumes that Java has access to the font arialuni.ttf.

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

#2


0  

As itext is not supporting vernacular language, convert text to bitmap and set as image. Use below method for conversion:

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

} 

#3


-1  

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 .

我的工作正常。