Tables, Cells, and background colors...
I know you mentioned in my other thread about drawing the rectangle and filling it for the background color, however, my problem is that I don't see a way to discover the 4 corners of the text that I wish to put a color behind. It is dynamic (might be in a different place vertically on the page) and could also be different lengths. It is also contained within a paragraph within a paragraph as in:
|------------------|
| master paragraph |
||----------------||
||Header paragraph||
||----------------||
||----------------||
|| Body Paragraph ||
|| Some addtional ||
|| text content.. ||
||----------------||
|------------------|
And it's part or all of the content in the header paragraph that needs to have a background color to highlight it. It would be easy if there were a "background color" attribute in the Font, but I know there isn't, so, I need to find another way.
Which brings me to tables and cells. I know there is a background color attribute on PdfPCell, so I'm attempting to re-factor a portion of the code to utilize that. However, I can't seem to get the table that contains the cell, that contains the header paragraph to be placed into the master paragraph and show up on the rendered document.
Apparently it is "allowed" to add the table to a paragraph (that is then added as usual to my Column) because I don't get objections from the compiler, or exceptions at runtime. So, I wonder what I'm doing wrong.
Basically, what I'm doing looks something like this:
Paragraph masterParagraph = new Paragraph();
PdfPCell cell = new PdfPCell();
cell.addElement(new Paragraph("Header Text", myFont));
cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
PdfPTable table = new PdfPTable(1);
table.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
masterParagraph.add(table);
masterParagraph.add(new Paragraph("body text", myOtherFont));
ct.addElement(masterParagraph);
Of course, the ct.addElement(...) is within my other code that determines if I need a new page, etc... that was discussed in the other thread...
When I do the above, it renders the space for the element, but not the element itself.
Any ideas as to what I might be missing?
Thanks!
- Login to post comments

Please don't write such long questions
Submitted by Bruno Lowagie on Sat, 02/26/2011 - 09:32.I won't answer your questions here, I'll deal with them in separate posts:
I wonder why you're adding a
PdfPTablewithColumnTextwhen you could as well add aPdfPTablewithwriteSelectedRows()?You want this:
|------------------|| . master table . |
||----------------||
|| . nested table ||
||----------------||
||----------------||
|| Body Paragraph ||
|| Some addtional ||
|| text content.. ||
||----------------||
|------------------|
That's nothing more than a special table, isn't it?
Edit: your subsequent question addresses this. I'm reading the questions in chronological order.
Cell borders?
Submitted by jhasz on Fri, 02/25/2011 - 19:49.Ok, so I'm converting some of my code to use Tables and cells rather than the way I had it. Yeah, even tho it allows it (as in no compile errors, no exceptions thrown), you apparently can't put a table into a paragraph and have it added to a ColumnText object and actually be rendered... so changing things to use tables to add to the ColumnText and it works ... now I have other issues.
I build a table - basically it's two rows right now, and I can turn off the border of the cell that is the first row, but not the cell that is the second row. What's happening? cell2.setBorder(PdfPCell.NO_BORDER) has no effect?
Any ideas?
Thanks
Please clean up your code
Submitted by Bruno Lowagie on Sat, 02/26/2011 - 09:32.I noticed something when you posted one of your previous questions: http://support.itextpdf.com/node/58
Something didn't work, but I couldn't help you rightaway because:
When I rewrote your code from scratch, you replied "Now it works!"
I'm sure that if I wrote a small standalone example to draw a table with two borderless cells, I wouldn't be able to reproduce the problem, and you'd reply once more: "Now it works!"
So I suggest you clean up your code, and I'm pretty sure you'll discover why a border is drawn. For instance: you're working with nested tables, and the border you see isn't part of cell2, but it's a border in the master table (or vice-versa), or maybe you think you're adding
cell2, but in reality you're adding the content ofcell2directly to the table withaddCell()in which case the properties of the default cell are used instead of the properties ofcell2.Those are possible reasons that pop into my mind, but I can't tell you if they apply to your case. I just know that answering "it doesn't work"-questions is a very ungrateful job.