purePDF (iText) current x/horizontal position.

purePDF (iText) current x/horizontal position.

I just started with purePDF and I haven’t seen any solutions for getting the current horizontal/x postion in a doc. Though, you can get the vertical postion by:

var buffer:ByteArray = new ByteArray();
var rect:RectangleElement = PageSize.A4;
var writer:PdfWriter = PdfWriter.create( buffer, rect );
var document:PdfDocument = writer.pdfDocument;
var vertPosition:Number = document.getVerticalPosition(false)

What I wanted to do was create a line that extended form the end of a sentence to the end of the page. Something like:

What is your first name? ______________________________________________________________

alivePDF makes this possible but with purePDF/iText, the easiest way I could figure it out was by using Chunk.getWidthPoint():

var ch:Chunk = new Chunk("What is your first name?");
var para:Paragraph = new Paragraph(null, null);
para.add(ch);			
document.add(para); //document is defined in the code block at the top of this post.
var cb: PdfContentByte = writer.getDirectContent();
 
var re:RectangleElement = new RectangleElement(document.marginLeft + ch.getWidthPoint(), document.getVerticalPosition(false), document.pageSize.width - document.marginRight, document.getVerticalPosition(true) + 5);
re.border = RectangleElement.BOTTOM;
re.borderColor = RGBColor.BLACK;
re.borderWidth = 1;
cb.rectangle(re);
 
document.close();
var f: FileReference = new FileReference();
f.save( buffer, "Test.pdf" );

I realize this only works for a single line paragraph but it does work well. If anyone has an alternative solution using page events, etc.. please share!