I have a strange problem with iText & PdfTemplate.
I'm using PdfTemplate & Image to print the total Page number to the page footer. I create a PdfTemplate, create an Image from the Template and put the image into a chunk, the chunk is added then to the table/phrase/paragraph, whatever component the parent is.
PdfTemplate tmp = PdfTemplate.createTemplate(writer, 20, 10);
Image img = null;
try
{
img = Image.getInstance(tmp);
}
catch (BadElementException e)
{
}
return new Chunk(img, 0, 0);
This Chunk returned is then added to the page (into a Phrase for example). The created PdfTemplate is saved into a list to get it later.
Then in the pageEventHelper (onCloseDocument), I fill the template with the total page number with the help of the ColumnText, like this:
PdfTemplateRecord rec = pagenums.get(i);
PdfTemplate tmp = rec.getTemplate();
Font font = CreatorHelper.getCurrentFont(rec.getFontContext());
if (font == null)
{
font = new Font(defaultFont, 8f);
}
ColumnText columnText = new ColumnText(tmp);
columnText.setSimpleColumn(new Phrase(Integer.toString(pagenum), font), 0, -tmp.getHeight(), tmp.getWidth(), 0, 0, Element.ALIGN_LEFT);
columnText.setUseAscender(false);
try
{
columnText.go();
}
catch (DocumentException e)
{
log.error("Failed to render total page number", e);
}
Everything is working just fine except two things:
1.) if you check the setSimpleColumn carefully, I had to set the Y position to -tmp.getHeight() (eg. -10) instead of 0. If I set the Y to 0, the text is drawn just above the line... Probably I messed up something with the image addition, or so, but don't know
2.) I have problems with the font sizing. Strange, but if the font size if bigger than 10f, the normal text, and the text in the template are identical. However if the font size is lower than 10f, the text in template gets smaller. Here some image
(images in this order, font size: 6f, 8f, 9f, 10f, 14f, the "Page 1 of" is the normal text, and the number after the "of" comes with the template)
As you can see with 9f font size the template text is slightly smaller than the normal text, but after 10f the two texts are identical.
I searched if there is some rule between the PdfTemplate and Elements regarding font sizing, but did not found anything.
Any help is appretiated!
Thanks!