使用itext中的中心旋转文本 [英] Rotating text using center in itext

查看:891
本文介绍了使用itext中的中心旋转文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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).

由于Image使用中心旋转,我认为这样可行,但事实并非如此。任何人都知道如何使用itext中的中心点旋转文本?

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);


推荐答案

为什么使用 beginText () moveText() showText() ENDTEXT()?这些方法将由能够流利地使用PDF语法的开发人员使用。其他开发人员应该避免使用这些低级方法,因为它们必然会出错。

Why are you using beginText(), moveText(), showText(), endText()? Those methods are to be used by developers who speak PDF syntax fluently. Other developers should avoid using those low-level methods, because they are bound to make errors.

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

For instance: you're using the setFontAndSize() method outside a text object. That method is forbidden in graphics state, you can only use it in text state. Although Adobe Reader will probably tolerate it, Acrobat will complain when doing a syntax check.

建议不会说流利的PDF语法的开发人员使用便利方法,如 TextMethods 示例。请查看 text_methods.pdf 。文章Away again Center可能就是你所需要的。

Developers who don't speak PDF syntax fluently are advised to use convenience methods as demonstrated in the TextMethods example. Take a look at text_methods.pdf. The text "Away again Center" is probably what you need.

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

However, there's even 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);

在<使用 showTextAligned()方法code> ColumnText 还有一个优点,就是你可以使用包含不同字体的块的短语。

Using the showTextAligned() method in ColumnText also has the advantage that you can use a phrase that contains chunks with different fonts.

这篇关于使用itext中的中心旋转文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆