none
Do Paragraph Properties need to be in a specific order? RRS feed

  • Question

  • Do Paragraph Properties need to be in a specific order for the OpenXml SDK to properly view the object. Below is a small sample that I found while debugging some code. The interesting thing is that the indentation should be a part of the Paragraph Property, but it does not exist when viewing the serialized Paragraph Properties object in code.

    <w:pPr>
    	<w:spacing w:line="240" w:lineRule="auto" />
    	<w:rPr>
    		<w:rFonts w:cstheme="minorHAnsi" />
    	</w:rPr>
    	<w:ind w:left="2160" />
    </w:pPr>

    While reading the Paragraph Propertties on MSDN, there is no statement that mentions the child elements must be in a specific order. Is this a bug or should the Indentation not display on the Paragraph Properties object?

    Thanks!

    Tony

    Friday, April 12, 2013 8:30 PM

Answers

  • Hi Cindy,

    While creating an OpenXML document using the SDK, we are appending the following properties on the paragraph in this order: RunFonts, SpacingBetweenLines, Indentation, ParagraphMarkRunProperties. This works as expected and is displayed in the Correct sample below.

    If we happened to change the order to RunFonts, SpacingBetweenLines, ParagraphMarkRunProperties, and Indentation, then the Indentation property is ignored and not displayed in the finished document. This was our issue and is displayed in the Incorrect Sample at the bottom.

    It seems that users ought to be careful when appending properties to the ParagraphProperties object in code, as they are dependent on a specific order.

    Correct:

    using DocumentFormat.OpenXml.Wordprocessing;
    using DocumentFormat.OpenXml;
    namespace GeneratedCode
    {
        public class GeneratedClass
        {
            // Creates an ParagraphProperties instance and adds its children.
            public ParagraphProperties GenerateParagraphProperties()
            {
                ParagraphProperties paragraphProperties1 = new ParagraphProperties();
                SpacingBetweenLines spacingBetweenLines1 = new SpacingBetweenLines(){ Line = "240", LineRule = LineSpacingRuleValues.Auto };
                Indentation indentation1 = new Indentation(){ Left = "1440" };
                ParagraphMarkRunProperties paragraphMarkRunProperties1 = new ParagraphMarkRunProperties();
                RunFonts runFonts1 = new RunFonts(){ ComplexScriptTheme = ThemeFontValues.MinorHighAnsi };
                paragraphMarkRunProperties1.Append(runFonts1);
                paragraphProperties1.Append(spacingBetweenLines1);
                paragraphProperties1.Append(indentation1);
                paragraphProperties1.Append(paragraphMarkRunProperties1);
                return paragraphProperties1;
            }
        }
    }


    Incorrect:

    using DocumentFormat.OpenXml.Wordprocessing;
    using DocumentFormat.OpenXml;
    namespace GeneratedCode
    {
        public class GeneratedClass
        {
            // Creates an ParagraphProperties instance and adds its children. 
            //The indentation is appended out of order and will be ignored by the Paragraph Properties object.
            public ParagraphProperties GenerateParagraphProperties()
            {
                ParagraphProperties paragraphProperties1 = new ParagraphProperties();
                SpacingBetweenLines spacingBetweenLines1 = new SpacingBetweenLines(){ Line = "240", LineRule = LineSpacingRuleValues.Auto };
                Indentation indentation1 = new Indentation(){ Left = "1440" };
                ParagraphMarkRunProperties paragraphMarkRunProperties1 = new ParagraphMarkRunProperties();
                RunFonts runFonts1 = new RunFonts(){ ComplexScriptTheme = ThemeFontValues.MinorHighAnsi };
                paragraphMarkRunProperties1.Append(runFonts1);
                paragraphProperties1.Append(spacingBetweenLines1);
                paragraphProperties1.Append(paragraphMarkRunProperties1);
                paragraphProperties1.Append(indentation1);
                return paragraphProperties1;
            }
        }
    }

    Thank you,

    Tony

    Monday, April 15, 2013 3:56 PM

All replies

  • Hi Tony

    I'm not sure I'm following your scenario. What do you mean by " it does not exist when viewing the serialized Paragraph Properties object in code"?


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, April 13, 2013 7:06 AM
    Moderator
  • Hi Cindy,

    Sorry for being confusing. When debugging, the Paragraph Properties object displays that the Indentation Property is set to null. It seems that the paragraph properties in that specific order does not serialize the Indentation property.

    If the <w:ind> object is before the <w:rPr> object in xml as shown below, then the Indentation displys properly when debugging.

    <w:pPr>
    	<w:spacing w:line="240" w:lineRule="auto" />
    	<w:ind w:left="2160" />
    	<w:rPr>
    		<w:rFonts w:cstheme="minorHAnsi" />
    	</w:rPr>
    </w:pPr>

    Thank you,

    Tony

    Monday, April 15, 2013 1:16 AM
  • Hi Tony

    I'd really appreciate more information - repro steps - for this. I now understand (I think) what you mean by "debugging", but I still don't know how you're getting there...

    Is the resulting document correct when opened in Word?

    Why is it important that this information appear correctly while debugging? I agree it "would be nice", but what's the practical reason for its needing to do so.


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, April 15, 2013 8:07 AM
    Moderator
  • Hi Cindy,

    While creating an OpenXML document using the SDK, we are appending the following properties on the paragraph in this order: RunFonts, SpacingBetweenLines, Indentation, ParagraphMarkRunProperties. This works as expected and is displayed in the Correct sample below.

    If we happened to change the order to RunFonts, SpacingBetweenLines, ParagraphMarkRunProperties, and Indentation, then the Indentation property is ignored and not displayed in the finished document. This was our issue and is displayed in the Incorrect Sample at the bottom.

    It seems that users ought to be careful when appending properties to the ParagraphProperties object in code, as they are dependent on a specific order.

    Correct:

    using DocumentFormat.OpenXml.Wordprocessing;
    using DocumentFormat.OpenXml;
    namespace GeneratedCode
    {
        public class GeneratedClass
        {
            // Creates an ParagraphProperties instance and adds its children.
            public ParagraphProperties GenerateParagraphProperties()
            {
                ParagraphProperties paragraphProperties1 = new ParagraphProperties();
                SpacingBetweenLines spacingBetweenLines1 = new SpacingBetweenLines(){ Line = "240", LineRule = LineSpacingRuleValues.Auto };
                Indentation indentation1 = new Indentation(){ Left = "1440" };
                ParagraphMarkRunProperties paragraphMarkRunProperties1 = new ParagraphMarkRunProperties();
                RunFonts runFonts1 = new RunFonts(){ ComplexScriptTheme = ThemeFontValues.MinorHighAnsi };
                paragraphMarkRunProperties1.Append(runFonts1);
                paragraphProperties1.Append(spacingBetweenLines1);
                paragraphProperties1.Append(indentation1);
                paragraphProperties1.Append(paragraphMarkRunProperties1);
                return paragraphProperties1;
            }
        }
    }


    Incorrect:

    using DocumentFormat.OpenXml.Wordprocessing;
    using DocumentFormat.OpenXml;
    namespace GeneratedCode
    {
        public class GeneratedClass
        {
            // Creates an ParagraphProperties instance and adds its children. 
            //The indentation is appended out of order and will be ignored by the Paragraph Properties object.
            public ParagraphProperties GenerateParagraphProperties()
            {
                ParagraphProperties paragraphProperties1 = new ParagraphProperties();
                SpacingBetweenLines spacingBetweenLines1 = new SpacingBetweenLines(){ Line = "240", LineRule = LineSpacingRuleValues.Auto };
                Indentation indentation1 = new Indentation(){ Left = "1440" };
                ParagraphMarkRunProperties paragraphMarkRunProperties1 = new ParagraphMarkRunProperties();
                RunFonts runFonts1 = new RunFonts(){ ComplexScriptTheme = ThemeFontValues.MinorHighAnsi };
                paragraphMarkRunProperties1.Append(runFonts1);
                paragraphProperties1.Append(spacingBetweenLines1);
                paragraphProperties1.Append(paragraphMarkRunProperties1);
                paragraphProperties1.Append(indentation1);
                return paragraphProperties1;
            }
        }
    }

    Thank you,

    Tony

    Monday, April 15, 2013 3:56 PM
  • Hi Tony

    I'm going to take a look at this in the next couple of days, but...

    Which version of the Open XML SDK are you using? 2.0 or 2.5?

    There were some issues in 2.0 that have been fixed in 2.5. If you're using 2.0 would you mind doing a quick test against 2.5?

    A document created using 2.5 is exactly the same as one created using 2.0, so if you're currently using 2.0 and find the problem is gone in 2.5 it would be a good idea to "upgrade" your project.


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, April 15, 2013 3:59 PM
    Moderator
  • Hi Cindy,

    We are currently targeting Open XML SDK 2.0. I have changed our code to not append the Indentation property in the current case which fixes our issue.

    I will see if I can test this in 2.5. However, it will be sometime before we are able to upgrade to 2.5.

    Thank you,

    Tny

    Monday, April 15, 2013 4:17 PM