我正在转换使用在线编辑器创建的文档.我需要能够使用它的中心点旋转文本,而不是(0,0).
由于Image使用中心旋转,我认为这可以工作,但事实并非如此.任何人都知道如何使用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);
Run Code Online (Sandbox Code Playgroud)
你为什么要使用beginText(),moveText(),showText(),endText()?这些方法将由能够流利地使用PDF语法的开发人员使用.其他开发人员应该避免使用那些低级方法,因为它们必然会出错.
例如:您正在setFontAndSize()文本对象外部使用该方法.该方法在图形状态下被禁止,您只能在文本状态下使用它.虽然Adobe Reader可能会容忍它,但Acrobat会在进行语法检查时抱怨.
建议不会流利地使用PDF语法的开发人员使用TextMethods示例中演示的便捷方法.看看text_methods.pdf."远离中心"这个文字可能就是你所需要的.
然而,甚至有一种更简单的方法来实现你想要的.只需使用类中showTextAligned()提供的静态方法即可ColumnText.这样,你甚至不需要使用beginText(),setFontAndSize(),endText():
ColumnText.showTextAligned(template,
Element.ALIGN_CENTER, new Phrase(text), x, y, rotation);
Run Code Online (Sandbox Code Playgroud)
使用该showTextAligned()方法ColumnText还具有以下优点:您可以使用包含具有不同字体的块的短语.