Christian wrote:
> The final goal is to write Html text into Header and Footer of a Pdf. That
> Html must contain the number of total pages too
> For example: <b>Page 1 of <i>6</i></b>

Aha, now I understand.

> For do that I've create a class that extend PdfPageEventHelper just like the
> example.
> In the onEndPage method I add a PdfTemplate object to the document.
> In the onCloseDocument method, I try to write Html text to that PdfTemplate
> object

OK.

> For write Html I the method 'parse' of HTMLWorker object but HTMLWorker can
> write only on a DocListener so I've created a new class called DocElement
> that extends Paragraph and implements DocListener

That's not necessary! See http://1t3xt.be/?X00d
More specifically:

public static boolean addText(
        String s, PdfContentByte canvas,
        float[] f, float size, boolean simulate)
        throws DocumentException, IOException {
        StyleSheet styles = new StyleSheet();
        styles.loadTagStyle("p", "size", size + "px");
        styles.loadTagStyle("p", "align", "justify");
        styles.loadTagStyle("p", "hyphenation", "en_us");
        ArrayList<Element> objects = HTMLWorker.parseToList(
                new StringReader(s), styles, null);
        ColumnText ct = new ColumnText(canvas);
        ct.setAlignment(Element.ALIGN_JUSTIFIED);
        ct.setLeading(size * 1.2f);
        ct.setSimpleColumn(f[1] + 2, f[2] + 2, f[3] - 2, f[4]);
        for (Element element : objects) {
                ct.addElement(element);
        }
        return ColumnText.hasMoreText(ct.go(simulate));
}

String s is some HTML, float f keeps the coordinates.
The content is written to a PdfContentByte using ColumnText.
You could do the same with PdfTemplate (it extends PdfContentByte).

> After I've write the Html text in this DocElement object I add it to a
> PdfPtable and I write the PdfPtable object to the template

That's overdoing it. It's easier with the above code snippet.

> All works fine if my Html doesn't contain any ' table' tag.
> 
> About your question, if I add my DocElement object to Document with
> Document.add I can see the table in the PDF correctly
> 
> But my goal is add it to PdfTemplate. So How can I do it?
> 
> Probabily there is a simpler way to add Html text with total pages in the
> Header but I'm not able to find it.

Try if it works as done in the code snippet.
If it doesn't, post another question.
-- 
This answer is provided by 1T3XT BVBA
http://www.1t3xt.com/ - http://www.1t3xt.info

------------------------------------------------------------------------------
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.1t3xt.com/docs/book.php
Check the site with examples before you ask questions: 
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/

Reply via email to