简体   繁体   English

在“当前位置”绘制一个矩形,然后获取其位置坐标

[英]Draw a rectangle at the *current position* and then get its position coordinates

So, I am writing some text into a PDF file using iTextSharp. 因此,我正在使用iTextSharp将一些文本写入PDF文件。

After having added a few paragraphs and phrases to the PDF document, I want to: 在PDF文档中添加了几段和短语之后,我想要:

Draw the next piece of text on top of a rectangle that has a fill color, say, red. 在具有填充颜色(例如红色)的矩形上方绘制下一个文本。

I can compute the required width and height that the rectangle must have based on the text metrics of the text I am going to write on top of it. 我可以根据要在其上面编写的文本的文本度量来计算矩形必须具有的宽度和高度。 But how do I tell the Rectangle API what is top and left coordinates are, as in where it must be drawn? 但是,我如何告诉Rectangle API顶部和左侧坐标是什么,如必须在哪里绘制?

Seems that you are looking for the Chunk.setBackground() method. 似乎您在寻找Chunk.setBackground()方法。 This draws a colored background underneath some text. 这会在一些文本下方绘制一个彩色的背景。 There's also a variation of the method that takes extra parameters if you need a larger or smaller rectangle. 如果需要更大或更小的矩形,该方法还有一个变体,它需要额外的参数。

Suppose that you don't want a colored rectangle, but a custom type of shape, then you'd use the page event onGenericTag() . 假设您不希望使用彩色矩形,而是想要使用自定义类型的形状,则可以使用页面事件onGenericTag() See Chunk > Generic tag for more info. 有关更多信息,请参见块>通用标签

The onGenericTag() method is triggered every time a Chunk that is marked as generic (using the setGenericTag() method) is rendered to a page. onGenericTag()方法,每触发一次Chunk标记为通用(使用setGenericTag()方法)被渲染到一个页面。 Your implementation of the page event can then use the Rectangle value that is passed to the event method. 页面事件的实现然后可以使用传递给事件方法的Rectangle值。 It is important to understand that a single Chunk marked as a generic tag can result in multiple invocations of this method: if the contents of a single Chunk needs to be distributed over different lines, the event will be triggered as many times as there are lines (giving you a separate Rectangle value for every separate line). 重要的是要理解,标记为通用标记的单个Chunk可能导致此方法的多次调用:如果单个Chunk的内容需要分布在不同的行上,则事件的触发次数将与行数一样多(为每个单独的行都提供一个单独的Rectangle值)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM