我使用下面的代码在pdf中打印一个字符串,它给我一个相同大小的字符串的输出,我想用不同的字体大小制作最后两个字符串。我该怎么做?
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);
期望的输出:
最佳答案
执行此操作的最佳方法是在包含最后 2 位数字的第一个字符串之后打印一个单独的字符串。
代码示例:
1) 将 String new1 ="";
替换为:
String textLarge ="" ;
String textSmall ="" ;
2) 将 new1 = STL + STL1;
替换为以下内容:
textLarge = stl;
textSmall = stl1;
3)更改一些代码以分别打印大文本和小文本
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);
替换“?”在上面的行中,显示大文本的打印宽度。
看起来您正在使用 itext,因此您可以使用:
BaseFont.getWidthPoint(String text, float fontSize)
获取字符串的宽度(以 pt 为单位)。
或者将字符串放入 Chunk 中并执行 chunk.getWidthPoint()
关于java - 在pdf中以小字体打印子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12139252/