How to prevent Microsoft Word from adding default paragraph spacing.

Home Forums WordprocessingML How to prevent Microsoft Word from adding default paragraph spacing.

This topic contains 2 replies, has 2 voices, and was last updated by  tatsuya 8 years, 6 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #3445

    tatsuya
    Participant

    I created a simple docx file named HelloWorld.docx using Open XML 2.5.
    Initially, HelloWorld.docx doesn’t contain styles.xml.

    However, after resaving HelloWorld.docx using Microsoft Word 2010,
    styles.xml is added to HelloWorld.docx, and it contains w:spacing as follows.

    <w:docDefaults>
      <w:pPrDefault>
        <w:pPr>
          <w:spacing w:after="200" w:line="276" w:lineRule="auto" />
        </w:pPr>
      </w:pPrDefault>
    <w/:docDefaults>
    

    How do I prevent Microsoft Word from adding w:spacing when resaving a docx file?

    I suspected Normal.dot and removed such default spacing from Normal.dot but it didn’t help.

    #3458

    Eric White
    Keymaster

    You cannot prevent Microsoft Word from doing this.

    Instead, I recommend that when you generate the document, create a styles part and put the docDefaults in it, with the default paragraph and run properties that you want for your document.

    #3490

    tatsuya
    Participant

    Thank you very much for the advice, which solved my problem.

    I wrote the following code.

    private static void Main()
    {
        using (var wd = WordprocessingDocument.Create("HelloWorld.docx", WordprocessingDocumentType.Document))
        {
            var mdp = wd.AddMainDocumentPart();
    
            AddStyleDefinitionPart(mdp);
            AddParagraphPropertiesDefault(mdp.StyleDefinitionsPart);
    
            mdp.Document = new Document(new Body());
            mdp.Document.Body.AppendChild(new Paragraph(new Run(new Text("Hello"))));
            mdp.Document.Body.AppendChild(new Paragraph(new Run(new Text("World"))));
    
            mdp.Document.Save();
            wd.Close();
        }
    }
    
    private static void AddStyleDefinitionPart(OpenXmlPartContainer oxpc)
    {
        new Styles().Save(oxpc.AddNewPart<StyleDefinitionsPart>());
    }
    
    private static void AddParagraphPropertiesDefault(StylesPart sp)
    {
        sp.Styles = new Styles(new DocDefaults(new ParagraphPropertiesDefault()));
    }
    

    Now, even after opening and resaving HelloWorld.docx using Microsoft Word,
    styles.xml remains the same as follows without w:spacing in it.

    <w:styles xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
      <w:docDefaults>
        <w:pPrDefault />
      </w:docDefaults>
    </w:styles>
    
    • This reply was modified 8 years, 6 months ago by  tatsuya.
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.