How to rotate a single line of text?

How do I rotate text using the central point in itext?

1st November 2015
admin-marketing

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);
r.setBackgroundColor(BaseColor.YELLOW);
template.rectangle(r);
 
template.setFontAndSize(bf, 12);
template.beginText();
template.moveText(0, 0);
template.showText(text);
template.endText();
 
Image tmpImage = Image.getInstance(template);
tmpImage.setAbsolutePosition(Utilities.millimetersToPoints(x),
    Utilities.millimetersToPoints(
        pageHeight - (y + Utilities.pointsToMillimeters(fh))));
tmpImage.setRotationDegrees(0);
document.add(tmpImage);

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():

ColumnText.showTextAligned(template,
    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.



Contact

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