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); 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 ColumnText
class. 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.