wordpress hit counter
Re: Adding content controls dynamically and binding values to it using openXML - WordprocessingML - Formats - OpenXML Developer

Re: Adding content controls dynamically and binding values to it using openXML

Formats

Discussions about working with different Open XML Formats

Adding content controls dynamically and binding values to it using openXML

  • rated by 0 users
  • This post has 2 Replies |
  • 2 Followers
  • i am using a word document template which consist of some static content controls placed in it and while generating the word document i want to add some more content controls to the same template and bind values to it.

    i am using the following code to add the content controls but it is over writing the template.

     

        WordprocessingDocument doc = WordprocessingDocument.Create(@"c:\users\\documents\visual studio 2010\Projects\SdtBlocks\SdtBlocks\Template\myOpenXmlTest.docx", DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
           
           
              
                MainDocumentPart mainDocPart = doc.AddMainDocumentPart();
                Document dc = doc.MainDocumentPart.Document;
                mainDocPart.Document = new Document();
                mainDocPart.Document.Body = new Body();
    
    
                Paragraph tPara = new Paragraph();
                Run tRun = new Run();
    
                Text tText = new Text() { Space = SpaceProcessingModeValues.Preserve };
                tText.Text = "This is a test document";
    
                tRun.Append(tText);
                tPara.Append(tRun);
    
                mainDocPart.Document.Body.Append(tPara);
    
                // Add three SdtBlocks
                for (int i = 0; i < 1; ++i)
                {
    
                  
    
                    //mainDocPart.Document.Body.Append(tPara);
                    SdtBlock sdtBlock1 = new SdtBlock();
    
                    SdtProperties sdtProperties1 = new SdtProperties();
                    SdtAlias sdtAlias1 = new SdtAlias(); //{ Val = "ContnetControl1" };
                    sdtAlias1.Val = string.Format("Content Control : {0}", i + 1);
                    Tag tag1 = new Tag(); //{ Val = "ContnetControl1" };
                    tag1.Val = string.Format("Content Control : {0}", i + 1);
    
                    //SdtId sdtId1 = new SdtId() { Val = 1108931250 };
    
                    SdtId sdtId1 = new SdtId();
                    sdtId1.Val = 100 + i + 1;
    
                    SdtPlaceholder sdtPlaceholder1 = new SdtPlaceholder();
                    DocPartReference docPartReference1 = new DocPartReference(); //{ Val = "12094F6314EF4D9B85FCD4B9C39FFB77" };
    
                    sdtPlaceholder1.Append(docPartReference1);
                    ShowingPlaceholder showingPlaceholder1 = new ShowingPlaceholder();
    
                    sdtProperties1.Append(sdtAlias1);
                    sdtProperties1.Append(tag1);
                    sdtProperties1.Append(sdtId1);
                    sdtProperties1.Append(sdtPlaceholder1);
                    sdtProperties1.Append(showingPlaceholder1);
                    SdtEndCharProperties sdtEndCharProperties1 = new SdtEndCharProperties();
    
                    SdtContentBlock sdtContentBlock1 = new SdtContentBlock();
    
                    Paragraph paragraph1 = new Paragraph() { RsidParagraphAddition = "0042569B", RsidParagraphProperties = "0042569B", RsidRunAdditionDefault = "0042569B", ParagraphId = "08643F79", TextId = "77777777" };
    
                    Run run1 = new Run() { RsidRunProperties = "00342EFC" };
    
                    RunProperties runProperties1 = new RunProperties();
                    RunStyle runStyle1 = new RunStyle() { Val = "Heading2Char" };
    
                    runProperties1.Append(runStyle1);
                    Text text1 = new Text();
                    //text1.Text = "Click here to enter text.";
                    text1.Text = "Cick here to test";//string.Format("Content Control : {0}", i + 1);
                    
    
                    run1.Append(runProperties1);
                    run1.Append(text1);
    
                    paragraph1.Append(run1);
    
                    sdtContentBlock1.Append(paragraph1);
    
                    sdtBlock1.Append(sdtProperties1);
                    sdtBlock1.Append(sdtEndCharProperties1);
                    sdtBlock1.Append(sdtContentBlock1);
    
                    // I am getting error here 
    
    
                    IEnumerable<SdtBlock> contentControls = dc.Descendants<SdtBlock>();
                    foreach (SdtBlock ctrl in contentControls)
                    {
                        sdtAlias1 = ctrl.GetFirstChild<SdtProperties>().GetFirstChild<SdtAlias>();
    
                        if (sdtAlias1.Val != null && sdtAlias1.Val == "Content Control : 1")
                        {
                            WriteControls wt = new WriteControls();
                            wt.WriteToCOntentCOntrol(ctrl, "Hello");
                        }
                    }
                 
    
                    mainDocPart.Document.Body.Append(sdtBlock1);
    
    
    
                }

    Thanks In Advance, 
    Shiva. 

  • DynamicContentControls.rar

    Hi Shiva,

                 I have attached a sample solution, which actually creates a word document from the existing word document template. The template contains 2 static content controls. And in runtime, 3 more content controls are created and the data gets populated in all these content controls. Let me know if you face any problem.

    Thanks

  • Thanks alot pramodh.....your example helped me to build the document with dynamic controls.........

Page 1 of 1 (3 items)