Unable to apply style using iTextSharp
13,170
Solution 1
You're using HTMLWorker
, an obsolete class that is no longer supported. It doesn't support CSS files and that explains why all questions about iText, HTML and CSS remain unanswered. You should use XML Worker instead of the HTMLWorker
class. See http://sourceforge.net/projects/itextsharp/files/xmlworker and http://demo.itextsupport.com/xmlworker
Solution 2
Please try the below code. I've tested and it applied all styles:
class Program
{
private static void createPDF(string html)
{
FileStream fs = new FileStream("test.pdf", FileMode.Create);
TextReader reader = new StringReader(html);
Document document = new Document(PageSize.A4, 30, 30, 30, 30);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
HTMLWorker worker = new HTMLWorker(document);
document.Open();
worker.StartDocument();
worker.Parse(reader);
worker.EndDocument();
worker.Close();
document.Close();
fs.Close();
}
static void Main(string[] args)
{
string data = @"<table cellspacing='0' border='0' style='border-collapse:collapse;margin-top:30px;color:red;'>
<tr>
<td></td><td>Visit</td><td></td><td></td><td></td>
</tr><tr>
<th>Datw</th><td>07/01/2013</td><td>07/18/2013</td><td>07/17/2013</td><td>07/09/2013</td>
</tr><tr>
<th>Score</th><td>3.00</td><td>6.33</td><td>1.00</td><td>8.00</td>
</tr><tr>
<th>Heading</th><td>7.0</td><td>8.0</td><td>2.0</td><td>3.0</td>
</tr><tr>
<th>Minutes</th><td>88</td><td>n/a</td><td>22</td><td>n/a</td>
</tr><tr>
<th>Test Data</th><td>5.0</td><td>8.0</td><td>4.0</td><td>3.0</td>
</tr><tr>
<th>Status</th><td>8.0</td><td>8.0</td><td>3.0</td><td>3.0</td>
</tr><tr>
<th>Data </th><td>3.96</td><td>6.88</td><td>5.83</td><td>6.67</td>
</tr><tr>
<th>Assessment (0-10)</th><td>5.0</td><td>3.0</td><td>2.0</td><td>2.0</td>
</tr><tr>
<th class='seperator'>With heading</th><td>n/a</td><td>n/a</td><td>n/a</td><td>n/a</td>
</tr>
</table>";
createPDF(data);
Console.ReadKey();
}
}
Related videos on Youtube
Author by
SharpCoder
Updated on June 04, 2022Comments
-
SharpCoder almost 2 years
I am using following code to generate PDF using .iTextSharp version 5.4.3.
// Create a Document object var document = new Document(PageSize.A4, 50, 50, 25, 25); // Create a new PdfWriter object, specifying the output stream var output = new MemoryStream(); var writer = PdfWriter.GetInstance(document, output); // Open the Document for writing document.Open(); string data = @"<table cellspacing="0" border="0" style="border-collapse:collapse;margin-top:30px;;"> <tr> <td></td><td>Visit</td><td></td><td></td><td></td> </tr><tr> <th>Datw</th><td>07/01/2013</td><td>07/18/2013</td><td>07/17/2013</td><td>07/09/2013</td> </tr><tr> <th>Score</th><td>3.00</td><td>6.33</td><td>1.00</td><td>8.00</td> </tr><tr> <th>Heading</th><td>7.0</td><td>8.0</td><td>2.0</td><td>3.0</td> </tr><tr> <th>Minutes</th><td>88</td><td>n/a</td><td>22</td><td>n/a</td> </tr><tr> <th>Test Data</th><td>5.0</td><td>8.0</td><td>4.0</td><td>3.0</td> </tr><tr> <th>Status</th><td>8.0</td><td>8.0</td><td>3.0</td><td>3.0</td> </tr><tr> <th>Data </th><td>3.96</td><td>6.88</td><td>5.83</td><td>6.67</td> </tr><tr> <th>Assessment (0-10)</th><td>5.0</td><td>3.0</td><td>2.0</td><td>2.0</td> </tr><tr> <th class="seperator">With heading</th><td>n/a</td><td>n/a</td><td>n/a</td><td>n/a</td> </tr> </table>"; IElement ele; PdfPTable t; var stringWriter = new StringWriter(); StyleSheet styles = new StyleSheet(); styles.LoadStyle("seperator", "border-top", "#a9a9a9 2px solid"); List<IElement> htmlarraylist = HTMLWorker.ParseToList(new StringReader(data), styles); for (int k = 0; k < htmlarraylist.Count; k++) { ele = htmlarraylist[k]; document.Add(ele); //document.Add((IElement)htmlarraylist[k]); } document.Close(); Response.ContentType = "application/pdf"; Response.AddHeader("Content-Disposition", "attachment;filename=Receipt-test.pdf"); Response.BinaryWrite(output.ToArray());
While its generating PDF, its not applying styling. Also its not showing tags in bold.
Am I missing something? Thank you
-
SharpCoder almost 11 yearsThank you quick reply and adding an example. I have following problems: 1) Its not showing the data in <TH> tags in bold, which is normally the case with an HTML table. 2) The last <TH> tag is having a class by the name 'seperator', this class adds border at the top of the cell, this class is not applied. Can you please verify it again.
-
TCM almost 11 yearsYes, th doesn't take bold by default in iTextsharp. You can apply bold specially using style='font-weight:bold;'. Regarding separator it doesn't work because you have specified border='0'. Remove that and you should be good to go.
-
SharpCoder almost 11 yearsI can understand about 'th' tag but even after removing border from the table, I am not seeing the style being applied. Also, in your code you are not telling explicitly to include/apply the styling.
-
TCM almost 11 yearshmmm StyleSheet don't work many of the times. I think it is not properly implemented in PDFSharp. Instead try specifying inline CSS directly and it would work.