none
restart subnheading number in paragraph RRS feed

  • Question

  • Hi,

    iam applying style to many paragrpahs .i am getting the ouput as

    1.  heading 1

      1.1 heading 2

      1.2 heading 3

    2. heading 1

     1.3 heading

    1.4 heading

    I need to do restart numbering in  subheadings .how to do that in open xml?

    The out put shuld come like

    1.  heading 1

      1.1 heading 2

      1.2 heading 3

    2. heading 1

     2.1 heading

    2.2 heading


    Tuesday, August 21, 2012 8:37 AM

Answers

  • Hi nikhita

    Before you can work with OPen XML you have to understand how the feature works in Word.

    This question has to do with how paragraph styles are linked to levels in a list template (numbering definition). The behavior is controlled by the numbering definition (the numbering.xml part). I don't see any code in what you show us that works with the part that defines the Numbering.

    If you want to use your own style names, rather than the built-in Heading 1, Heading 2, etc. then you will need to customize the numbering definition you apply to these paragraphs in order to have the levels use your style names. Once the numbering and styles are linked, then numbering will be applied when you apply the style.

    Possibly, you wouldn't need to do this "on-the-fly" (using code) if your starting point is an existing file. In that case, you can open the file in Word, create the two styles, link them to the numbering, save, and use that as your basis for the Open XML Documents you're generating.

    Following is some Word OPen XML that defines two (plus the start of the third) of the required nine levels for a numbering definition. The first two levels are linked to the paragraph styles Protokol Level 1 and Subtitle (just some styles I have on my machine). I'm not able to give you the Open XML SDK code in C# to do this, I'm only able to show you where to start.

    <w:abstractNum w:abstractNumId="2"><w:nsid w:val="39651384"/>
    <w:multiLevelType w:val="multilevel"/>
    <w:tmpl w:val="66C068C4"/>
    <w:styleLink w:val="TestSTyle1"/>
    <w:lvl w:ilvl="0"><w:start w:val="1"/><w:numFmt w:val="decimal"/>
    <w:pStyle w:val="ProtokollLevel1"/><w:lvlText w:val="%1)"/>
    <w:lvlJc w:val="left"/><w:pPr><w:ind w:left="360" w:hanging="360"/>
    </w:pPr><w:rPr><w:rFonts w:hint="default"/></w:rPr></w:lvl>
    <w:lvl w:ilvl="1"><w:start w:val="1"/>

    <w:numFmt w:val="decimal"/><w:pStyle w:val="Subtitle"/>
    <w:lvlText w:val="%1.%2)"/><w:lvlJc w:val="left"/><w:pPr>
    <w:ind w:left="720" w:hanging="360"/></w:pPr><w:rPr>
    <w:rFonts w:hint="default"/></w:rPr></w:lvl><w:lvl w:ilvl="2">
    <w:start w:val="1"/><w:numFmt w:val="lowerRoman"/>
    <w:lvlText w:val="%3)"/><w:lvlJc w:val="left"/><w:pPr>
    <w:ind w:left="1080" w:hanging="360"/></w:pPr><w:rPr>
    <w:rFonts w:hint="default"/></w:rPr></w:lvl>
    <w:lvl w:ilvl="3"><w:start w:val="1"/>


    Cindy Meister, VSTO/Word MVP

    Wednesday, August 22, 2012 2:33 PM
    Moderator

All replies

  • Hi nikitha

    From the look of it, you're applying to different "list templates" (numbering definitions) to the paragraphs at the first numbering level and the paragraphs of the second numbering level. That means the numbering will run independently in the two sets.

    You need to use the same numbering definition for all the paragraphs, just changing the level of the paragraphs.

    Here is an excerpt of Word Open XML for specifying the numbering applied to a paragraph:

       <w:numPr><w:ilvl w:val="1"/><w:numId w:val="12"/></w:numPr><w:rPr>

    w:ilvl w:val specifies the LEVEL. For a "main paragraph", you specify level 1; for a "sub paragraph" you should specify level 2.

    w:numId w:val specifies the numbering definition to use on the paragraph. This value should be the same for all the paragraphs.


    Cindy Meister, VSTO/Word MVP

    Tuesday, August 21, 2012 3:40 PM
    Moderator
  • Hi,

    Thanks for your quqick reply.But i want to code in c# .here is my code.My project is to create document from the scratch from the excel sheet and assigning styles.

    public static void ApplyStyleToParagraph(WordprocessingDocument doc, string styleid, string stylename, Paragraph p)
    {
        // If the paragraph has no ParagraphProperties object, create one.
        if (p.Elements<ParagraphProperties>().Count() == 0)
        {
            p.PrependChild<ParagraphProperties>(new ParagraphProperties());
        }

        // Get the paragraph properties element of the paragraph.
        ParagraphProperties pPr = p.Elements<ParagraphProperties>().First();

        // Get the Styles part for this document.
        StyleDefinitionsPart part =
            doc.MainDocumentPart.StyleDefinitionsPart;

        // If the Styles part does not exist, add it and then add the style.
        if (part == null)
        {
            part = AddStylesPartToPackage(doc);
            AddNewStyle(part, styleid, stylename);
        }
        else
        {
            // If the style is not in the document, add it.
            if (IsStyleIdInDocument(doc, styleid) != true)
            {
                // No match on styleid, so let's try style name.
                string styleidFromName = GetStyleIdFromStyleName(doc, stylename);
                if (styleidFromName == null)
                {
                    AddNewStyle(part, styleid, stylename);
                }
                else
                    styleid = styleidFromName;
            }
        }

        // Set the style of the paragraph.
        pPr.ParagraphStyleId = new ParagraphStyleId() { Val = styleid };
    }

    public static string GetStyleIdFromStyleName(WordprocessingDocument doc, string styleName)
    {
        StyleDefinitionsPart stylePart = doc.MainDocumentPart.StyleDefinitionsPart;
        string styleId = stylePart.Styles.Descendants<StyleName>()
            .Where(s => s.Val.Value.Equals(styleName) &&
                (((DocumentFormat.OpenXml.Wordprocessing.Style)s.Parent).Type == StyleValues.Paragraph))
            .Select(n => ((DocumentFormat.OpenXml.Wordprocessing.Style)n.Parent).StyleId).FirstOrDefault();
        return styleId;
    }

    private static void AddNewStyle(StyleDefinitionsPart styleDefinitionsPart, string styleid, string stylename)
    {
        // Get access to the root element of the styles part.
        DocumentFormat.OpenXml.Wordprocessing.Styles styles = styleDefinitionsPart.Styles;

        // Create a new paragraph style and specify some of the properties.
        DocumentFormat.OpenXml.Wordprocessing.Style style = new DocumentFormat.OpenXml.Wordprocessing.Style()
        {
            Type = StyleValues.Paragraph,
            StyleId = styleid,
            CustomStyle = true
        };
        StyleName styleName1 = new StyleName() { Val = stylename };
        BasedOn basedOn1 = new BasedOn() { Val = "Normal" };
        NextParagraphStyle nextParagraphStyle1 = new NextParagraphStyle() { Val = "Normal" };
        style.Append(styleName1);
        style.Append(basedOn1);
        style.Append(nextParagraphStyle1);

        // Create the StyleRunProperties object and specify some of the run properties.
        StyleRunProperties styleRunProperties1 = new StyleRunProperties();
        Bold bold1 = new Bold();
        DocumentFormat.OpenXml.Wordprocessing.Color color1 = new DocumentFormat.OpenXml.Wordprocessing.Color()
        {
            ThemeColor = ThemeColorValues.Accent2
        };
        RunFonts font1 = new RunFonts() { Ascii = "Lucida Console" };
        Italic italic1 = new Italic();
        // Specify a 12 point size.
        DocumentFormat.OpenXml.Wordprocessing.FontSize fontSize1 = new DocumentFormat.OpenXml.Wordprocessing.FontSize() { Val = "24" };
        styleRunProperties1.Append(bold1);
        styleRunProperties1.Append(color1);
        styleRunProperties1.Append(font1);
        styleRunProperties1.Append(fontSize1);
        styleRunProperties1.Append(italic1);

        // Add the run properties to the style.
        style.Append(styleRunProperties1);

        // Add the style to the styles part.
        styles.Append(style);
    }

    public static StyleDefinitionsPart AddStylesPartToPackage(WordprocessingDocument doc)
    {
        StyleDefinitionsPart part;
        part = doc.MainDocumentPart.AddNewPart<StyleDefinitionsPart>();
        DocumentFormat.OpenXml.Wordprocessing.Styles root = new DocumentFormat.OpenXml.Wordprocessing.Styles();
        root.Save(part);
        return part;
    }

    public static bool IsStyleIdInDocument(WordprocessingDocument doc, string styleid)
    {
        // Get access to the Styles element for this document.
        DocumentFormat.OpenXml.Wordprocessing.Styles s = doc.MainDocumentPart.StyleDefinitionsPart.Styles;

        // Check that there are styles and how many.
        int n = s.Elements<DocumentFormat.OpenXml.Wordprocessing.Style>().Count();
        if (n == 0)
            return false;

        // Look for a match on styleid.
        DocumentFormat.OpenXml.Wordprocessing.Style style = s.Elements<DocumentFormat.OpenXml.Wordprocessing.Style>()
            .Where(st => (st.StyleId == styleid) && (st.Type == StyleValues.Paragraph))
            .FirstOrDefault();
        if (style == null)
            return false;

        return true;
    }

    I will call this function in my code

    assigning the styles to each paragrpah.caN U TELL ME WHICH CODE I NEED TO CHANGE

    ApplyStyleToParagraph(activedoc, "Subheading", "Subheading", , nPara);

    Wednesday, August 22, 2012 6:25 AM
  • Hi nikhita

    Before you can work with OPen XML you have to understand how the feature works in Word.

    This question has to do with how paragraph styles are linked to levels in a list template (numbering definition). The behavior is controlled by the numbering definition (the numbering.xml part). I don't see any code in what you show us that works with the part that defines the Numbering.

    If you want to use your own style names, rather than the built-in Heading 1, Heading 2, etc. then you will need to customize the numbering definition you apply to these paragraphs in order to have the levels use your style names. Once the numbering and styles are linked, then numbering will be applied when you apply the style.

    Possibly, you wouldn't need to do this "on-the-fly" (using code) if your starting point is an existing file. In that case, you can open the file in Word, create the two styles, link them to the numbering, save, and use that as your basis for the Open XML Documents you're generating.

    Following is some Word OPen XML that defines two (plus the start of the third) of the required nine levels for a numbering definition. The first two levels are linked to the paragraph styles Protokol Level 1 and Subtitle (just some styles I have on my machine). I'm not able to give you the Open XML SDK code in C# to do this, I'm only able to show you where to start.

    <w:abstractNum w:abstractNumId="2"><w:nsid w:val="39651384"/>
    <w:multiLevelType w:val="multilevel"/>
    <w:tmpl w:val="66C068C4"/>
    <w:styleLink w:val="TestSTyle1"/>
    <w:lvl w:ilvl="0"><w:start w:val="1"/><w:numFmt w:val="decimal"/>
    <w:pStyle w:val="ProtokollLevel1"/><w:lvlText w:val="%1)"/>
    <w:lvlJc w:val="left"/><w:pPr><w:ind w:left="360" w:hanging="360"/>
    </w:pPr><w:rPr><w:rFonts w:hint="default"/></w:rPr></w:lvl>
    <w:lvl w:ilvl="1"><w:start w:val="1"/>

    <w:numFmt w:val="decimal"/><w:pStyle w:val="Subtitle"/>
    <w:lvlText w:val="%1.%2)"/><w:lvlJc w:val="left"/><w:pPr>
    <w:ind w:left="720" w:hanging="360"/></w:pPr><w:rPr>
    <w:rFonts w:hint="default"/></w:rPr></w:lvl><w:lvl w:ilvl="2">
    <w:start w:val="1"/><w:numFmt w:val="lowerRoman"/>
    <w:lvlText w:val="%3)"/><w:lvlJc w:val="left"/><w:pPr>
    <w:ind w:left="1080" w:hanging="360"/></w:pPr><w:rPr>
    <w:rFonts w:hint="default"/></w:rPr></w:lvl>
    <w:lvl w:ilvl="3"><w:start w:val="1"/>


    Cindy Meister, VSTO/Word MVP

    Wednesday, August 22, 2012 2:33 PM
    Moderator