C# 在添加文档中的最后3行之前,请检查页面结尾是否接近

C# 在添加文档中的最后3行之前,请检查页面结尾是否接近,c#,pdf,pdf-generation,pdfsharp,migradoc,C#,Pdf,Pdf Generation,Pdfsharp,Migradoc,我第一次使用MigraDoc库,需要帮助。我正在使用MigraDoc以pdf格式打印表格。在我的表中,3行组成一个组。因此每个3行组必须在同一页上,但在我当前的代码中,这不会发生 它将在同一页上打印第一行或第二行,如果该页结束,则第三行进入下一页 public void CreateDocument() { this.document = new Document { Info = { Title = "Time Sheet Report By Job", } }; this.DefineSty

我第一次使用MigraDoc库,需要帮助。我正在使用
MigraDoc
以pdf格式打印表格。在我的表中,3行组成一个组。因此每个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;
    }
}
public void CreateDocument()
{
this.document=新文档{Info={Title=“工作时间表报告”,};
这个.DefineStyles();
这个.CreatePage();
这是FillContent();
}
公共内容
{
int x=1;
foreach(本.xReport.Element(ElrepTable.Elements(ElrepRow))中的元素r)
{
行row1=this.table.AddRow();
int i=0;
行1.单元格[0].Borders.Visible=false;
foreach(r.Elements中的元素c(ElrepCell))
{
行1.单元格[i++]。添加段落(c.值);
}
如果(x++%3!=0)
{
继续;
}
row1.Borders.Bottom.Width=1;
行1.单元格[0]。Borders.Bottom.Visible=true;
行1。单元格[0]。Borders.Bottom.Width=1;
}
int colCount=this.GetColCount();
int rowCount=this.GetRowCount();
这个.table.SetEdge(1,1,colCount-1,rowCount-1,Edge.Box,BorderStyle.Single,0.05);
}
私有void CreatePage()
{
Section=this.document.AddSection();
section.PageSetup.LeftMargin=“1cm”;
section.PageSetup.RightMargin=“1cm”;
section.PageSetup.BottomMargin=“2cm”;
section.PageSetup.TopMargin=“4.5cm”;
//在标题中添加徽标
字符串imgPath=this.SaveImg(Resource1.MMS_Logo,“Logo”);
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
图2.Height=“1.5cm”;
图2.Width=“5cm”;
图2.RelativeVertical=RelativeVertical.Line;
图2.相对水平=相对水平边缘;
image2.Top=ShapePosition.Top;
图2.左=形状位置。右;
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.rightextframe.Height=“3.0cm”;
this.rightextframe.Width=“5.0cm”;
this.rightextframe.RelativeHorizontal=RelativeHorizontal.Margin;
this.rightextframe.RelativeVertical=RelativeVertical.Page;
this.rightextframe.Left=ShapePosition.Right;
this.rightextframe.Top=“1.75cm”;
Dictionary jobdet=this.GetJobDetails();
//将发件人放入地址框
段落p1=此.leftTextFrame.AddParague();
var f=新字体{Bold=true,Size=8};
p1.AddFormattedText(this.GetTitle(),f);
p1.AddLineBreak();
p1.AddLineBreak();
p1.AddFormattedText(“账户信息”,新字体{Bold=true,斜体=true,Size=7});
p1.AddLineBreak();
段落p2=此.leftTextFrame.AddParague();
p2.Format.LeftIndent=“2cm”;
p2.添加格式化文本(“作业:”,新字体{Bold=true,Size=6});
p2.AddTab();
p1.AddTab();
p2.AddFormattedText(this.GetJobNo(),新字体{Size=6});
p2.AddLineBreak();
p2.AddFormattedText(“作业名称:”,新字体{Bold=true,Size=6});
p2.AddTab();
p2.AddFormattedText(jobdet[“Name”],新字体{Size=6});
p2.AddLineBreak();
p2.AddFormattedText(“地址:”,新字体{Bold=true,Size=6});
p2.AddTab();
p2.AddFormattedText(jobdet[“Address”],新字体{Size=6});
p2.AddLineBreak();
段落cp1=this.centerTextFrame.add段落();
cp1.AddFormattedText(“主管:”,新字体{Bold=true,Size=6});
cp1.AddTab();
cp1.AddFormattedText(jobdet[“Supervisor”],新字体{Size=6});
cp1.AddLineBreak();
cp1.AddLineBreak();
AddFormattedText(“位置:”,新字体{Bold=true,Size=6});
cp1.AddTab();
cp1.AddTab();
cp1.AddFormattedText(jobdet[“Location”],新字体{Size=6});
段落rp1=此.rightTextFrame.AddParague();
rp1.Format.Alignment=段落Alignment.Center;
rp1.AddFormattedText(“作业时间表”,新字体{Bold=true,Size=9});
rp1.AddLineBreak();
rp1.AddFormattedText(“付款期结束”+this.GetEdate(),新字体{Bold=true,Size=8});
rp1.AddLineBreak();
rp1.AddLineBreak();
rp1.AddFormattedText(this.GetOnDate(),新字体{Size=6});
rp1.AddTab();
FormattedText ft=rp1.AddFormattedText(“页面”,新字体{Bold=true,Size=7});
ft.AddPageField();
ft.Font.Size=7;
ft.Font.Bold=真;
ft=rp1.AddFormattedText(“of”,新字体{Bold=true,Size=7});
ft.AddNumPagesField();
ft.Font.Size=7;
ft.Font.Bold=真;
第条第()款;
cp1.AddLineBreak();
cp1.AddLineBreak();
this.table=section.AddTable();