2012-12-11 34 views
8

Trong XML mở, tài liệu từ của tôi mặc định có "Khoảng cách sau: 10 pt" Làm cách nào để thay đổi thành 0, vì vậy không có khoảng trắng.

Đây là mã của tôi, mà lấy khá nhiều thông tin từ một cơ sở dữ liệu và đặt nó vào một tài liệu từ để có thể in ra. Nhưng khoảng cách là làm cho tài liệu quá lớn.Làm thế nào để thoát khỏi "Sau khi khoảng cách" của tôi trên mở 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

Tôi có thể lấy thư viện ở đâu, dường như không thể tìm thấy dll. Bạn có thể phải làm điều gì đó như: wordDoc.MainDocumentPart.Document.Body.Paragraph.Spacing.After = 0; Nhưng tôi không thể kiểm tra hoặc tìm kiếm nội dung. – MrFox

Trả lời

7

Khoảng cách dòng cần được nối thêm vào thuộc tính đoạn và tất nhiên cần được nối thêm vào đoạn đó.

Đây là chặng đường dài để thực hiện. SpacingBetweenLines cũng có thể đặt chiều cao dòng và "quy tắc" kiểm soát cách sử dụng giá trị trước và sau.

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

Dường như bạn đang cố gắng đặt khoảng cách dòng vào bảng. Điều đó sẽ không hoạt động theo cách đó (tin tôi đi). Văn bản xung quanh bảng được kiểm soát bởi gói văn bản và vị trí của bảng.

Ngoài ra khi làm việc với nhiều bảng, nếu bạn muốn giữ chúng tách biệt, cần phải có một đoạn (hoặc một cái gì đó khác sau đó là bảng) sau bảng, nếu không các bảng của bạn chúng tôi hợp nhất với nhau.

Nếu bạn cần không gian đó tạo một đoạn có phông chữ được đặt thành 0,5 hoặc cái gì đó thật nhỏ và chỉ thêm nó sau mỗi bảng.

Các vấn đề liên quan