Question

I need to get absolute coordinates of paragraph that I already added to the document and join an image near that.

Generally my problem is below: I have a checklist with images (checked/unchecked) before each line. I already did that but if check item takes for example 2 lines, then second line starts from the begining of the page. What I want is to start this second line from position that first line is starting. It is equal to if the second line will have a margin. Thanks in advance!

Was it helpful?

Solution

I think your question is wrong. Allow me to explain: you have a specific requirement: you want to start a line with an image (representing a checked/unchecked check mark) that acts as a bullet. More specifically: you want the text that follows the bullet to be aligned correctly. That is a valid requirement.

However, in your question, you're asking about a specific implementation. You want to juggle with Y positions (check if a paragraph takes one or more lines) and X positions (start the second line using a specific indentation).

While it would probably be possible to achieve what you want using page events (asking a paragraph for its start and end postion), I think you are actually asking for functionality that is available out of the box: why not use a List with an image chunk as bullet?

I've written some sample code, ListWithImageAsBullet, where I use a light bulb as bullet (in your case, you'd use a checkbox image). I've added three items to the List and the second item takes more than one line. As you can see, the second line is indented correctly (you can augment the indentation using different methods available in the List class).

Please take a look at the resulting PDF. Is that what you're looking for?

If so, this is how it's done:

Image image = Image.getInstance(IMG);
image.scaleAbsolute(12, 12);
image.setScaleToFitHeight(false);
List list = new List();
list.setListSymbol(new Chunk(Image.getInstance(image), 0, 0));
list.add("Hello World");
list.add("This is a list item with a lot of text. It will certainly take more than one line. This shows that the list item is indented and that the image is used as bullet.");
list.add("This is a test");
document.add(list);

Note that I scaled the image to 12 by 12 pt, because 12pt is the default font size. Also don't forget to disable the automatic scaling of the image (otherwise, you'll end up with really tiny images as bullets).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top