views:

292

answers:

1

Im using ITextSharp to generate a PDF, and have a situation where a need to use DirectContent via a ColumnText.

The problem is, that after writing text via the ColumnText object, the PdfDocument.GetVerticalPosition has not been updated? See following test which fails:

    public void TestGetVerticalPositionIsUpdated()
    {
        PdfContentByte cb = Writer.DirectContent;
        var columnText = new ColumnText(cb);

        float position1 = Writer.GetVerticalPosition(true);
        columnText.SetSimpleColumn(Document.Left,
                                   Document.Bottom, 
                                   Document.Right, 
                                   position1, 
                                   0, 
                                   Element.ALIGN_JUSTIFIED);

        columnText.AddText(new Phrase("Test test test test test\nTest test test test test"));
        columnText.Go();

        float position2 = Writer.GetVerticalPosition(true);
        Assert.AreEqual(position1, position2);
    }

Is there anyway to tell either the writer or the document to update the documents currentHeight.

The obvoius solution was to use PdfDocument.SetVerticalPosition if it only existed :-)

Or am I misunderstanding the whole concept of using DirectContent?

It seems to me that youre not able to use PdfDocument.Add after you have added content to DirectContent, if the current Y position on the document can not be updated or isnt updated automaticly.

+3  A: 

Unfortunately it's not possible to manipulate the currentHeight field of the document. So when you insert a absolutely positioned object using DirectContent you can't "force" the next content added to the document to be inserted after the absolute positioned content.

It's seems that the only way is to keep track of the vertical position yourself and add all content absolutely.

asgerhallas