1

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

我有一个表格行,我想在其中显示一些文本(孟加拉语 ttf)。文本应该占用 2 行,但它占用 3 行。问题是,第一行只显示了 5 个单词,并且在结尾处显示了巨大的空白,看起来好像可以轻松占用 8 个单词。

请注意,如果我使用英语,我不会遇到这样的问题。我认为孟加拉语 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;
}

在此处输入图像描述

4

1 回答 1

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);

// 请注意,您可能需要更改前导值以进行调整,并且它适用于孟加拉语等印度语,而不适用于英语

于 2013-04-16T07:00:00.093 回答