Apply FitText attribute for table html

Hi you guys,

I’m using DocumentBuilder.insertHtml() to embedded table html into word document. But I don’t know how to apply CellFormat.FitText attribute for table html.

Please give me solution for above issue.

Many thanks,
Hanh

Hi Hanh,

Thanks for your inquiry. I think, you need to implement INodeChangingCallback interface if you want to change values of the CellFormat object of a Cell i.e. being inserted via InsertHtml method. Please try run the following code snippet:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
HandleNodeChanging handler = new HandleNodeChanging();
doc.NodeChangingCallback = handler;
builder.InsertHtml(File.ReadAllText(MyDir + "HtmlPage1.html"));
foreach (Table table in handler.InsertedTables)
    foreach (Row row in table)
        foreach (Cell cell in row)
            cell.CellFormat.FitText = true;
doc.Save(MyDir + "out.docx");
public class HandleNodeChanging : INodeChangingCallback
{
    void INodeChangingCallback.NodeInserted(NodeChangingArgs args)
    {
        if (args.Node.NodeType == NodeType.Table)
            mInsertedTables.Add(args.Node);
    }
    void INodeChangingCallback.NodeInserting(NodeChangingArgs args)
    {
        // Do Nothing
    }
    void INodeChangingCallback.NodeRemoved(NodeChangingArgs args)
    {
        // Do Nothing
    }
    void INodeChangingCallback.NodeRemoving(NodeChangingArgs args)
    {
        // Do Nothing
    }
    public List<Node> InsertedTables
    {
        get { return mInsertedTables; }
    }
    private readonly List<Node> mInsertedTables = new List<Node>();
}

I hope, this helps.

Best regards,

Hi guys,

Many thanks for your answer but it isn’t my expect output. So I wonder If I choose right way.
So as to choose right way I would like to ask follow question : Different between using DocumentBuilder.cellFormat.FitText= true and cell.CellFormat.FitText= true. Because when I use two above method, I’ll get 2 different output.
I hope your answer will help me to get exact solution.

Thanks,
Hanh

Hi Hanh,

Thanks for your inquiry. There is no difference between these two methods. However, I would suggest you please use the following code for clarity:

Cell cell = builder.InsertCell();
cell.CellFormat.FitText = true;

Best regards,

Hi guys,

I tried to apply your code for my project but I didn’t have expect output.
My issue as following:
I used DocumentBuilder.InsertHtml() to insert below table html into document:

Risk Type Risk Level Risk Description
Business Risk Medium TC business processes are at severe risk due to the complexity and lack of total understanding of the business applications, system architecture, numerous technical TC vulnerabilities, lack of IT security expertise and insufficient disaster recovery.
There are a numerous threats with a reasonable likelihood of occurrence that can directly impact TC ability to conduct business as usual.
Legal Medium As a publicly traded company, executive leadership faces the threat of shareholder lawsuits challenging their due diligence, among many other potential adverse legal scenarios.
Many of the same factors that affect regulatory risk also expose TC to legal risk.

Please see my table and You will see: I set width for column 1st is 30%, column 2nd is 20% and column 3nd is 50%.
But when I exported document this table has wrong column size. I opened this document by MS Word then uncheck attribute: “Automatic resize Fittext content” so that this table will be had fine column size.
I don’t know how to set attribute : “Automatic resize Fittext content” for table html so that I send question to you guys and I received your answer but when I applied your code for my document, I received output in my attach file. Please see it.

As you see this output is so bad.

Please help me to review and give me few good advise.

Many thanks,
Hanh

Hi Hanh,

Thanks for your inquiry. Table.AllowAutoFit is the property corresponding to MS Word’s “Automatically resize to fit contents” option. Hope, this helps.

Best regards,