Itextsharp Header html only show one line

This is the header I see:

enter image description here

This is my actual header:

enter image description here

This is my code:

    public override void OnStartPage(PdfWriter writer, Document document)
    {
        base.OnStartPage(writer, document);
        ColumnText ct = new ColumnText(writer.DirectContent);
        XMLWorkerHelper.GetInstance().ParseXHtml(new ColumnTextElementHandler(ct), new StringReader((string)System.Web.HttpContext.Current.Session["encabezado"]));
        int? columnas = ct.LinesWritten;
        int? columnas2 = ct.LinesWritten;

        ct.SetSimpleColumn(document.Left, document.Top, document.Right, document.GetTop(-20), 10, Element.ALIGN_MIDDLE);
        ct.Go();
    }

Upvotes: 0

Views: 398

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

When you define the rectangle for the ColumnText object, you don't provide sufficient space:

document.Left, document.Top, document.Right, document.GetTop(-20)

You have two paragraphs:

<p style="text-align: center;">header Ants Nit: [Nit organizacion]</p>
<p style="text-align: center;">secon line: [Nombre organizacion]</p>

You don't define a font, font size or leading, so the defaults are using:

Helvetica
12pt
18pt

You have two paragraphs that each fit a single line, so you need at least 36 user units to make sure that the two lines fit inside the rectangle.

You are defining a rectangle that spans the full width of the page, but that is only 20 user units high. 20 user units is only sufficient for one line, which is consistent with what you see: only one line.

If you want the two lines to appear, you need to define a bigger rectangle, for instance:

ct.SetSimpleColumn(
    document.Left, document.Top,
    document.Right, document.GetTop(-40));

Now is the time for you to say: Wait a minute! I have two extra questions!

  1. I am defining a leading, don't you see the parameter 10 in the SetSimpleColumn() method? Yes, I do see that parameter, but [1] your font is size 12, and a leading that is smaller than the font size will result in ugly text, and [2] I suspect that XML Worker adds paragraphs in composite mode, in which case the leading of the column is ignored in favor of the leading of the paragraphs. The same goes for the alignment (you used text:align: center in your HTML to achieve this), which is why I removed the two unused parameters from your setSimpleColumn() method.

  2. If I follow your advice, my header overlaps with the actual content. That is true because the default top margin is 36 user units high, and you define a header rectangle that is 40 user units high. You will have to expand your top margin if you want to avoid that your top margin overlaps with the actual content.

I have one counter-question: why on earth are you using XML Worker to create the header. Your header is extremely simple. I don't understand why you'd want to define your header using two <p> tags. Just define two Paragraph objects and you'll save your CPU the trouble of parsing HTML.

Finally: I must point at an error you are making. You should never add content in the OnStartPage() method, only in the OnEndPage() method.

In the documentation, we made it very clear that it was forbidden to add content in the onStartPage() method; content can only be added in the onEndPage() method. We also made it very clear that the Document object passed to the page event methods was passed for read-only purposes only. It was forbidden to use document.add() even in the onEndPage() method.

Unfortunately, many developers completely ignore the documentation, which led to problems such as:

Furthermore, you might remove the strange lines about the LinesWritten. I don't understand why they would be useful.

Upvotes: 1

Related Questions