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

On to the "I need to read an HTML file and convert it to PDF" button. A PDF file is generated without any problems. But the width of the columns in the table is equally distributed when converting to PDF. But I need the first column of my table to occupy 70% of the total size of my table (540)

How can i do this? Any help please.

**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); } 
+4
source share
5 answers

You need to use a combination of colspan and LoadTagStyle, both of which are beyond what I can put in this text box. To simplify, iTextSharp treats the total number of columns as 100% of the table, so colspan 5 from the table, which is 10 columns, should be treated as 50% of the table.

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

+6
source

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

+4
source

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

+4
source

set width = "70%" and it should work.

 <td width="70%"> 
+3
source

his assistant professor. There is no way to set the width of the table cell. im using unusual code to change cell width.

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

* You need 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); } 
0
source

All Articles