Finally got it working
var inputString = @"<html>
<body>
<table class='table-bordered'>
<thead>
<tr>
<th>Customer Name</th>
<th>Customer's Address</th>
</tr>
</thead>
<tbody>
<tr>
<td> XYZ </td>
<td> Bhubaneswar </td>
</tr>
<tr>
<td> MNP </td>
<td> Cuttack </td>
</tr>
</tbody>
</table>
</body>
</html>";
List<string> cssFiles = new List<string>();
cssFiles.Add(@"/Content/bootstrap.css");
var output = new MemoryStream();
var input = new MemoryStream(Encoding.UTF8.GetBytes(inputString));
var document = new Document();
var writer = PdfWriter.GetInstance(document, output);
writer.CloseStream = false;
document.Open();
var htmlContext = new HtmlPipelineContext(null);
htmlContext.SetTagFactory(iTextSharp.tool.xml.html.Tags.GetHtmlTagProcessorFactory());
ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
cssFiles.ForEach(i => cssResolver.AddCssFile(System.Web.HttpContext.Current.Server.MapPath(i), true));
var pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(document, writer)));
var worker = new XMLWorker(pipeline, true);
var p = new XMLParser(worker);
p.Parse(input);
document.Close();
output.Position = 0;
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=myfile.pdf");
Response.BinaryWrite(output.ToArray());
// myMemoryStream.WriteTo(Response.OutputStream); //works too
Response.Flush();
Response.Close();
Response.End();
As you have discovered yourself, the main problem you experienced was caused by feeding invalid HTML to XML Worker. I have written a Java example that is equivalent to your (updated) C# example:
The resulting table looks like this:
You can tweak the values stored in
CSS
to create a nicer appearance for the table.