Autofit tables to page width for PDF

Hi,
I have a query about autofitting a table to fit the page width of a PDF document. The PDF is generated by using a Word template and converting it to a PDF. The content for the documents are defined in HTML.
When the Word document is generated the table fits into the page for tables with cells that have large amounts of text, . However, when the same content is generated as a PDF (by converting the Word document to PDF) the tables overflow out of the page.
I have attached a Word and PDF outputs to demonstrate the problem - please refer to the very last table in the Word and PDF files. Also attached is the HTML text that was used for the initial rendering.
Is there anyway to correct the behaviour for the generated PDF by altering the Word document?
Thank you.

Hi Sunny,
Thanks for your request.
There is no one line API call to fit a table to page width at the moment. Instead you can try using an implementation built to achieve this functionality. The code is from this thread here. I have directly translated it to C# for you.

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

In your case you can call it like below before saving to PDF.

NodeCollection tables = doc.GetChildNodes(NodeType.Table, true);
Table table = (Table) tables[9];
fitTableToPageWidth(table);
doc.Save("Document out.pdf");

Thanks,

Thanks Adam. That’s resolved the issue.

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.
(1)