2013-08-01 40 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()方法也有,你可以使用包含不同字體的塊短語的優勢。