locked
how to insert multiple lines of text in content controls in word 2007\2010 using open xml RRS feed

  • Question

  • Hi,

    I have word 2007\2010 file with content controls in it which acts like a bookmark\placeholder

    I need to replace these content controls with exact values at runtime by the application.

    I am first trying to read the content controls in for loop and then replacing its value.

    The code below.

    The issue is i am not able to insert multiple lines in the control,for e.g if i have 3 lines to insert then all the 3 lines gets inserted as 1 line,it should be inserted into 3 different lines as the original text.

    Please find

    using (WordprocessingDocument doc = WordprocessingDocument.Open(stream, true))
    {
           MainDocumentPart mainPart = doc.MainDocumentPart;
           foreach (SdtElement sdtRun in mainPart.Document.Body.Descendants<SdtElement>())
           {
           string title = sdtRun.SdtProperties.GetFirstChild<SdtAlias>().Val.ToString().Trim();
           string bookMarkValue = string.Empty;

           if (sdtRun.Descendants<Text>().Count() == 3)
           {
                 sdtRun.Descendants<Text>().ElementAt(1).Text = string.Empty;
                 sdtRun.Descendants<Text>().ElementAt(2).Text = string.Empty;
           }

           sdtRun.Descendants<Text>().ElementAt(2).Text = "multiple lines of text";

     }

    }

    the code below.

     

    please let me know any suggestions.

     

     

    Thursday, March 15, 2012 9:39 AM

Answers

  •  

    Hi Ramesh,

    After my search, you can put the text in a table. Then you can achieve it with multiple lines.

            <table>

                <tr>

                    <td>A</td>

                </tr>

                <tr>

                    <td>B</td>

                </tr>

                <tr>

                    <td>C</td>

                </tr>

            </table>

    Here is the link, you can refer to it .

    http://msdn.itags.org/visual-studio/19726/

    Thanks,

    Jack

    Friday, March 16, 2012 7:49 AM
    Moderator

All replies

  •  

    Hi Ramesh,

    After my search, you can put the text in a table. Then you can achieve it with multiple lines.

            <table>

                <tr>

                    <td>A</td>

                </tr>

                <tr>

                    <td>B</td>

                </tr>

                <tr>

                    <td>C</td>

                </tr>

            </table>

    Here is the link, you can refer to it .

    http://msdn.itags.org/visual-studio/19726/

    Thanks,

    Jack

    Friday, March 16, 2012 7:49 AM
    Moderator
  • Hi,

    You could use a function like this:

    private static Run InsertFormatRun(Run run, string[] formatText)

            {

                foreach (string text in formatText)

                {

                    run.AppendChild(new Text(text));

                    RunProperties runProps = run.AppendChild(new RunProperties());

                    Break linebreak = new Break();

                    runProps.AppendChild(linebreak);

                }

                    return run;

              

            }

    Thursday, November 1, 2012 5:54 AM