iText unicode字体问题

时间:2013-04-15 10:38:53

标签: java fonts alignment reporting itext

我使用itext(lowagie)sdk在java中使用ttf字体。我想通过例子来解释我的问题。

我有一个表格行,我想显示一些文字(bengali ttf)。该文本应该采用2行,但它需要3行。问题是,第一行显示只有5个单词和最后一个巨大的空白区域,看起来它可以轻松地占用8个单词。

请注意,如果我使用英语,我不会遇到这样的问题。我假设bengali unicode导致问题。如果有关于问题的任何锻炼,那就太棒了。

我通过例子重新解释我的问题:

Output:

Line1: AAA BBBB CCCC DDDD EEEE <BLANK SPACE BLANK SPACE>ENDOFLINE

Line2: CC DD EE PP QQ RR SS EE <BLANK SPACE BLANK SPACE>ENDOFLINE

Line3: CC DD EE PP QQ RR       <BLANK SPACE BLANK SPACE>ENDOFLINE


Expected:

Line1: AAA BBBB CCCC DDDD EEEE CC DD EE PP  #ENDOFLINE

Line2: QQ RR SS EE CC DD EE PP QQ RR    #ENDOFLINE

来源单元:

fontpath="D://Bangla.ttf";
this.banglaFont = BaseFont.createFont(fontpath,BaseFont.IDENTITY_H,BaseFont.EMBEDDED);



        PdfPTable bTable = new PdfPTable(1);
        bTable.getDefaultCell().setBorder(1);
        bTable.setWidthPercentage(100);
        bTable.addCell(backTableRow1cell(28.5f, 8.8f, CardStr.GOVTNOTE_BNG));




public static PdfPCell backTableRow1cell(float hght, float fontSize, String txt) {
    PdfPCell c1 = new PdfPCell();
    c1.setFixedHeight(hght);
    c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
    ITextUtil.setCellBorder(c1, 0, 0, 1, 0);
    ITextUtil.setCellPadding(c1, 0, 7, 0, 0);
    c1.addElement(TextProcessing.designPar(TextProcessing.getFontStyle(VoterCard.banglaFont, fontSize, Color.BLACK, Font.NORMAL), txt, Element.ALIGN_LEFT, 8, 2));
    return c1;
}
public static Paragraph designPar(Font fnt, String txt, int alignment, int leading, int afterspacing) {
    Paragraph phrs = new Paragraph(txt, fnt);
    phrs.setLeading(leading);
    phrs.setAlignment(alignment);
    return phrs;
}

public static Font getFontStyle(BaseFont baseFont, float size, Color clr, int FontStyle) {
    Font fnt = new Font(baseFont, size);
    fnt.setColor(clr);
    fnt.setStyle(FontStyle);

    return fnt;
}

enter image description here

1 个答案:

答案 0 :(得分:0)

正如布鲁诺所说,指法语言还不支持连字。因此,如果缩进真的成为必需品,那么它可以通过以下方式实现,因为我只使用较旧版本的itext。

Say the text is: "আমার পথ চলা আমার পথে যেনো বেলা শেষে হারায় পথে আমার স্বপ্ন আমার সাথে থাকে স্বপ্নের ভিরে থাকে স্বপ্ন হয়ে রোদ্দুর খুঁজে পাই জীবনের"

And i want to show it in two line as:
আমার পথ চলা আমার পথে যেনো বেলা শেষে হারায় পথে আমার স্বপ্ন
আমার সাথে থাকে স্বপ্নের ভিরে থাকে স্বপ্ন হয়ে রোদ্দুর খুঁজে পাই জীবনের"

then I break it into 4 parts.
txt1="আমার পথ চলা আমার পথে যেনো বেলা শেষে হারায় পথে"
txt2="                                           আমার স্বপ্ন"
txt3="আমার সাথে থাকে স্বপ্নের ভিরে থাকে স্বপ্ন হয়ে রোদ্দুর খুঁজে"
txt4="                                          পাই জীবনের"




    PdfPCell c1 = new PdfPCell();
    c1.setFixedHeight(hght);
    c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
    ITextUtil.setCellBorder(c1, 0, 0, 1, 0);
    ITextUtil.setCellPadding(c1, 0, 7, 0, 0);




    Paragraph phrs1 = new Paragraph(txt1, fnt);
    phrs1.setLeading(12);
    c1.addElement(phrs1);



    Paragraph phrs2 = new Paragraph(txt2, fnt);
    phrs2.setLeading(0);
    c1.addElement(phrs2);


    Paragraph phrs3 = new Paragraph(txt3, fnt);
    phrs3.setLeading(11);
    c1.addElement(phrs3);


    Paragraph phrs4 = new Paragraph(txt4, fnt);
    phrs4.setLeading(0);
    c1.addElement(phrs4);

//请注意,您可能需要更改要调整的主要值,它适用于像孟加拉语这样的指标语言,不适用于英语