Text position issue


We are stamping text to existing PDF document, but I ran into a
strange issue when I was testing the application. Here are the specs:
1 - Text to be stamped is variable
2 - Text position is set by the user (inches)

It looks like the text position I set via cmd line is not the one I
wanted. Take a look at a piece of my code:

            writer.begin( page );
            pdftron.PDF.Element element =
builder.createTextBegin( Font.create( pdfDoc, font ), fontSize );
            writer.writeElement( element );
            element = builder.createTextRun( whiteIndex );
            // get the real text position
            double x = xPosition * 72;
            double y = page.getPageHeight() - ( yPosition * 72 );
            System.out.println( category, "x: " + x );
            CommonLog.getLog().write( category, "y: " + y );
            // set the text position
            element.setTextMatrix( 1, 0, 0, 1, x, y );
            Rect rect = element.getBBox();
            System.out.println( category, "X1: " + rect.getX1() );
            System.out.println( category, "Y1: " + rect.getY1() );
            System.out.println( category, "X2: " + rect.getX2() );
            System.out.println( category, "Y2: " + rect.getY2() );
            // set text color to white
            GState gstate = element.getGState();
            gstate.setFillColorSpace( ColorSpace.createDeviceRGB() );
            gstate.setFillColor( new ColorPt( 0, 0, 1 ) );
            writer.writeElement( element );
            writer.writeElement( builder.createTextEnd() );

Not sure why, but the Rect object from the text Element does not have
the coordinates I set using the setTextMatrix() method. Am I missing

You would need to take into account the page rotation or crop-box
Probably the simplest way is to use page.GetDefaultMatrix() as
described in the following articles:

What is the proper way to add new content to an existing (rotated/
cropped) PDF page:

Taking into account effect of page rotation when adding new content to
a page:

Since you are adding fairly simple stamp you may also want to consider
using 'pdftron.PDF.Stamper' (http://www.pdftron.com/pdfnet/
samplecode.html#Stamper) which is much simpler to use.