About us | News | Contact
iText ® Licenses Support   
You are here: Home > Forums > iText in Action — Second Edition > Part 1: Creating PDF documents from scratch > Chapter 4: Organizing content in tables
User login
  • Request new password

Tables, Cells, and background colors...

Submitted by jhasz on Fri, 02/25/2011 - 18:46

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!

‹ Tables in Paragraphs
  • 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:

  • Coordinates for the background: http://support.itextpdf.com/node/62
  • Font background color: http://support.itextpdf.com/node/63
  • Table in paragraph: http://support.itextpdf.com/node/64

I wonder why you're adding a PdfPTable with ColumnText when you could as well add a PdfPTable with writeSelectedRows()?

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.

  • Login to post comments

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

  • Login to post comments

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:

  1. You didn't provide a standalone example I could use to reproduce the problem. That's a common (and frustrating) problem described here: http://support.itextpdf.com/node/26
  2. When you did provide a source code snippet, I didn't understand it. I had to guess what it was supposed to do and rewrite it from scratch.

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 of cell2 directly to the table with addCell() in which case the properties of the default cell are used instead of the properties of cell2.

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.

  • Login to post comments
Content © 2010 1T3XT BVBA