Pergunta

I using the below code to print a string in pdf which give me an out put of the same sized string i want to make the last two string in different font size. how can i do this ?

PdfContentByte cb = writer.getDirectContent();

    String stl = "", stl1 = (String) request.getParameter("x0");//                        (String)val.get(0);
        int ln = stl1.length();
        String new1 ="" ;
        if (ln > 1)
        {
            for (int ii = 0; ii < ln - 2; ii++)
                stl = stl + stl1.charAt(ii);
            stl = stl + " . ";
            stl1 = stl1.substring(ln - 2, ln);
            new1 = stl + stl1;
            //stl1 = stl + stl1;

        }
        cb.setFontAndSize(bf, 18); 
        cb.setTextRenderingMode(2);
        //cb.showTextAligned(PdfContentByte.ALIGN_LEFT, stl1, 20 + x, 663 + y, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_LEFT, new1, 20 + x, 663 + y, 0);

Desired Output :

enter image description here

Foi útil?

Solução

The best way to do this is to print a separate string after the first one that contains the last 2 digits.

Code example:

1) Replace String new1 ="" ; with this:

String textLarge ="" ;
String textSmall ="" ;

2) Replace new1 = stl + stl1; with the following:

textLarge = stl;
textSmall = stl1;

3) Change some code to print the large and small text separately

cb.setFontAndSize(bf, 18);        
cb.showTextAligned(PdfContentByte.ALIGN_LEFT, textLarge, 20 + x, 663 + y, 0);
cb.setFontAndSize(bf, 14);        
cb.showTextAligned(PdfContentByte.ALIGN_LEFT, textSmall, 20 + ? + x, 663 + y, 0);

Replace the "?" in the line above with the printed width of large text. It looks like you are using itext, so you can use either: BaseFont.getWidthPoint(String text, float fontSize) to get the width of the string in pt. Or put the string in a Chunk and do chunk.getWidthPoint()

Outras dicas

The previous answer is correct, but there's also an alternative way to do it. If you want to position text at an absolute coordinate, you can also use the ColumnText class. For instance: you can use the static method showTextAligned() that is similar to what you've been using so far. The advantage of the ColumnText method is that it accepts Phrase objects. A Phrase object can consist of different Chunk objects and each Chunk can use a different font with a different size. For an example that demonstrates the previous answer as well as this one, see http://itextpdf.com/examples/iia.php?id=62

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top