MigraDoc:强制嵌入表在逻辑点处中断

时间:2013-10-11 14:15:23

标签: c# pdfsharp migradoc

我有一个大型表格,我正在转换成PDF。第一个1/6看起来像这样:

http://i.imgur.com/y4pO8Th.png

然而,输入字段的数量从每个部分的1到20不等,我需要能够智能地使这个文档中断页面。我的计划是最初逐个绘制表格,并通过抓取所有先前表格中的行数来管理Y坐标。这很有效,但是当我进入分页符时就会崩溃,我开始需要一些半复杂的逻辑来使它工作,并且这种逻辑会在添加每个附加表时变得更加混乱和混乱。

我的第二个计划是在PDF中重现HTML文档的表格结构,我设法成功完成...

private void DrawPDF()
{
    Document tDoc = new Document();
    MigraDoc.DocumentObjectModel.Style style = tDoc.Styles["Normal"];
    style.Font.Name = tPdfFont;
    style.Font.Size = 10;
    Section tSec = tDoc.AddSection();

    MigraDoc.DocumentObjectModel.Tables.Table masterTable = new MigraDoc.DocumentObjectModel.Tables.Table();
    masterTable = tSec.AddTable();
    masterTable.Borders.Visible = false;

    Column leftColumn = masterTable.AddColumn("365pt");
    Column spacer = masterTable.AddColumn("10pt");
    Column rightColumn = masterTable.AddColumn("365pt");

    Row tFS = masterTable.AddRow();
    Cell tCell = tFS.Cells[0];

    //
    // Farm Assets Column
    //
    {
        MigraDoc.DocumentObjectModel.Tables.Table tAssetsTable = new MigraDoc.DocumentObjectModel.Tables.Table();
        tAssetsTable.Borders.Visible = false;
        Column tColumn = tAssetsTable.AddColumn("365pt");
        tCell.Elements.Add(tAssetsTable);



        //
        // Current Farm Assets
        //
        for (int i = 0; i < 10; i++)  // Drawn 10 times to force it to draw over the 1st page.
        {
            Section thisSection = tDoc.AddSection();
            Row tAssetsRow = tAssetsTable.AddRow();
            Cell tAssetsCell = tAssetsRow.Cells[0];

            MigraDoc.DocumentObjectModel.Tables.Table table = new MigraDoc.DocumentObjectModel.Tables.Table();
            table = thisSection.AddTable();
            table.Borders.Width = 0.2;
            table.Rows.LeftIndent = 0;

            Column columnData = table.AddColumn("295pt");
            columnData.Borders.Left.Visible = false;
            Column columnValue = table.AddColumn("70pt");

            Row rowA = table.AddRow();
            rowA.Shading.Color = Color.FromRgbColor((byte)255, Color.Parse("0xa2a2d2"));
            rowA.Cells[0].AddParagraph("CURRENT FARM ASSETS");
            rowA.Cells[1].AddParagraph("$ Value");
            rowA.Cells[1].Format.Alignment = ParagraphAlignment.Right;

            Row row1 = table.AddRow();
            row1.Borders.Bottom.Visible = false;
            row1.Cells[0].AddParagraph("Cash: Savings: ($" + MP.FormFinancialStatement.CurrentStaticAssets.Savings + ") Checking: ($" + MP.FormFinancialStatement.CurrentStaticAssets.Checking + ")");
            row1.Cells[1].AddParagraph(MP.FormFinancialStatement.CurrentStaticAssets.CashTotal);
            row1.Cells[1].Format.Alignment = ParagraphAlignment.Right;

            Row row2 = table.AddRow();
            row2.Borders.Bottom.Visible = false;
            row2.Cells[0].AddParagraph("Invest: Time Cret $" + MP.FormFinancialStatement.CurrentStaticAssets.TimeCret + " Other: $" + MP.FormFinancialStatement.CurrentStaticAssets.OtherInvestments + "");
            row2.Cells[1].AddParagraph(MP.FormFinancialStatement.CurrentStaticAssets.InvestTotal);
            row2.Cells[1].Format.Alignment = ParagraphAlignment.Right;

            Row row3 = table.AddRow();
            row3.Borders.Bottom.Visible = false;
            row3.Cells[0].AddParagraph("Replacement Account");
            row3.Cells[1].AddParagraph(MP.FormFinancialStatement.CurrentStaticAssets.ReplacementAccount);
            row3.Cells[1].Format.Alignment = ParagraphAlignment.Right;

            Row row4 = table.AddRow();
            row4.Cells[0].AddParagraph("Accouts and Notes Recievable");
            row4.Cells[1].AddParagraph(MP.FormFinancialStatement.CurrentStaticAssets.AccountsNotesReceivable);
            row4.Cells[1].Format.Alignment = ParagraphAlignment.Right;

            MigraDoc.DocumentObjectModel.Tables.Table clone = (MigraDoc.DocumentObjectModel.Tables.Table)table.Clone();

            tAssetsCell.Elements.Add(clone);
        }
    }

    MigraDoc.Rendering.DocumentRenderer docRenderer = new DocumentRenderer(tDoc);
    docRenderer.PrepareDocument();
    docRenderer.RenderObject(gfx, 30, 85, "740pt", masterTable);
}

但是,这实际上并没有正确破坏页面。我尝试了对每个单独的表进行分区,希望它能够进行分页魔术,但事实并非如此。

如何构建它以允许良好的分页?

1 个答案:

答案 0 :(得分:0)

您可以使用表行的KeepWith属性将块保持在一个页面上。只能将它用于肯定适合一页的块。

另见:
https://stackoverflow.com/a/6831048/1015447
https://stackoverflow.com/a/1327228/1015447