Pergunta

I am using MigraDoc library for the first time and need help. I am printing a table in pdf using MigraDoc. In my table 3 rows form a group. So each 3 row group must be on the same page but in my current code this is not happening.

It will print 1st or 2nd row on the same page and if the page ends the 3rd row gets onto the next page.

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

I dont know weather MigraDoc has some feature to do this kind of stuff directly. Otherwise I want to know if the page will break in between 3 rows or not and if it does I will have to myself make a pagebreak.

Any pseudo code snippets will be very helpful.

Foi útil?

Solução

For the first row of each group, set row1.KeepWith = 2 to keep three rows together in one group.

Outras dicas

I was using the patched version of Migradoc offered by pakeha_by and not the official version. Since the patched version was highly optimised for table generation. But it had a bug for KeepWith Property of TableRow.

The problem got solved when I updated the assemblies to the latest assemblies that pakeha_by offered Updated MigraDoc Assemblies by Pakeha_by.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top