DocumentBuilder InsertHtml Save as pdf will exceed page

I use Words’s DocumentBuilder InsertHtml with htmlTable string.
Then Save to PDF file, the table will exceed page
My code is below,

    var doc = new Document();
                doc.Styles.DefaultParagraphFormat.Style.Font.NameFarEast = "標楷體";
                doc.Styles.DefaultParagraphFormat.Style.Font.NameAscii = "Times New Roman";
                doc.Styles.DefaultParagraphFormat.Style.Font.Size = 16D;
                var builder = new DocumentBuilder(doc);
                var htmlTable = @"<style>
        .table {
            width: auto; 
            table-layout:fixed;
        }

        .table, .table tr {
            height: 40px;
        }

        .table, .table td {
            border-collapse: collapse;
            border: 1px solid #000000;
            padding:2px 5px 2px 5px;
        }
    </style>
    <table class='table' style='width: 100%;'>
        <colgroup>
            <col style='width: 15%;'>
            <col style='width: 35%;'>
            <col style='width: 15%;'>
            <col style='width: 35%;'>
        </colgroup>
        <tbody>
            <tr>
                <td>TITLE1:</td>
                <td></td>
                <td>TITLE2:</td>
                <td>106/10/14 22:26</td>
            </tr>
            <tr>
                <td>TITLE3:</td>
                <td colspan='3'>測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試</td>
            </tr>
        </tbody>
    </table>";

                builder.InsertHtml(htmlTable, true);
                doc.Save("TEST.pdf", SaveFormat.Pdf);

Thanks for your help

@rainmaker_ho,

Thanks for your inquiry. Please call Document.UpdateTableLayout method and set CompatibilityOptions.GrowAutofit property to false to get the desired output. CompatibilityOptions.GrowAutofit property allow Tables to AutoFit Into Page Margins.

builder.InsertHtml(htmlTable, true);

doc.CompatibilityOptions.GrowAutofit = false;
doc.UpdateTableLayout();
doc.Save(MyDir + "output.pdf");
1 Like

@tahir.manzoor,
It’s worked. Awesome.
Thanks for your help.