2013-08-01 41 views
2

我正在转换使用在线编辑器创建的文档。我需要能够使用它的中心点旋转文本,而不是(0,0)。在itext中使用中心旋转文本

由于图像使用中心旋转我认为这将工作,但它没有。任何人都有线索如何使用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); 

回答

7

你为什么要使用beginText()moveText()showText()endText()?这些方法将被流利使用PDF语法的开发人员使用。其他开发人员应该避免使用这些低级别的方法,因为他们必然会犯错误。

例如:您在文本对象外使用setFontAndSize()方法。该方法在图形状态下被禁止,您只能在文本状态下使用它。尽管Adobe Reader可能会容忍它,但Acrobat在进行语法检查时会发出抱怨。

建议不使用PDF语法的开发人员使用便利方法,如TextMethods示例中所示。看看text_methods.pdf。文字“再次离开中心”可能是您需要的。

然而,甚至有更简单的方法来实现你想要的。只需使用ColumnText类中提供的静态showTextAligned()方法即可。这样,你甚至不需要使用beginText()setFontAndSize()endText()

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

ColumnText使用showTextAligned()方法也有,你可以使用包含不同字体的块短语的优势。