2013-09-05 32 views
3

我首次使用MigraDoc庫並需要幫助。我使用MigraDoc以pdf格式打印表格。在我的表格中,3行形成一個組。所以每個3行組必須在同一頁,但在我目前的代碼,這不會發生。在添加Migradoc中的最後3行之前檢查頁面結尾是否接近

它將在同一頁上打印第1行或第2行,如果頁面結束,第3行進入下一頁。

public void CreateDocument() 
{ 
this.document = new Document { Info = { Title = "Time Sheet Report By Job", } }; 
this.DefineStyles(); 
this.CreatePage(); 
this.FillContent(); 
} 

public void FillContent() 
{ 
    int x = 1; 
    foreach (XElement r in this.xReport.Element(ElrepTable).Elements(ElrepRow)) 
    { 
     Row row1 = this.table.AddRow(); 
     int i = 0; 
     row1.Cells[0].Borders.Visible = false; 
     foreach (XElement c in r.Elements(ElrepCell)) 
     { 
      row1.Cells[i++].AddParagraph(c.Value); 
     } 

     if (x++ % 3 != 0) 
     { 
      continue; 
     } 

     row1.Borders.Bottom.Width = 1; 
     row1.Cells[0].Borders.Bottom.Visible = true; 
     row1.Cells[0].Borders.Bottom.Width = 1; 
    } 

    int colCount = this.GetColCount(); 
    int rowCount = this.GetRowCount(); 

    this.table.SetEdge(1, 1, colCount - 1, rowCount - 1, Edge.Box, BorderStyle.Single, 0.05); 
} 


private void CreatePage() 
{ 
    Section section = this.document.AddSection(); 
    section.PageSetup.LeftMargin = "1cm"; 
    section.PageSetup.RightMargin = "1cm"; 
    section.PageSetup.BottomMargin = "2cm"; 
    section.PageSetup.TopMargin = "4.5cm"; 

    // Put a logo in the header 
    string imgPath = this.SaveImg(Resource1.MMS_Logo, "logo"); 
    Image image = section.Headers.Primary.AddImage(imgPath); 
    image.Height = "2cm"; 
    image.LockAspectRatio = true; 
    image.RelativeVertical = RelativeVertical.Line; 
    image.RelativeHorizontal = RelativeHorizontal.Margin; 
    image.Top = ShapePosition.Top; 
    image.Left = ShapePosition.Center; 
    image.WrapFormat.Style = WrapStyle.Through; 

    imgPath = this.SaveImg(Resource1.shadow, "shadow"); 
    Image image2 = section.Headers.Primary.AddImage(imgPath); // .Headers.Primary 
    image2.Height = "1.5cm"; 
    image2.Width = "5cm"; 
    image2.RelativeVertical = RelativeVertical.Line; 
    image2.RelativeHorizontal = RelativeHorizontal.Margin; 
    image2.Top = ShapePosition.Top; 
    image2.Left = ShapePosition.Right; 
    image2.WrapFormat.Style = WrapStyle.Through; 

    this.leftTextFrame = section.Headers.Primary.AddTextFrame(); 
    this.leftTextFrame.Height = "10.0cm"; 
    this.leftTextFrame.Width = "7.0cm"; 
    this.leftTextFrame.RelativeHorizontal = RelativeHorizontal.Margin; 
    this.leftTextFrame.RelativeVertical = RelativeVertical.Page; 
    this.leftTextFrame.Left = ShapePosition.Left; 
    this.leftTextFrame.Top = "1.75cm"; 

    this.centerTextFrame = section.Headers.Primary.AddTextFrame(); 
    this.centerTextFrame.Height = "5.0cm"; 
    this.centerTextFrame.Width = "6.0cm"; 
    this.centerTextFrame.RelativeHorizontal = RelativeHorizontal.Margin; 
    this.centerTextFrame.RelativeVertical = RelativeVertical.Page; 
    this.centerTextFrame.Left = ShapePosition.Center; 
    this.centerTextFrame.Top = "3.5cm"; 

    this.rightTextFrame = section.Headers.Primary.AddTextFrame(); 
    this.rightTextFrame.Height = "3.0cm"; 
    this.rightTextFrame.Width = "5.0cm"; 
    this.rightTextFrame.RelativeHorizontal = RelativeHorizontal.Margin; 
    this.rightTextFrame.RelativeVertical = RelativeVertical.Page; 
    this.rightTextFrame.Left = ShapePosition.Right; 
    this.rightTextFrame.Top = "1.75cm"; 

    Dictionary<string, string> jobdet = this.GetJobDetails(); 

    // Put sender in address frame 
    Paragraph p1 = this.leftTextFrame.AddParagraph(); 
    var f = new Font { Bold = true, Size = 8 }; 
    p1.AddFormattedText(this.GetTitle(), f); 
    p1.AddLineBreak(); 
    p1.AddLineBreak(); 
    p1.AddFormattedText("Account Information", new Font { Bold = true, Italic = true, Size = 7 }); 
    p1.AddLineBreak(); 

    Paragraph p2 = this.leftTextFrame.AddParagraph(); 
    p2.Format.LeftIndent = "2cm"; 

    p2.AddFormattedText("Job #:", new Font { Bold = true, Size = 6 }); 
    p2.AddTab(); 
    p1.AddTab(); 
    p2.AddFormattedText(this.GetJobNo(), new Font { Size = 6 }); 
    p2.AddLineBreak(); 

    p2.AddFormattedText("Job Name:", new Font { Bold = true, Size = 6 }); 
    p2.AddTab(); 
    p2.AddFormattedText(jobdet["Name"], new Font { Size = 6 }); 
    p2.AddLineBreak(); 

    p2.AddFormattedText("Address:", new Font { Bold = true, Size = 6 }); 
    p2.AddTab(); 
    p2.AddFormattedText(jobdet["Address"], new Font { Size = 6 }); 
    p2.AddLineBreak(); 

    Paragraph cp1 = this.centerTextFrame.AddParagraph(); 
    cp1.AddFormattedText("Supervisor:", new Font { Bold = true, Size = 6 }); 
    cp1.AddTab(); 
    cp1.AddFormattedText(jobdet["Supervisor"], new Font { Size = 6 }); 

    cp1.AddLineBreak(); 
    cp1.AddLineBreak(); 

    cp1.AddFormattedText("Location:", new Font { Bold = true, Size = 6 }); 
    cp1.AddTab(); 
    cp1.AddTab(); 
    cp1.AddFormattedText(jobdet["Location"], new Font { Size = 6 }); 

    Paragraph rp1 = this.rightTextFrame.AddParagraph(); 
    rp1.Format.Alignment = ParagraphAlignment.Center; 
    rp1.AddFormattedText("Timesheet by Job", new Font { Bold = true, Size = 9 }); 
    rp1.AddLineBreak(); 
    rp1.AddFormattedText("Pay Period End " + this.GetEdate(), new Font { Bold = true, Size = 8 }); 
    rp1.AddLineBreak(); 
    rp1.AddLineBreak(); 

    rp1.AddFormattedText(this.GetOnDate(), new Font { Size = 6 }); 
    rp1.AddTab(); 
    FormattedText ft = rp1.AddFormattedText("Page ", new Font { Bold = true, Size = 7 }); 
    ft.AddPageField(); 
    ft.Font.Size = 7; 
    ft.Font.Bold = true; 
    ft = rp1.AddFormattedText(" of ", new Font { Bold = true, Size = 7 }); 
    ft.AddNumPagesField(); 
    ft.Font.Size = 7; 
    ft.Font.Bold = true; 

    section.AddParagraph(); 

    cp1.AddLineBreak(); 
    cp1.AddLineBreak(); 
    this.table = section.AddTable(); 

    this.table.Style = "Table"; 
    this.table.Borders.Color = Color.Parse("Black"); 
    this.table.Rows.LeftIndent = 0; 

    int cols = this.GetColCount(); 
    Column column = this.table.AddColumn("4cm"); 
    column.Format.Alignment = ParagraphAlignment.Left; 

    column = this.table.AddColumn("0.6cm"); 
    column.Format.Alignment = ParagraphAlignment.Left; 

    for (int i = 2; i < cols; i++) 
    { 
     column = this.table.AddColumn("0.85cm"); 
     column.Format.Alignment = ParagraphAlignment.Left; 
    } 

    // Create the header of the table 
    Row row = this.table.AddRow(); 
    row.HeadingFormat = true; 
    row.Format.Alignment = ParagraphAlignment.Center; 
    row.Format.Font.Bold = true; 
    row.Cells[0].Borders.Top.Visible = false; 
    row.Cells[0].Borders.Left.Visible = false; 
    row.Cells[0].Borders.Right.Visible = false; 
    row.Cells[1].Borders.Top.Visible = false; 
    row.Cells[1].Borders.Left.Visible = false; 
    string[] colNames = this.GetColNames(); 
    for (int i = 0; i < cols; i++) 
    { 
     row.Cells[i].AddParagraph(colNames[i]); 
     row.Cells[i].Format.Font.Bold = false; 
     row.Cells[i].Format.Alignment = ParagraphAlignment.Left; 
     row.Cells[i].VerticalAlignment = VerticalAlignment.Center; 
     row.Cells[i].Borders.Width = 1.5; 
    } 
} 

我不知道天氣MigraDoc有一些功能可以直接做這種東西。否則,我想知道頁面是否會在3行之間中斷,如果是這樣,我將不得不自己做一個分頁符。

任何僞代碼片斷都會非常有幫助。

回答

4

對於每組的第一行,設置row1.KeepWith = 2將三行保留在一組中。

+0

智能感應捕獲KeepWith屬性。行類型沒有KeepWithNext屬性。我已經添加了如下代碼,但沒有顯示任何效果。 Row row1 = this.table.AddRow(); ((i + 1)%3 == 0) { row1.KeepWith = 2; } – coding

+0

我在做什麼錯? – coding

+0

在這個鏈接http://forum.pdfsharp.net/viewtopic.php?f=2&t=2008他們說它不適用於表..這是否也指keepWith屬性? – coding

0

我使用的是pakeha_by提供的Migradoc修補版本,而不是官方版本。由於補丁版本針對表格生成進行了高度優化。但是它有一個針對TableRow的KeepWith屬性的錯誤。

當我將程序集更新到pakeha_by提供的最新程序集Updated MigraDoc Assemblies by Pakeha_by時,問題就解決了。

相關問題