0

I have a word document template which contains several form fields that need to be filled using c# code.

below is the document image enter image description here The code below is used to reach and fill the document form fields, But when i reach the table sections sometimes the rows need to be filled are more than what is pre defined inside the template.

red marked area is the table which i want to fill it with data and create as many rows as needed.

the code i use for filling the data is

        string fileName = Path.GetTempFileName();
        File.WriteAllBytes(fileName, Properties.Resources.DocumentTemplate);
        Word.Application word = new Word.Application();
        Word.Document doc = new Word.Document();
        doc = word.Documents.Add(fileName);
        doc.Activate();


        doc.FormFields["file_num"].Range.Text = "some text";
        doc.FormFields["fam_size"].Range.Text = "another text";
        doc.FormFields["nationality"].Range.Text = "another text";
        for(int i =0; i< rowsInDatabaseCount; i++)
        {
           //i don't know how to add row and reach each form field inside
        }

I hope someone can help me on how to achieve what i want.

Thank you in advance...

1 Answer 1

1

There are multiple ways to handle that. 1) Since the data is coming from a database, one way is to use InsertDatabase method.

2) You could insert the block as tab or comma separated text then convert to a table using ConvertToTable() method.

3) You might use Rows and Cols collections (of Table) and .Add method to add new rows.

4) You might instead create your template as an XSL and transform data (XML) using that XSL to generate final HTM. Word is able to open an HTM file.

(All these options worked for me in "resume", "test results" ... creations which have similar layouts to the ones you gave. I found final option 4 to be the most dynamic one, where end users could define their own templates using simple HTML editors - worked better than word templates for me).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.