How to prevent Microsoft Word from adding default paragraph spacing.

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

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #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 9 years, 8 months ago by tatsuya.
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.