How to get rid of my "After Spacing" on open xml

10,594

The line spacing needs to be appended to the paragraph properties and of course that needs to be appended to the paragraph.

Here is the long way to do it. The SpacingBetweenLines can also set the line height and the "rules" control how the before and after values are used.

SpacingBetweenLines spacing = new SpacingBetweenLines() { Line = "240", LineRule = LineSpacingRuleValues.Auto, Before = "0", After = "0" };
ParagraphProperties paragraphProperties = new ParagraphProperties();
Paragraph paragraph = new Paragraph();

paragraphProperties.Append(spacing);
paragraph.Append(paragraphProperties);

It looks like you are trying to set the line spacing to the table. That will not work that way(believe me I tried). Text around the table is controlled by the text wrapping and the positioning of the table.

Also when working with multiple tables, if you want to keep them separated there needs to be a paragraph(or something other then a table) after the table, otherwise your tables we merge together.

If you need that space create a paragraph with the font set to .5 or something really small and just add it after each table.

Share:
10,594
AustinT
Author by

AustinT

Software Developer. Groupies Inc. Co-founder & Chief Technology Officer University of Utah Computer Science Alumni. “The harder I work, the luckier I get.” iOS Developer Mobile: iOS, Swift Microsoft Technologies: C#, CRM Dynamics, ASP.NET MVC Web: Ruby on Rails, Javascript, CSS, HTML

Updated on June 09, 2022

Comments

  • AustinT
    AustinT almost 2 years

    In open XML my word document defaults to having "Spacing After: 10 pt" How would I change it to 0, so there is no spacing.

    Here is my code, which pretty much grabs the information from a database and places it onto a word document to be able to print out. But the spacing is making the document too big.

    using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(filepath,  WordprocessingDocumentType.Document)) {
        MainDocumentPart mainPart = wordDoc.AddMainDocumentPart();
        mainPart.Document = new Document();
        Body body = mainPart.Document.AppendChild(new Body());
    
        Paragraph para_main = body.AppendChild(new Paragraph());
        Run run_main = para_main.AppendChild(new Run());
    
        // Goes through all of the forms
        foreach (var form in forms) {
            Table table = new Table();
            // Initialize all of the table properties
            TableProperties tblProp = new TableProperties(
                new TableBorders(
                    new TopBorder() { Val = new EnumValue<BorderValues>(BorderValues.BasicBlackSquares), Size = 16 },
                    new LeftBorder() { Val = new EnumValue<BorderValues>(BorderValues.BasicBlackSquares), Size = 16 },
                    new RightBorder() { Val = new EnumValue<BorderValues>(BorderValues.BasicBlackSquares), Size = 16 },
                    new BottomBorder() { Val = new EnumValue<BorderValues>(BorderValues.BasicBlackSquares), Size = 16 },
                    new InsideHorizontalBorder() { Val = new EnumValue<BorderValues>(BorderValues.BasicBlackSquares), Size = 8 },
                    new InsideVerticalBorder() { Val = new EnumValue<BorderValues>(BorderValues.BasicBlackSquares), Size = 8 }
                ),
                new SpacingBetweenLines() { Before = "20", After = "20" }
                //new TableCellProperties(
                //    new 
                //new TableJustification() {Val = TableRowAlignmentValues.Center}
            );
    
            table.AppendChild<TableProperties>(tblProp);
    
            Paragraph para_header = body.AppendChild(new Paragraph());
            Run run_header = para_header.AppendChild(new Run());
            RunProperties runProps = run_header.AppendChild(new RunProperties(new Bold()));
    
            string username = form.Username;
            string proces_header = form.HeaderTitle;
    
            run_header.AppendChild(new Text(proces_header + " | " + username));
    
            for (int i = 0; i < form.FieldList.Count; i++) {
                if (!(form.FieldList[i].Token == "USR" || form.FieldList[i].Token == "SNT")) {
                    TableRow tr = new TableRow();
                    TableCell header_cell = new TableCell();
                    header_cell.Append(new Paragraph(new Run(new Text(form.FieldList[i].Label))));
                    TableCell value_cell = new TableCell();
                    value_cell.Append(new Paragraph(new Run(new Text(form.FieldList[i].Value))));
                    tr.Append(header_cell, value_cell);
                    table.Append(tr);
                }
            }
            wordDoc.MainDocumentPart.Document.Body.Append(table);
        }
        mainPart.Document.Save();
        wordDoc.Close();
        return "Success";
    }