I followed all the instructions on this link.
tr:nth-child(even) and tr:nth-child(odd) not reflecting in IText7 exported pdf, It only reflects on the apsx page not pdf.
is there any other way to add this effect
My code
CSS
StyleSheet .css
#tableDetails {
font-family: Arial;
font-size: 8px;
width: 200px;
border: 1px solid black;
}
.header {
background-color: #18B5F0;
height: 18px;
color: black;
border: 1px solid white;
text-align: center;
}
.div {
background-color: #18B5F0;
height: 18px;
color: black;
border: 1px solid white;
text-align: center;
}
tr:nth-child(even) {
background-color: #D1DDF1
}
tr:nth-child(odd) {
background-color: #EFF3FB
}
HTML
<div>
<link href="StyleSheet.css" rel="stylesheet" />
<asp:Panel ID="pnlPerson" runat="server">
<table border="1" class="tableDetails">
<tr>
<td colspan="2" class="header">
<b>Personal Details</b>
</td>
</tr>
<tr>
<td><b>Name</b></td>
<td>
<asp:Label ID="lblName" runat="server"></asp:Label></td>
</tr>
<tr>
<td><b>Age</b></td>
<td>
<asp:Label ID="lblAge" runat="server"></asp:Label></td>
</tr>
<tr>
<td><b>City</b></td>
<td>
<asp:Label ID="lblCity" runat="server"></asp:Label></td>
</tr>
<tr>
<td><b>Country</b></td>
<td>
<asp:Label ID="lblCountry" runat="server"></asp:Label></td>
</tr>
</table>
</asp:Panel>
<asp:Button ID="btnExport" runat="server" Text="Export" OnClick="btnExport_Click" />
</div>
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Populate DataTable
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Age");
dt.Columns.Add("City");
dt.Columns.Add("Country");
dt.Rows.Add();
dt.Rows[0]["Name"] = "Mudassar Khan";
dt.Rows[0]["Age"] = "27";
dt.Rows[0]["City"] = "Mumbai";
dt.Rows[0]["Country"] = "India";
//Bind Datatable to Labels
lblName.Text = dt.Rows[0]["Name"].ToString();
lblAge.Text = dt.Rows[0]["Age"].ToString();
lblCity.Text = dt.Rows[0]["City"].ToString();
lblCountry.Text = dt.Rows[0]["Country"].ToString();
}
}
protected void btnExport_Click(object sender, EventArgs e)
{
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
pnlPerson.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document();
PdfWriter PdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
cssResolver.AddCssFile(Server.MapPath("~/StyleSheet.css"), true);
IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(pdfDoc, PdfWriter)));
var worker = new XMLWorker(pipeline, true);
var xmlParse = new XMLParser(true, worker);
pdfDoc.Open();
xmlParse.Parse(sr);
xmlParse.Flush();
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
Why is the css not applying to the pdf?
Please help.