如何在打开的xml上摆脱我的"After Spacing"

Aus*_*inT 9 c# ms-word openxml

在开放XML中,我的word文档默认为"Spacing After:10 pt"如何将其更改为0,因此没有间距.

这是我的代码,它几乎从数据库中获取信息并将其放在word文档上以便能够打印出来.但是间距使得文档太大了.

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";
}
Run Code Online (Sandbox Code Playgroud)

小智 11

行间距需要附加到段落属性,当然需要附加到段落.

这是很长的路要走.SpacingBetweenLines还可以设置行高,"rules"控制如何使用before和after值.

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);
Run Code Online (Sandbox Code Playgroud)

看起来您正在尝试将行间距设置为表格.这不会那样(相信我,我试过).表格周围的文本由文本换行和表格的定位控制.

此外,当处理多个表时,如果要将它们分开,则需要在表之后有一个段落(或者除了表之外的其他内容),否则我们将表合并在一起.

如果您需要该空间,请创建一个字体设置为.5的段落或者非常小的段落,并在每个表之后添加它.