Why does the cell background color affect the color of other lines?
I'm creating a PDF where I add some text to each page as well as 2 lines that are drawn using the following method:
private void DrawLines(Document pdfDoc, PdfContentByte cb) { cb.MoveTo(0, 562); cb.LineTo(pdfDoc.PageSize.Width, 562); cb.MoveTo(0, 561); cb.LineTo(pdfDoc.PageSize.Width, 561); }
On one specific page, there's a table where I'm using the following code to change the background color for one particular cell:
header = new PdfPCell(new Phrase(market_data_list[i], grid_data_heading)); header.Colspan = 2; header.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER; header.BackgroundColor =new BaseColor(238,233,233); market_table.AddCell(header); //adds cell to the table
I now get the cell with the background color I specified (grey), but the lines change from black to grey... I want to draw those lines in black!
Posted on StackOverflow on Oct 28, 2014 by Annadate Piyush There are two problems with your code:
Problem #1: the method DrawLines()
doesn't draw any lines.
It creates the paths for two lines, but the lines are not drawn by that method. You need to add the following line:
cb.Stroke();
Without that line, drawing the lines is postponed until the stroke operator is called. That may never happen, in which case, the lines are never drawn. In your case, it happens when other content is drawn. By that time, the stroke color may have changed, in which case, the color used to draw the paths you've constructed in your DrawLines()
method is unpredictable.
Problem #2: you don't use best practices.
The colors that will be used to draw lines and shapes in your code are unpredictable because you aren't careful with the graphics state stack. Best practices would be to save and restore the graphics state when changing colors, line widths, etc...
I would change your DrawLines()
method like this:
private void DrawLines(Document pdfDoc, PdfContentByte cb) {
cb.SaveState();
cb.SetColorStroke(GrayColor.GRAYBLACK);
cb.MoveTo(0, 562);
cb.LineTo(pdfDoc.PageSize.Width, 562);
cb.MoveTo(0, 561);
cb.LineTo(pdfDoc.PageSize.Width, 561);
cb.Stroke();
cb.RestoreState();
}
Now you save the graphics state (SaveState()
) before you change the color to Black (SetRGBColorStroke()
). You construct the paths for the lines (using the LineTo()
and MoveTo()
method) and you draw those lines (Stroke()
). To make sure that the color change you applied doesn't affect other content you might be adding, you restore the graphics state stack to its previous state (RestoreState()
).
Click this link if you want to see how to answer this question in iText 7.