Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set width of a <TD> when converting HTML to PDF using iTextSharp

On button click I need to read the HTML file and convert it to PDF. The PDF is generating without any problem. But the width of columns in table is equally distributed when converted to PDF. But I need the first column of my table to take 70% of the total size of my table (540)

How can I do this?

Template.html :

 <table runat="server" id="header" border="3" width="540">
        <tr>
            <td style="width:70%; text-align: center; font-weight: bold;">
                <strong>Test Specification </strong>
            </td>
            <td style="width:10%; text-align: center; font-weight: bold;">
                <strong>GST </strong>
            </td>
            <td style="width:10%; text-align: center; font-weight: bold;">
                <strong>Service </strong>
            </td>
            <td style="width:10%; text-align: center; font-weight: bold;">
                <strong>Amount </strong>
            </td>
        </tr>
    </table>

Button click to convert HTML to PDF :

    protected void Button1_Click(object sender, EventArgs e)
        {
            String htmlText = System.IO.File.ReadAllText(Request.PhysicalApplicationPath + "\\Template.htm");
      Document document = new Document();
            PdfWriter.GetInstance(document, new FileStream(Request.PhysicalApplicationPath + DateTime.Now.ToString("ddMMyyyy") + "_" + DateTime.Now.ToString("HHmmss tt") + ".pdf", FileMode.Create));
            document.Open();
      iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
            hw.Parse(new StringReader(HTML));
            document.Close();
            StringBuilder sb = new StringBuilder();
            sb.Append(HTML);
    }
like image 592
Anuya Avatar asked Nov 30 '22 22:11

Anuya


2 Answers

You need to use a combination of colspan and LoadTagStyle, both of which are outside the bounds of what I can fit in this textbox. To simplify, iTextSharp treats the total number of columns as 100% of the table, so a colspan of 5 on a table that is 10 columns should be treated like 50% of the table.

I have found that I have to play around with the values a bit, but hopefully that will lead you down the right path.

like image 136
David Brunow Avatar answered Dec 05 '22 09:12

David Brunow


you must set td width in percentage <td width="10%"> for td in each row, not only in header or first row.

like image 28
swapnil shahane Avatar answered Dec 05 '22 08:12

swapnil shahane