我生成一个Table对象,其中一些单元格具有背景颜色。此背景颜色是从数据库动态加载的。
我在代码中使用以下行设置BackColor:
TableCell tCell = new TableCell();
tCell.BackColor = (Color)converter.ConvertFromString(color_startBorderCrtColor);
tCell.Text = Convert.ToString(row[column.ColumnName]);
tRow.Cells.Add(tCell);
当我将渲染的表格附加到StringBuilder
并使用iTextSharp
将其写入PDF时,单元格的Background-Color
未显示。相反,当我将 StringBuilder 写入文字时,单元格被正确绘制。
以下是我如何转换表格并将其附加到我的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
有没有办法可以在pdf文件中绘制ccells? 使用以下代码将表的边界设置为表本身的边界存在同样的问题:
tblcblCellsCQ.BorderColor = Color.Black;
tblcblCellsCQ.BorderWidth = 2;
tblcblCellsCQ.BorderStyle = BorderStyle.Dashed;
以下是我将StringBuilder编写到PDF文件中的代码:
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();
答案 0 :(得分:0)
试试这个:
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);
}
检查答案:https://forums.asp.net/t/1577892.aspx?iTextSharp+table+headers