2012-12-11 12 views
8

In open XML il mio documento word ha come predefinito "Spacing After: 10 pt" Come lo cambierei a 0, quindi non c'è spazio.

Ecco il mio codice, che praticamente cattura le informazioni da un database e le colloca su un documento word per poterle stampare. Ma la spaziatura sta rendendo il documento troppo grande.Come sbarazzarsi del mio "After Spacing" su open xml

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"; 
} 
+1

Dove posso ottenere questa libreria, non riesco a trovare la DLL. Probabilmente devi fare qualcosa del tipo: wordDoc.MainDocumentPart.Document.Body.Paragraph.Spacing.After = 0; Ma non posso testarlo o cercare cose. – MrFox

risposta

7

spaziatura La linea deve essere aggiunto alle proprietà di paragrafo e, naturalmente, che deve essere aggiunto al paragrafo.

Ecco la lunga strada per farlo. SpacingBetweenLines può anche impostare l'altezza della linea e le "regole" controllano come vengono utilizzati i valori precedente e successivo.

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

Sembra che si stia tentando di impostare l'interlinea sul tavolo. Questo non funzionerà in questo modo (credimi ci ho provato). Il testo attorno al tavolo è controllato dall'avvolgimento del testo e dal posizionamento della tabella.

Anche quando si lavora con più tabelle, se si desidera tenerle separate, è necessario che ci sia un paragrafo (o qualcosa di diverso da un tavolo) dopo la tabella, altrimenti le tabelle verranno unite insieme.

Se è necessario lo spazio creare un paragrafo con il carattere impostato su .5 o qualcosa di veramente piccolo e aggiungerlo dopo ogni tabella.