Unable to set multiline while editing Word ContentControl using SDTElement

Home Forums WordprocessingML Unable to set multiline while editing Word ContentControl using SDTElement

This topic contains 1 reply, has 2 voices, and was last updated by  Eric White 7 years, 10 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #4141

    SATISD9X
    Participant

    The following code sets the text inside the ContentControl from a Word Template. The ContentControls are iterated through WordProcessingDocument objects SDTElement’s SDTAlias value and if found set the appropriate desired text/date etc.

    	    byte[] byteArray = File.ReadAllBytes("Test.docx");
                using (MemoryStream mem = new MemoryStream())
                {
                    mem.Write(byteArray, 0, (int)byteArray.Length);
                    using (WordprocessingDocument wordDoc =
                        WordprocessingDocument.Open(mem, true))
                    {
                        MainDocumentPart mainPart = wordDoc.MainDocumentPart;
    
                        foreach (SdtElement sdtEle in mainPart.Document.Descendants<SdtElement>())
                        {
                            string sdType = sdtEle.GetType().Name;
                            SdtAlias alias = sdtEle.Descendants<SdtAlias>().FirstOrDefault();
                            string sdtTitle = alias.Val.Value;
                            Run run = new Run();
    
                            if (alias != null)
                            {
                                // delete all paragraph of the sdt
                                sdtEle.Descendants<Paragraph>().ToList().ForEach(p => p.Remove());
                                
                                switch (alias.Val.Value)
                                {
                                    case "TOPIC":
                                        var t = sdtEle.Descendants<Text>().FirstOrDefault();
                                        t.Text = "Text for Via Fedex1";
                                        break;
    
                                    case "DATE":
                                        var pd = sdtEle.Descendants<Text>().FirstOrDefault();
                                        pd.Text = "02/08/2017";
                                        break;
    
                                    case "ADDRESS":
                                        if(sdType.ToUpper() == "SDTRUN")
                                        { 
                                            var sca = sdtEle.Descendants<Text>().FirstOrDefault();
                                            //sca.Text = "ABC" + Environment.NewLine + "XYZ";
                                            
                                            run.AppendChild(new Text("ABC"));
                                            run.AppendChild(new Break());
                                            run.AppendChild(new Text("XYZ"));
                                            
                                        }
                                        else if(sdType.ToUpper() == "SDTBLOCK")
                                        {
                                            var t = sdtEle.Descendants<Text>().FirstOrDefault();
                                        	t.Text = "Another Address";
                                        	break;
    
                                            
                                        }
                                        break;
    
                                }
    
                            }
                        }
    
                    }
                    
                    using (FileStream fileStream = new FileStream("Test1.docx",
                        System.IO.FileMode.CreateNew))
                    {
                        mem.WriteTo(fileStream);
                    }

    But in the above code, the multiline is NOT set to ContentControl (ADDRESS in above code) using the below lines
    //sca.Text = “ABC” + Environment.NewLine + “XYZ”;
    OR
    //run.AppendChild(new Text(“ABC”));
    //run.AppendChild(new Break());
    //run.AppendChild(new Text(“XYZ”));

    Please assist.

    #4142

    Eric White
    Keymaster

    Hi,

    Looking at your code, nothing looks incorrect to me, but I normally use LINQ to XML to access and manipulate content instead of the strongly-typed object model, so I may be missing something.

    In general, if you need to generate documents from data, I recommend using the DocumentAssembler module in Open-Xml-PowerTools.

    http://www.ericwhite.com/blog/blog/documentassembler-developer-center/

    Watch those videos – you can do document assembly without writing code.

    If you go down that path, and if you continue to use the strongly-typed object model, I recommend closing and opening the document any time you switch between using LINQ to XML and the strongly-typed object model.

    Best, Eric

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.