简体   繁体   中英

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

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.

对于每行中的td,您必须为百分比<td width="10%">设置td宽度,而不仅仅是在标题或第一行中。

Use colspan property. If you have two cells defined as 10% and 90% for instance. Set colspan = 1 and 9 respectively. It does the trick.

设置width =“70%”它应该工作。

<td width="70%">

its dosent work. there is no any way to set the table cell width. im using unusual code for change the cell width.

with this sample code, you can find all tables and the find all rows. if row has 2 columns, cell widths set to 400 and 100!

*you have to change the code for your project!

            foreach (var htmlElement in parsedHtmlElements)
            {
                if (htmlElement is PdfPTable)
                {
                    var table = (PdfPTable)htmlElement;
                    foreach (var row in table.Rows)
                    {
                        PdfPCell[] pdfCellTemp = row.GetCells();
                        if (pdfCellTemp.Length == 2)
                        {
                            float[] w = { 400, 100 };
                            row.SetWidths(w);
                        }
                    }
                }
                pdfCell.AddElement(htmlElement);
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM