简体   繁体   中英

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.

After having added a few paragraphs and phrases to the PDF document, I want to:

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?

Seems that you are looking for the Chunk.setBackground() method. 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() . 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. Your implementation of the page event can then use the Rectangle value that is passed to the event method. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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