Itext pdf,如何将字符串绘制到特定位置? [英] Itext pdf, how to draw string to specific location?

查看:859
本文介绍了Itext pdf,如何将字符串绘制到特定位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Itext将一些字符串输出到PDF。目前我可以将它们打印到文档中,但我想将它们绘制/写入文档中的特定(x,y)。我怎么会这样呢?或者是否有任何教程或资源可以提供帮助?任何信息都将不胜感激。

I am trying to use Itext to output some strings to a PDF. Currently I can print them to the document but I would like to draw/write them to a specific (x,y) in the document. How would I go about this? Or is there any tutorial or resource that can help? Any info would be appreciated.

推荐答案

如果您使用的是iText 5,请转到常见问题解答部分,标题为文本的绝对定位

If you are using iText 5, please go to the FAQ section conveniently titled as Absolute positioning of text

您将找到诸如如何撰写等问题的答案页面上特定位置的Zapfdingbats字符?在回答该问题时,我们使用 ColumnText.showTextAligned()

You'll find answers to questions such as How to write a Zapfdingbats character at a specific location on a page? In the answer to that question, we use ColumnText.showTextAligned():

Phrase phrase = new Phrase(zapfstring, font);

其中 zapfstring 是一个包含任何Zapfdingbats的字符串你想要的字符和 font 是一个字体对象。然后我们将它添加到绝对位置:

Where zapfstring is a string containing any Zapfdingbats character you want and font is a Font object. Then we add it at an absolute position:

PdfContentByte canvas = writer.getDirectContent();
ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER, phrase, x, y, 0);

如果要在矩形内添加文本(如果文本不适合宽度,则为文本换行) ,看看这个问题:如何在矩形内添加文字?

If you want to add text inside a rectangle (text wraps if it doesn't fit the width), take a look at this question: How to add text inside a rectangle?

PdfContentByte canvas = writer.getDirectContent();
ColumnText ct = new ColumnText(canvas);
ct.setSimpleColumn(rect);
ct.addElement(new Paragraph("This is the text added in the rectangle"));
ct.go();

如果您使用的是iText 7,请使用 setFixedPosition()方法,例如在段落对象上。

If you are using iText 7, use the setFixedPosition() method, for instance on the Paragraph object.

这篇关于Itext pdf,如何将字符串绘制到特定位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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