Writing to DirectContent with ColumnText does not update PdfDocument.GetVerticalPosition
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.
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.
You could follow your column text object with a multicolumntext object. You would then place all the remaining document objects into the mct object.
var mct = new MultiColumnText(yBottomOfColumnText, MultiColumnText.AUTOMATIC);
mct.AddSimpleColumn(doc.Left, doc.Right); //creates one column
for (int i = 0; i < 100; i++)
{
mct.AddElement(new Paragraph("Test Paragaph Goes HEREEEEEEEE")); //repeats 100 times for test purposes
}
doc.Add(mct);
精彩评论