0

I generate a Table object where some cells have a background-color. This background-color is dynamically loaded from a database.

I set the BackColor in my code with the following lines:

TableCell tCell = new TableCell();
tCell.BackColor = (Color)converter.ConvertFromString(color_startBorderCrtColor);
tCell.Text = Convert.ToString(row[column.ColumnName]);
tRow.Cells.Add(tCell);

When I append the rendered table to a StringBuilder and write it into a PDF using iTextSharp the Background-Color of the cells is not shown. Instead when I write the StringBuilder into a literal the cells are painted correctly.

Here is the code how I convert the Table and append it to my StringBuilder:

TextWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
Table myGenTable = (Table)genObjects[0];
myGenTable.RenderControl(hw);
sb.Append(tw.ToString()); //sb is the StringBuilder I'm working with

Is there a way where I can paint the ccells even in the pdf codument? The same problem exists with the border of the table which is set as attributes to the table itself with the following code:

tblcblCellsCQ.BorderColor = Color.Black;
tblcblCellsCQ.BorderWidth = 2;
tblcblCellsCQ.BorderStyle = BorderStyle.Dashed;

Here is the code where I write the StringBuilder into the PDF file:

StringReader sr = new StringReader(sb.ToString());
Document pdfDoc = new Document(PageSize.A4.Rotate(), 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + friendlyName + ".pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
Hack4Life
  • 563
  • 1
  • 11
  • 34
  • Your question is unclear: in iTextSharp, you have objects such as `PdfPTable` and `PdfPCell`. There is no `TableCell` object. There is no `TextWriter`, nor an `HtmlTextWriter` in iTextSharp. A `PdfPCell` doesn't have attributes such as `BorderStyle`. Your claim "Background Color is not shown in iTextSharp" in the title is very impolite and incorrect, because nowhere in your question can we see any proof that you are using iTextSharp. **Please rephrase your question:** either make it a real iTextSharp question or leave iTextSharp out of this! – Bruno Lowagie Nov 19 '15 at 14:06
  • @BrunoLowagie I do not use `PdfTable` or `PdfCell` because I need the generated table also for other parts in my website and not only in the pdf. I have added the code where I generate the pdf file. – Hack4Life Nov 19 '15 at 14:13
  • OK, I retracted my down-vote. You are using `HTMLWorker`. As explained in many answers on many questions **HTMLWorker has been abandoned in favor of XML Worker.** `HtmlWorker` is a *very incomplete* implementation of HTML to PDF. It is no longer supported. It should no longer be used. Switch to using iTextSharp's XML Worker and see if the problem persists. – Bruno Lowagie Nov 19 '15 at 14:16
  • Ok, I will have a look at it and try to update my code tomorrow. – Hack4Life Nov 19 '15 at 14:26
  • 1
    Take a look at these examples: [example 1](http://stackoverflow.com/questions/24530852/), [example 2](http://stackoverflow.com/questions/32765453/), [example 3](http://stackoverflow.com/questions/31020576/), [example 4](http://stackoverflow.com/questions/26679003/) and many more on SO. – Bruno Lowagie Nov 19 '15 at 14:26

1 Answers1

0

Try this :

for (int i = 0; i < col.Length; ++i)  
{  
   Cell cell = new Cell(new Phrase(col[i], new iTextSharp.text.Font(iTextSharp.text.Font.COURIER, 5)));  
   cell.Header = true;  
   cell.BackgroundColor = new iTextSharp.text.Color(204, 204, 204);       
   table.AddCell(cell);  
}

check the answer at : https://forums.asp.net/t/1577892.aspx?iTextSharp+table+headers

hakuna
  • 6,243
  • 10
  • 52
  • 77