SharpCoder
SharpCoder

Reputation: 19199

Unable to apply style using iTextSharp

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

Upvotes: 2

Views: 13451

Answers (2)

Bruno Lowagie
Bruno Lowagie

Reputation: 77606

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

Upvotes: 6

TCM
TCM

Reputation: 16920

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();
    }
}

Upvotes: 1

Related Questions