Hi George616,
iTextSharp supports external css file. So use iTextSharp. Add the latest dll from nuget to your project and add the css style in external css file.
Check this example. Now please take its reference and correct your code.
HTML
<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" />
StyleSheet.css
#tableDetails {
font-family: Arial;
font-size: 10pt;
width: 200px;
}
.header {
background-color: #18B5F0;
height: 18px;
color: White;
border: 1px solid white;
text-align: center;
}
Namespaces
C#
using System.Data;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
using iTextSharp.tool.xml.html;
using iTextSharp.tool.xml.parser;
using iTextSharp.tool.xml.pipeline.css;
using iTextSharp.tool.xml.pipeline.end;
using iTextSharp.tool.xml.pipeline.html;
VB.Net
Imports System.Data
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.tool.xml
Imports iTextSharp.tool.xml.html
Imports iTextSharp.tool.xml.parser
Imports iTextSharp.tool.xml.pipeline.css
Imports iTextSharp.tool.xml.pipeline.end
Imports iTextSharp.tool.xml.pipeline.html
Code
C#
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();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim dt As DataTable = 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"
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()
End If
End Sub
Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim sw As StringWriter = New StringWriter()
Dim hw As HtmlTextWriter = New HtmlTextWriter(sw)
pnlPerson.RenderControl(hw)
Dim sr As StringReader = New StringReader(sw.ToString())
Dim pdfDoc As Document = New Document()
Dim PdfWriter As PdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
Dim htmlContext As HtmlPipelineContext = New HtmlPipelineContext(Nothing)
htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory())
Dim cssResolver As ICSSResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(False)
cssResolver.AddCssFile(Server.MapPath("~/StyleSheet.css"), True)
Dim pipeline As IPipeline = New CssResolverPipeline(cssResolver, New HtmlPipeline(htmlContext, New PdfWriterPipeline(pdfDoc, PdfWriter)))
Dim worker = New XMLWorker(pipeline, True)
Dim 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()
End Sub
Screenshot
The Form
Exported PDF