Table Rendering in PDF

Hi, the size from the first table ist not correctly calculated:

Document doc = new Document(); 
DocumentBuilder docBuilder = new DocumentBuilder(doc);
docBuilder.InsertHtml(@" <table width=""100%""><tr><th>​gfgffgddfg</th>
<th >​sasasasaasas</th>
<th>​asassasasasasasasasasa asassasa</th>
<th>​asassasaassasaas</th>
<th>​assasasasasasa assa ssasaasasassasaasassaasasasasassaassaasas</th></tr></table>");
docBuilder.InsertHtml(@" <table width=""100%""><tr><td>​gfgffgddfg</td>
<td>​sasasasaasas</td>
<td>​asassasasasasasasasasa asassasa</td>
<td>​asassasaassasaas</td>
<td>​assasasasasasa assa ssasaasasassasaasassaasasasasassaassaasas</td></tr></table>"); 
doc.Save("test.pdf", SaveFormat.Pdf); 
doc.Save("test.docx", SaveFormat.Docx);

Can you fix that?

Hello

Thanks for your inquiry. I managed to reproduce the problem on my side. This appears to be happening because the width attribute as percent is not fully supported when HTML is imported. We are in the process of reworking the HTML import system and this feature may be included. We will keep you informed of any developments.
In the mean time you can however fit the table to the page with only one change to your existing HTML. You can call the FitTableToPageWidth method below which will autofit the table to the page width.

public static void FitTableToPageWidth(Table table)
{
    //Get the page settings from the section where the table occurs, as each section can have different page settings.
    Section section = (Section)table.GetAncestor(NodeType.Section);
 
    //First run simply gets the table size (the widest row). This is used to calculate the ratio below instead of just each row length
    //as the last cell in one row could be shorter than the last cell in the other row. This will preserve these different sizes when fitting.
    double tableWidth = 0;
 
    foreach (Row row in table.Rows)
    {
        double rowWidth = 0;
        foreach (Cell cell in row.Cells)
        {
            rowWidth += cell.CellFormat.Width;
            cell.CellFormat.FitText = true;
        }
 
        //If this row is larger than previous set this width as the longest row.
        if (rowWidth > tableWidth)
            tableWidth = rowWidth;
    }
 
    //Calculate the width of the page
    double pageWidth = section.PageSetup.PageWidth - (section.PageSetup.LeftMargin + section.PageSetup.RightMargin);
 
    //In the second run set each cell in the row proportionally to the width of the page
    foreach (Row row in table.Rows)
    {
        foreach (Cell cell in row.Cells)
        {
            //Calculate the ratio of each cell to the row width and then translate this ratio to the page width.
            double cellRatio = cell.CellFormat.Width / tableWidth;
            cell.CellFormat.Width = (cellRatio * pageWidth);
        }
    }
}

The method should be called for the tables in your document before saving like this:

Document doc = new Document();
DocumentBuilder docBuilder = new DocumentBuilder(doc);
docBuilder.InsertHtml(@"<table width='100%'><tr><th>​gfgffgddfg</th>
                        <th>​sasasasaasas</th>
                        <th>​asassasasasasasasasasa asassasa</th>
                        <th>​asassasaassasaas</th>
                        <th>​assasasasasasa assa ssasaasasassasaasassaasasasasassaassaasas</th></tr></table>");
 
docBuilder.InsertHtml(@"<table width='100%'><tr><td>​gfgffgddfg</td>
                        <td>​sasasasaasas</td>
                        <td>​asassasasasasasasasasa asassasa</td>
                        <td>​asassasaassasaas</td>
                        <td>​assasasasasasa assa ssasaasasassasaasassaasasasasassaassaasas</td></tr></table>");
 
foreach (Table table in doc.GetChildNodes(NodeType.Table, true))
{
    fitTableToPageWidth(table);
}
 
doc.Save("test.pdf", SaveFormat.Pdf);
doc.Save("test.docx", SaveFormat.Docx);

Best regards,

Any time line when this will be implemented? I need this functionality, too.

Thank you.

Hello

Thanks for your inquiry. Unfortunately, it is difficult to provide you any reliable estimate regarding this feature at the moment. You will be notified as soon as it is supported.
Best regards,

The issues you have found earlier (filed as WORDSNET-352) have been fixed in this .NET update and in this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(6)