none
How to change the code to make the list items unbold and get the list items typed right after "obcservationArea"??? RRS feed

  • Question

  • using System;using System.Collections.Generic;using 
    System.Linq;using System.Text;using System.IO;using 
    DocumentFormat.OpenXml.Packaging;using 
    DocumentFormat.OpenXml.Wordprocessing;using 
    DocumentFormat.OpenXml;namespace 
    WordAutomation_inBay_Sample{class Program{static void 
    Main(string[] args){// convert template to 
    documentTemplateToDocument("inBaySampleTemplate.dotx", 
    "inBaySampleTemplate.docx");using (WordprocessingDocument wordDoc = 
    WordprocessingDocument.Open("inBaySampleTemplate.docx", 
    true)){List<SdtRun> sdtBlocks = new 
    List<SdtRun>();sdtBlocks.AddRange(wordDoc.MainDocumentPart.Document.Body.ChildElements.OfType<SdtRun>());foreach 
    (Paragraph para in 
    wordDoc.MainDocumentPart.Document.Body.OfType<Paragraph>()){var 
    moreBlocks = para.ChildElements.OfType<SdtRun>();if (moreBlocks != 
    null && moreBlocks.Count() > 
    0)sdtBlocks.AddRange(moreBlocks);}foreach (SdtRun block in 
    sdtBlocks){if (block.SdtProperties != null){Tag tag = 
    block.SdtProperties.ChildElements.OfType<Tag>().ElementAt(0);switch 
    (tag.Val.Value){// add text to Case:case 
    "caseTag":block.ModifyContent("New Case - 1");break;// add text 
    to Name:case "nameTag":block.ModifyContent("Your 
    Name");break;// check/uncheck check boxes.case 
    "virusCheckSelectionTag":block.UnCheck();// to make check, 
    val=1break;case "dataRecoverySelectionTag":block.Check(); // to make 
    uncheck, val=0break;// add dynamic listcase 
    "observationsArea":List<string> dynamicList = new List<string>() 
    { "List item 1..", "List item 2..", "List item 3..", "List item 4..", "List item 
    5..", "List item 6.." };List<Paragraph> newChildren = 
    CreateNewChildren(dynamicList);Paragraph oldChild = 
    block.Parent.NextSibling() as Paragraph;newChildren.ForEach(ch 
    =>{oldChild = wordDoc.MainDocumentPart.Document.Body.InsertAfter(ch, 
    oldChild);});break;}}}// save the 
    documentwordDoc.MainDocumentPart.Document.Save();}}private 
    static List<Paragraph> CreateNewChildren(List<string> 
    dynamicList){List<Paragraph> newParaCollection = new 
    List<Paragraph>();dynamicList.ForEach(item =>{Paragraph 
    paragraph = new Paragraph() { RsidParagraphMarkRevision = "008C2139", 
    RsidParagraphAddition = "008C2139", RsidParagraphProperties = "00050C00", 
    RsidRunAdditionDefault = "008C2139" };ParagraphProperties 
    paragraphProperties1 = new ParagraphProperties();ParagraphStyleId 
    paragraphStyleId1 = new ParagraphStyleId() { Val = "ListParagraph" 
    };NumberingProperties numberingProperties1 = new 
    NumberingProperties();NumberingLevelReference numberingLevelReference1 = new 
    NumberingLevelReference() { Val = 0 };NumberingId numberingId1 = new 
    NumberingId() { Val = 1 
    };numberingProperties1.Append(numberingLevelReference1);numberingProperties1.Append(numberingId1);ParagraphBorders 
    paragraphBorders1 = new ParagraphBorders();TopBorder topBorder1 = new 
    TopBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)6U, 
    Space = (UInt32Value)1U };BottomBorder bottomBorder1 = new BottomBorder() { 
    Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)6U, Space = 
    (UInt32Value)1U 
    };paragraphBorders1.Append(topBorder1);paragraphBorders1.Append(bottomBorder1);ParagraphMarkRunProperties 
    paragraphMarkRunProperties1 = new ParagraphMarkRunProperties();RunStyle 
    runStyle1 = new RunStyle() { Val = "Style1" };FontSize fontSize1 = new 
    FontSize() { Val = "27.5" 
    };paragraphMarkRunProperties1.Append(runStyle1);paragraphMarkRunProperties1.Append(fontSize1);paragraphProperties1.Append(paragraphStyleId1);paragraphProperties1.Append(numberingProperties1);// 
    paragraphProperties1.Append(paragraphBorders1);paragraphProperties1.Append(paragraphMarkRunProperties1);Run 
    run1 = new Run();RunProperties runProperties1 = new 
    RunProperties();RunStyle runStyle2 = new RunStyle() { Val = "Style3" 
    };FontSize fontSize2 = new FontSize() { Val = "27.5" 
    };FontSizeComplexScript fontSizeComplexScript1 = new FontSizeComplexScript() 
    { Val = "18" 
    };runProperties1.Append(runStyle2);runProperties1.Append(fontSize2);runProperties1.Append(fontSizeComplexScript1);Text 
    text1 = new Text();text1.Text = 
    item;run1.Append(runProperties1);run1.Append(text1);paragraph.Append(paragraphProperties1);paragraph.Append(run1);newParaCollection.Add(paragraph);});return 
    newParaCollection;}private static void TemplateToDocument(string 
    templateFile, string documentFile){MemoryStream presentationStream = 
    null;using (Stream tplStream = File.Open(templateFile, FileMode.Open, 
    FileAccess.Read)){presentationStream = new 
    MemoryStream((int)tplStream.Length);tplStream.Copy(presentationStream);presentationStream.Position 
    = 0L;}using (WordprocessingDocument wordPackage = 
    WordprocessingDocument.Open(presentationStream, 
    true)){wordPackage.ChangeDocumentType(DocumentFormat.OpenXml.WordprocessingDocumentType.Document);MainDocumentPart 
    docPart = wordPackage.MainDocumentPart;docPart.AddExternalRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/attachedTemplate",new 
    Uri(templateFile, 
    UriKind.RelativeOrAbsolute));docPart.Document.Save();}File.WriteAllBytes(documentFile, 
    presentationStream.ToArray());}}public static class 
    Extensions{public static void Copy(this Stream source, Stream 
    target){if (source != null){MemoryStream mstream = source as 
    MemoryStream;if (mstream != null) 
    mstream.WriteTo(target);else{byte[] buffer = new byte[2048];int 
    length = buffer.Length, size;while ((size = source.Read(buffer, 0, length)) 
    != 0)target.Write(buffer, 0, size);}}}public static void 
    ModifyContent(this SdtRun block, string content){SdtContentRun 
    contentBlock = 
    block.ChildElements.OfType<SdtContentRun>().ElementAt(0);Run run = 
    contentBlock.ChildElements.OfType<Run>().ElementAt(0);if (run != 
    null){Text text = run.GetFirstChild<Text>();text.Text = 
    content;}}public static void Check(this SdtRun 
    block){DocumentFormat.OpenXml.Office2010.Word.SdtContentCheckBox chkBox 
    = 
    block.SdtProperties.GetFirstChild<DocumentFormat.OpenXml.Office2010.Word.SdtContentCheckBox>();chkBox.Checked.Val.Value 
    = DocumentFormat.OpenXml.Office2010.Word.OnOffValues.True;}public 
    static void UnCheck(this SdtRun 
    block){DocumentFormat.OpenXml.Office2010.Word.SdtContentCheckBox chkBox 
    = 
    block.SdtProperties.GetFirstChild<DocumentFormat.OpenXml.Office2010.Word.SdtContentCheckBox>();chkBox.Checked.Val.Value 
    = DocumentFormat.OpenXml.Office2010.Word.OnOffValues.False;}}}

    I want to edit the code above to, make the list items unbold and get the list items typed right after "obcservationArea". Any ideas??

    For more infomation refer this link.

    http://openxmldeveloper.org/discussions/development_tools/f/35/p/5373/158788.aspx#158788

    • Moved by Bob Shen Monday, May 28, 2012 5:52 AM (From:Visual C# General)
    Friday, May 25, 2012 6:12 AM

All replies

  • Hi aroshlakshan,

    According to your description, I'd like to move this thread to Open XML Format SDK Forum for better support, where more experts live.

    Thanks for your understanding.

     

    Bob Shen [MSFT]
    MSDN Community Support | Feedback to us

    Monday, May 28, 2012 5:51 AM