iText pdf library
Website search

How to rotate a single line of text?

I'm converting a document which is created with a online editor. I need to be able to rotate the text using it's central point and not (0,0).

Since Image rotates using the centre I assumed this would work but it doesn't. Anyone has a clue how to rotate text using the central point in itext?

float fw = bf.getWidthPoint(text, textSize);
float fh = bf.getAscentPoint(text, textSize)
    - bf.getDescentPoint(text, textSize); 
PdfTemplate template = content.createTemplate(fw, fh);
Rectangle r = new Rectangle(0,0,fw, fw);
template.setFontAndSize(bf, 12);
template.moveText(0, 0);
Image tmpImage = Image.getInstance(template);
        pageHeight - (y + Utilities.pointsToMillimeters(fh))));

Posted on StackOverflow on Aug 1, 2013 by user1236552

Why are you using beginText(), moveText(), showText(), endText()? Those methods are to be used by developers who speak PDF syntax fluently.

There an easier way to achieve what you want. Just use the static showTextAligned() method that is provided in the ColumnTextclass. That way you don't even need to use beginText(), setFontAndSize(), endText():

    Element.ALIGN_CENTER, new Phrase(text), x, y, rotation);

Using the showTextAligned() method in ColumnText also has the advantage that you can use a phrase that contains chunks with different fonts.

Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now