• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I add a background color to my (pdf-) text using iText to create it with Java  RSS feed

 
Manuel Stein
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At first: My knowledge with frameworks which you can use for pdf creation isn't the best, please keep this in mind when answering.

I needed a framework/library with which I can create pdf-files in java and (important!) place text at certain x and y coordinates. After a lot of research I experienced that I can realize this with iText.


Here is a simple code snippet that basically shows what I'm doing right now with my text in iText. You can simply copy it into your programming environment, all you need is the iText jar (downloadable for you here: http://sourceforge.net/projects/itext/files/latest/download?source=files)



I have added possibilities to add my text from my datasources to this method and it's really working great. The result looks very promising to me and so is only one task for me left at the moment:

I need to add specfic background colors to my text (not font color!) that I'm moving and placing in the method shown above.

My research didn't provide me any beginner-friendly information about this task so I would be really happy if you could help me solve this.

If possible: can you modify my example in a way that adds a background color to the added example-text? I guess me and others (who may be reading this thread in the future having the same problem) would benefit most from this.

If you need further information or have additional suggestions for me please feel free to contact me as well.

Thanks for every answer and thought you're sharing with me.
 
Paul Clapham
Sheriff
Posts: 22697
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought this was an interesting question. So I googled and found the API documentation for iText.

I didn't know exactly what I was looking for but I noticed in the left frame a class named "BaseColor". That looked like it might be useful, so I clicked on it and brought up the class documentation in the right frame.

Then I went up to the menu at the top of the right frame and clicked on the "Use" link, to find out what used this BaseColor class.

There were a lot of methods which returned a Font which used a BaseColor -- I didn't see what that meant but I'm sure I could find out if I decided later that it might be useful. Then I looked a bit farther down and noticed that the Chunk method had a setBackground(BaseColor) method. This looked a lot like what you were asking.

That may or may not be the full answer to what you wanted to know. However it only took me a minute to find that out. I'm sure that if you needed something a bit different, you could find a solution for that as well by the technique of poking around in the documentation.
 
Manuel Stein
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:I thought this was an interesting question. So I googled and found the API documentation for iText.

I didn't know exactly what I was looking for but I noticed in the left frame a class named "BaseColor". That looked like it might be useful, so I clicked on it and brought up the class documentation in the right frame.

Then I went up to the menu at the top of the right frame and clicked on the "Use" link, to find out what used this BaseColor class.

There were a lot of methods which returned a Font which used a BaseColor -- I didn't see what that meant but I'm sure I could find out if I decided later that it might be useful. Then I looked a bit farther down and noticed that the Chunk method had a setBackground(BaseColor) method. This looked a lot like what you were asking.

That may or may not be the full answer to what you wanted to know. However it only took me a minute to find that out. I'm sure that if you needed something a bit different, you could find a solution for that as well by the technique of poking around in the documentation.


Thanks a lot for your answer. Focussing my research on what you've found I solved my problem using:



You define your text in a chunk (Chunk is just the smallest piece of text) and give this chunk your text as well as the Font (which also includes the size of your text). On your chunk you can setup the backgroundcolor and overall you're adding this to the Document with the shown "showTextAligned"-Method.

x and y here specify the coordinates and cb stands for the PdfContentByte.

And yea, you're right: It's possible to find a lot in the documentation, but somehow, I'm making my life alot harder from time to time since I just can't find the methods I'm searching for in the API as well as if I find something I'm having a hard time finding out how to use it without explanation. Anyway, I take your last sentence as constructive critique :) Thanks again.
 
Paul Clapham
Sheriff
Posts: 22697
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manuel Stein wrote:Anyway, I take your last sentence as constructive critique :)


Yes, that was how it was meant. But it's true, it isn't easy to learn from the API documentation. At best you can find classes or methods whose names look helpful, but when you're just starting out in a field and you don't even know the terminology, even that is difficult.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!