1
votes

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

when font size is 6f

when font size is 8f

when font size is 9f

when font size if 10f

when font size if 14f

(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!

1
Unfortunately your code does not easily allow reproducing the issue as there are many unknown methods you use and it also is not evident how you add your contents to the page. And I would assume the cause for your issue to be due to some quirks of those unknown pieces of code.mkl
Although a big change, couldn't you just switch to making two passes on your PDF? First pass makes the PDF, second puts the page numbers on? It really simplifies things. See third block of code hereChris Haas
Hi! I can't do it in two passes, since I don't know where exactly the totalPages comes. The PDF is generated from a template (XML), and the totalPages could be everywhere (even in the document body). @mkl: well I cannot paste the whole generator code, since it is about 2000 lines, but there are nothing special about those "unknown" methods, may be you thing about this? CreatorHelper.getCurrentFont(rec.getFontContext());user1536873
"I cannot paste the whole generator code, since it is about 2000 lines" - a good approach to such issues usually is to reduce the code to as little as needed to reproduce the issue. This often makes a possible error stand out prominently... "may be you thing about this? CreatorHelper.getCurrentFont(rec.getFontContext());" - Yes, that in particular. Probably that method sometimes returns a font with the wrong size.mkl
Well, I cannot reduce the code, but can create simple template, which does not utilize the whole 2000 lines of code (what I did). I checked in the debugger, the method in question does return the proper font with proper size, style and type. Since this problem only occured in PdfCell, I reviewed the code where values in the PdfCell was added. The elements were added in a Paragraph, I removed the Paragraph, and suddenly the characters were equal in all font size. No idea, why the Paragraph did a difference between the text and the Image (created from the template). Probably some image scaling..user1536873

1 Answers

0
votes

Sorry to revive an old post: This is in relation to inconsistent font size the page number [Page 1 of 1]

OP's comment of removing the Paragraph to get an equal font size did not work for me (on a similar image scaling issue).

Referring to this post here shows what could have gone wrong.

BUT: the first PDF (without the template) contains an "embedded subset" of the font "Georgia" (Ansi encoding). That's the correct way to use a font. The second PDF however, contains: - an embedded subset of Georgia (Ansi) - an embedded subset of Georgia (CID / Identity-H) - Georgia NOT EMBEDDED (Ansi) - Georgia-Bold NOT EMBEDDED (Ansi)

The two first fonts are OK, but as the two following fonts are not embedded, my Adobe Reader had to look for the font Georgia on my Operating System.

Hope this helps :)

P.S. When adding content to the template, the font can be set while adding the Phrase which allows the content of the template to have an independent font.