none
Removing Spacing in Tables and Paragraphs RRS feed

  • Question

  • Hi I'm trying to remove spacing in a Doc I'm producing with Open XML which has complex table structures. No matter what I do, my "No spacing" style applies 10pt spacing and Multiple line spacing at 1.15 times. I'm setting the style with this code:

    #region --- GenerateParagraphProperties ---
        public ParagraphProperties GenerateParagraphProperties()
        {
          try
          {
            ParagraphProperties paragraphProperties1 = new ParagraphProperties();
            ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "NoSpacing" };
            paragraphProperties1.Append(paragraphStyleId1);
            return paragraphProperties1;
          }
          catch (Exception ex)
          {
            return null;
          }
        }
        #endregion
    
        #region --- GetStyles ---
        private Styles GetStyles()
        {
          Styles styles = new Styles(
            new Style(
                new StyleName() { Val = "No Spacing" },
                new PrimaryStyle()
                ) { Type = StyleValues.Paragraph, StyleId = "NoSpacing", Default = true },
                new StyleParagraphProperties(
                  new SpacingBetweenLines() { After = "0", Line="0" }
                  )
                );            
          return styles;
        }
        #endregion

    And applying the generated styles to each table Row, the Main body paragraph and so on. but still the spacing persists. Any thoughts gratefully received.

    Thanks

    Philip

     

     

    Thursday, April 22, 2010 10:40 AM

Answers

  • I've fixed it, AT LAST. Example below.

    ParagraphProperties paragraphProperties1 = new ParagraphProperties(
              new ParagraphStyleId() { Val = "No Spacing" },
              new SpacingBetweenLines() { After = "0" }
              );
            return paragraphProperties1;
    Thanks for all your suggestions. The syntax is very fussy.
    • Marked as answer by Manonamission Friday, April 23, 2010 1:42 PM
    Friday, April 23, 2010 1:41 PM

All replies

  • Try not using Styles. Do it directly on the Paragraph properties

    Something like this

    ParagraphProperties paragraphProperties2 = new ParagraphProperties();

    SpacingBetweenLines spacingBetweenLines2 = new SpacingBetweenLines(){ After = "0" };

    paragraphProperties2.Append(spacingBetweenLines2);

    yourParagraph.Append(paragraphProperties2);

    Thursday, April 22, 2010 7:22 PM
  • Hi Philip,

    Thanks for your question.

    For your scenario, generally there are two steps, i.e. 1) creat a style and add it to StyleDefinitionsPart; 2) apply the style you defined to your destination paragraph. From your shared code, I guess you just create the new style but haven't added it to the StyleDefinitionsPart, which means the  Word application couldn't find the definition of the style you apply. You could try:  

    document.MainDocumentPart.GetPartsOfType< 

    StyleDefinitionsPart>()

    to get the StyleDefinitionsPart then use StyleDefinitionsPart.Styles to access the Styles.

    BTW, this how-to article might be helpful on the second step, you may need to check if there is ParagraphProperties and ParagraphStyleId existing in the document when creating a new one.

    Hope this helps. If you have any question, please let me know.

    Thanks,

    Lu

    Friday, April 23, 2010 2:21 AM
  • Hi thanks for both answers I'll see if I can fix it from what you've both said. Much appreciated.

    Philip

    Friday, April 23, 2010 9:07 AM
  • As an aside, I am adding the stlye to my document with the below where it is marked with ****. Is this wrong?

    MainDocumentPart mainPart = parent.AddMainDocumentPart();
          mainPart.Document = new Document();
          Body body = GenerateBody();
          mainPart.Document.Body = body;
          ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Jpeg);
          this._imagePartRID = mainPart.GetIdOfPart(imagePart);
          GenerateImagePart(imagePart, imageFileFolder + imageFileName, ref imageWidthEMU, ref imageHeightEMU);
          Table t = GenerateClientTable(imageFileName, imageWidthEMU, imageHeightEMU);
          body.Append(t);
    
          ****//Style set up
          StyleDefinitionsPart stylePart = mainPart.AddNewPart<StyleDefinitionsPart>();
          **** stylePart.Styles = GetStyles();
          
          
          // save
          parent.MainDocumentPart.Document.Save();
    Friday, April 23, 2010 9:15 AM
  • Hi Philip,

    Thanks for your sharing code.

    Now after adding the code between "****", could you run it successfully? If not, what's the error info do you get? Could you describe it in detail?

    Besides, you could also use the Productivity Tool to "Reflect Code" on a document which has the applied style to see how to add the style definitions.

    Hope this helps. If you have any question, please let me know.

    Thanks,

    Lu

    Friday, April 23, 2010 10:05 AM
  • Hi Lu, the code marked in **** is already included. When I run it and open the document, the word ribbon shows the "No spacing" style as the selected one, but if I look at the Paragraph options for this style it has the 10ot spacing and multiple line spacing in the style, as opposed to one created from scratch with the "No spacing" stlye where these values are set to zero. I've tried the Reflection tool, and It shows the No spacing style applied in the same way as I appear to have it. Puzzling....

    Friday, April 23, 2010 12:42 PM
  • Hi I've added this to my paragraphproperties, but it throws an exception on the 2nd line. Any ideas?
    ParagraphProperties paragraphProperties1 = new ParagraphProperties();
            paragraphProperties1.SpacingBetweenLines.After = "0";
            paragraphProperties1.SpacingBetweenLines.AfterLines = 0;
            paragraphProperties1.SpacingBetweenLines.Line = "0";
    Friday, April 23, 2010 1:18 PM
  • I've fixed it, AT LAST. Example below.

    ParagraphProperties paragraphProperties1 = new ParagraphProperties(
              new ParagraphStyleId() { Val = "No Spacing" },
              new SpacingBetweenLines() { After = "0" }
              );
            return paragraphProperties1;
    Thanks for all your suggestions. The syntax is very fussy.
    • Marked as answer by Manonamission Friday, April 23, 2010 1:42 PM
    Friday, April 23, 2010 1:41 PM
  • Hi Manonamission,

    I don't think you have to add an extra "SpacingBetweenLines" to ParagraphProperties, the style definition could include the "No Spacing" style. Below is my code which might be helpful:

    public

     

    static void TestApplyStyle(string filename) {

     

    using (var document = WordprocessingDocument.Open(filename, true))

    {

     

    //Add a new style definition

     

    StyleDefinitionsPart styleDefPart = document.MainDocumentPart.GetPartsOfType<StyleDefinitionsPart>().FirstOrDefault();

    AddNewStyleNoSpacing(styleDefPart);

     

    //change all the paragraph

     

    MainDocumentPart mainPart = document.MainDocumentPart;

     

    var paragraphs = mainPart.RootElement.Descendants<Paragraph>();

     

    foreach (var para in paragraphs)

    {

     

    if (para.Elements<ParagraphProperties>().Count() == 0)

    {

    para.PrependChild<

    ParagraphProperties>(new ParagraphProperties());

    }

    para.ParagraphProperties.RemoveAllChildren<

    ParagraphStyleId>();

    para.ParagraphProperties.InsertAt(

    new ParagraphStyleId() { Val = "NoSpacing" }, 0);

    }

    }

    }

     

    private static void AddNewStyleNoSpacing(StyleDefinitionsPart styleDefinitionsPart1)

    {

     

    // You need to deal with the situation that Styles is null

     

    Styles styles1 = styleDefinitionsPart1.Styles;

     

    Style style1 = new Style() { Type = StyleValues.Paragraph, StyleId = "NoSpacing", CustomStyle = true };

     

    StyleName styleName1 = new StyleName() { Val = "No Spacing" };

     

    StyleParagraphProperties styleParagraphProp = new StyleParagraphProperties(new SpacingBetweenLines() { After = "0"});

    style1.Append(styleName1);

    style1.Append(styleParagraphProp);

    styles1.Append(style1);

    }

     

    The code above runs well on my machine and compared with your code, I don't think you need to set "Line = 0".

    Hope this helps. If you have any question, please let me know.

    Thanks,

    Lu 

     

    Monday, April 26, 2010 9:49 AM
  • Thanks - that's great. I spent ages trying to find a solution!
    Monday, August 15, 2011 2:15 PM
  • Thanks, It helped me a lot. This is way that we can remove space and blank page is getting removed also.
    Thursday, September 5, 2019 2:32 PM