单元格数据的 itext 颜色
itext color for cell data
我正在使用 itexsharp 创建 PDF,并且有一个 table 和 header 和行。我可以给我的 header 添加颜色,但是当我向我的行(单元格)添加颜色时,我不能这样做。
如何为我的行添加颜色。
示例:
..................
:Header1 : Header2 : //I have a sytle here allreadey
...................
:Row1 : Row2 : //I want to add style here?
....................
代码:
private String WritePDF(DataTable dt)
{
String fileName = "";
//Creating iTextSharp Table from the DataTable data
PdfPTable pdfTable = new PdfPTable(m_PDFColumnCount);
pdfTable.DefaultCell.Padding = 1;
pdfTable.WidthPercentage = 100;
pdfTable.HorizontalAlignment = Element.ALIGN_JUSTIFIED;
// pdfTable.DefaultCell.BorderWidth = 1;
//194 214 155
this.BuildPDFHeader(pdfTable, "date");
this.BuildPDFHeader(pdfTable, "time");
this.BuildPDFHeader(pdfTable, "result");
this.BuildPDFHeader(pdfTable, "fullname");
this.BuildPDFHeader(pdfTable, "regarding");
//Adding DataRow
for (int intIndex = 0; intIndex < dt.Rows.Count; intIndex++)
{
dt.Rows[intIndex]["details"] = getplaintext(dt.Rows[intIndex]["details"].ToString());
//Font verdana = FontFactory.GetFont("Verdana", 10, new Color(125, 88, 15));
//cell.BackgroundColor = new iTextSharp.text.Color(51, 102, 102);
pdfTable.AddCell(dt.Rows[intIndex]["date"].ToString());
pdfTable.AddCell(dt.Rows[intIndex]["time"].ToString());
pdfTable.AddCell(dt.Rows[intIndex]["result"].ToString());
pdfTable.AddCell(dt.Rows[intIndex]["fullname"].ToString());
pdfTable.AddCell(dt.Rows[intIndex]["regarding"].ToString());
PdfPCell cell = new PdfPCell(new Phrase(dt.Rows[intIndex]["details"].ToString()));
cell.Colspan = 5;
pdfTable.AddCell(cell);
}
String folderPath = "C:\PDFs\"; //should be in configfile.
fileName = String.Format("{0}{1}{2}",folderPath, dt.Rows[0]["id"].ToString(),".pdf" );
//Exporting to PDF
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
using (FileStream stream = new FileStream(fileName, FileMode.OpenOrCreate ))
{
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
PdfWriter.GetInstance(pdfDoc, stream);
pdfDoc.Open();
pdfDoc.Add(pdfTable);
pdfDoc.Close();
stream.Close();
}
return fileName;
}
private void BuildPDFHeader( PdfPTable pdfTable, String strText)
{
PdfPCell cell = new PdfPCell(new Phrase(strText));
cell.BackgroundColor = new iTextSharp.text.Color(51, 102,102);
pdfTable.AddCell(cell);
}
For your header row, you are creating the PdfPCell
objects yourself:
PdfPCell cell = new PdfPCell(new Phrase(strText));
cell.BackgroundColor = new iTextSharp.text.BaseColor(51, 102,102);
For the rest of the rows, you are asking iTextSharp to create the PdfPCell
objects:
pdfTable.AddCell(dt.Rows[intIndex]["date"].ToString());
This is a string
: dt.Rows[intIndex]["date"].ToString()
iText will create a PdfPCell
the same way you did when creating the PdfPCell
objects for the header, so one option is that you create the PdfPCell
objects for the other rows the same way:
PdfPCell cell = new PdfPCell(new Phrase(dt.Rows[intIndex]["date"].ToString()));
cell.BackgroundColor = new iTextSharp.text.BaseColor(60, 60, 60);
pdfTable.AddCell(cell);
However, there is a short cut. You can define the background color for the default cell:
pdfTable.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(60, 60, 60);
Now, the background color of each cell that you add as a string
with the addCell()
method will have the background color of the default cell.
Important: I see that you are using an obsolete version of iTextSharp that is 6 years old: you are using the Color
class instead of BaseColor
. Please be aware that there are technical and legal issues with that version. Please upgrade to a more recent version.
我正在使用 itexsharp 创建 PDF,并且有一个 table 和 header 和行。我可以给我的 header 添加颜色,但是当我向我的行(单元格)添加颜色时,我不能这样做。 如何为我的行添加颜色。
示例:
..................
:Header1 : Header2 : //I have a sytle here allreadey
...................
:Row1 : Row2 : //I want to add style here?
....................
代码:
private String WritePDF(DataTable dt)
{
String fileName = "";
//Creating iTextSharp Table from the DataTable data
PdfPTable pdfTable = new PdfPTable(m_PDFColumnCount);
pdfTable.DefaultCell.Padding = 1;
pdfTable.WidthPercentage = 100;
pdfTable.HorizontalAlignment = Element.ALIGN_JUSTIFIED;
// pdfTable.DefaultCell.BorderWidth = 1;
//194 214 155
this.BuildPDFHeader(pdfTable, "date");
this.BuildPDFHeader(pdfTable, "time");
this.BuildPDFHeader(pdfTable, "result");
this.BuildPDFHeader(pdfTable, "fullname");
this.BuildPDFHeader(pdfTable, "regarding");
//Adding DataRow
for (int intIndex = 0; intIndex < dt.Rows.Count; intIndex++)
{
dt.Rows[intIndex]["details"] = getplaintext(dt.Rows[intIndex]["details"].ToString());
//Font verdana = FontFactory.GetFont("Verdana", 10, new Color(125, 88, 15));
//cell.BackgroundColor = new iTextSharp.text.Color(51, 102, 102);
pdfTable.AddCell(dt.Rows[intIndex]["date"].ToString());
pdfTable.AddCell(dt.Rows[intIndex]["time"].ToString());
pdfTable.AddCell(dt.Rows[intIndex]["result"].ToString());
pdfTable.AddCell(dt.Rows[intIndex]["fullname"].ToString());
pdfTable.AddCell(dt.Rows[intIndex]["regarding"].ToString());
PdfPCell cell = new PdfPCell(new Phrase(dt.Rows[intIndex]["details"].ToString()));
cell.Colspan = 5;
pdfTable.AddCell(cell);
}
String folderPath = "C:\PDFs\"; //should be in configfile.
fileName = String.Format("{0}{1}{2}",folderPath, dt.Rows[0]["id"].ToString(),".pdf" );
//Exporting to PDF
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
using (FileStream stream = new FileStream(fileName, FileMode.OpenOrCreate ))
{
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
PdfWriter.GetInstance(pdfDoc, stream);
pdfDoc.Open();
pdfDoc.Add(pdfTable);
pdfDoc.Close();
stream.Close();
}
return fileName;
}
private void BuildPDFHeader( PdfPTable pdfTable, String strText)
{
PdfPCell cell = new PdfPCell(new Phrase(strText));
cell.BackgroundColor = new iTextSharp.text.Color(51, 102,102);
pdfTable.AddCell(cell);
}
For your header row, you are creating the PdfPCell
objects yourself:
PdfPCell cell = new PdfPCell(new Phrase(strText));
cell.BackgroundColor = new iTextSharp.text.BaseColor(51, 102,102);
For the rest of the rows, you are asking iTextSharp to create the PdfPCell
objects:
pdfTable.AddCell(dt.Rows[intIndex]["date"].ToString());
This is a string
: dt.Rows[intIndex]["date"].ToString()
iText will create a PdfPCell
the same way you did when creating the PdfPCell
objects for the header, so one option is that you create the PdfPCell
objects for the other rows the same way:
PdfPCell cell = new PdfPCell(new Phrase(dt.Rows[intIndex]["date"].ToString()));
cell.BackgroundColor = new iTextSharp.text.BaseColor(60, 60, 60);
pdfTable.AddCell(cell);
However, there is a short cut. You can define the background color for the default cell:
pdfTable.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(60, 60, 60);
Now, the background color of each cell that you add as a string
with the addCell()
method will have the background color of the default cell.
Important: I see that you are using an obsolete version of iTextSharp that is 6 years old: you are using the Color
class instead of BaseColor
. Please be aware that there are technical and legal issues with that version. Please upgrade to a more recent version.