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

时间:2015-10-31 04:47:27

标签: java unicode printing javafx-2 itext

我在我的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 个答案:

答案 0 :(得分:0)

以不是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

答案 1 :(得分:0)

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

} 

答案 2 :(得分:-1)

我正在使用印地文文字出现在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));

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