10

在打开的 XML 中,我的 word 文档默认设置为“之后的间距: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";
}
4

1 回答 1

15

行距需要附加到段落属性中,当然也需要附加到段落中。

这是很长的路要走。SpacingBetweenLines 还可以设置行高,“规则”控制如何使用前后值。

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);

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

此外,在处理多个表格时,如果您想将它们分开,表格之后需要有一个段落(或表格以外的其他内容),否则我们会将您的表格合并在一起。

如果您需要该空间,请创建一个字体设置为 0.5 或非常小的段落,然后将其添加到每个表格之后。

于 2013-03-20T13:25:14.740 回答