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 :
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()