I posted this question on Stack Overflow, and got pointed in the right direction, but I can't fill in the blanks, so I was hoping for a more hand-holdy explanation to compensate for my dumb-ness.
I'm having issues getting a MigraDoc to split pages automatically. I'm stacking tables on top of each other and was hoping they'd just intelligently place page breaks as the number of rows in these tables is dynamic. The code I have currently can be found here:
Code:
private void createPDFDocument()
{
_Doc = new PdfDocument();
_Doc.Info.Title = "Test Sample";
page = _Doc.AddPage();;
page.Size = PageSize.Letter;
page.Orientation = PageOrientation.Landscape;
gfx = XGraphics.FromPdfPage(page);
font = new XFont(tPdfFont, 10);
DrawPDF();
const string filename = "C:\\Users\\mkautzman\\Desktop\\Strasburg\\trunk\\Documents\\WorkingPDF.pdf";
}
...
public void DrawPDF()
{
///
// Balance Sheet Header
///
Document tDoc = new Document();
MigraDoc.DocumentObjectModel.Style style = tDoc.Styles["Normal"];
style.Font.Name = tPdfFont;
style.Font.Size = 10;
Section tSec = tDoc.AddSection();
int tAccountsPayableRows = 0;
int tIntermediateLiabilitiesRows = 0;
int tLongTermLiabilitesRows = 0;
MigraDoc.DocumentObjectModel.Tables.Table tTopTable = new MigraDoc.DocumentObjectModel.Tables.Table();
MigraDoc.DocumentObjectModel.Tables.Table tCurrentAssetsTable = new MigraDoc.DocumentObjectModel.Tables.Table();
MigraDoc.DocumentObjectModel.Tables.Table tAccountsPayableTable = new MigraDoc.DocumentObjectModel.Tables.Table();
MigraDoc.DocumentObjectModel.Tables.Table tStaticLiabilitiesTable = new MigraDoc.DocumentObjectModel.Tables.Table();
MigraDoc.DocumentObjectModel.Tables.Table tIntermediateLiabilitiesTable = new MigraDoc.DocumentObjectModel.Tables.Table();
MigraDoc.DocumentObjectModel.Tables.Table tLongTermLiabilitiesTable = new MigraDoc.DocumentObjectModel.Tables.Table();
{
tTopTable = tSec.AddTable();
tTopTable.Borders.Width = 0.2;
tTopTable.Rows.LeftIndent = 0;
tCurrentAssetsTable = tSec.AddTable();
tCurrentAssetsTable.Borders.Width = 0.2;
tCurrentAssetsTable.Rows.LeftIndent = 0;
tAccountsPayableTable = tSec.AddTable();
tAccountsPayableTable.Borders.Width = 0.2;
tAccountsPayableTable.Rows.LeftIndent = 0;
tStaticLiabilitiesTable = tSec.AddTable();
tStaticLiabilitiesTable.Borders.Width = 0.2;
tStaticLiabilitiesTable.Rows.LeftIndent = 0;
tIntermediateLiabilitiesTable = tSec.AddTable();
tIntermediateLiabilitiesTable.Borders.Width = 0.2;
tIntermediateLiabilitiesTable.Rows.LeftIndent = 0;
tLongTermLiabilitiesTable = tSec.AddTable();
tLongTermLiabilitiesTable.Borders.Width = 0.2;
tLongTermLiabilitiesTable.Rows.LeftIndent = 0;
}
///
// Balance Sheet Header
///
{
Column column = tTopTable.AddColumn("740pt");
column.Format.Alignment = ParagraphAlignment.Center;
Row row = tTopTable.AddRow();
row.HeadingFormat = true;
row.Shading.Color = Color.FromRgbColor((byte)255, Color.Parse("0xa2d2a2"));
row.Cells[0].AddParagraph("Balance Sheet/Financial Statement");
}
///
// Current Farm Assets
///
{
Column columnData = tCurrentAssetsTable.AddColumn("295pt");
columnData.Borders.Left.Visible = false;
Column columnValue = tCurrentAssetsTable.AddColumn("70pt");
Row rowA = tCurrentAssetsTable.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 = tCurrentAssetsTable.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 = tCurrentAssetsTable.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 = tCurrentAssetsTable.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 = tCurrentAssetsTable.AddRow();
row4.Cells[0].AddParagraph("Accouts and Notes Recievable");
row4.Cells[1].AddParagraph(MP.FormFinancialStatement.CurrentStaticAssets.AccountsNotesReceivable);
row4.Cells[1].Format.Alignment = ParagraphAlignment.Right;
}
///
// Accounts Payable
///
{
Column columnData = tAccountsPayableTable.AddColumn("225pt");
Column columnPastDue = tAccountsPayableTable.AddColumn("70pt");
Column columnValue = tAccountsPayableTable.AddColumn("70pt");
columnValue.Borders.Right.Visible = false;
Row rowA = tAccountsPayableTable.AddRow();
rowA.Shading.Color = Color.FromRgbColor((byte)255, Color.Parse("0xa2a2d2"));
rowA.Cells[0].AddParagraph("CURRENT FARM LIABILITIES");
rowA.Cells[0].MergeRight = 1;
rowA.Cells[2].AddParagraph("$ Amount");
rowA.Cells[2].Format.Alignment = ParagraphAlignment.Right;
Row rowB = tAccountsPayableTable.AddRow();
rowB.Borders.Bottom.Visible = false;
rowB.Cells[0].AddParagraph("Accounts & Notes Payable (cr/due date)");
rowB.Cells[1].AddParagraph("Past Due");
rowB.Cells[1].Format.Alignment = ParagraphAlignment.Center;
rowB.Cells[2].AddParagraph("");
foreach (AccountsPayable thisAccount in tAccountsPayable)
{
Row dynRow = tAccountsPayableTable.AddRow();
dynRow.Borders.Bottom.Visible = false;
dynRow.Borders.Top.Visible = false;
dynRow.Cells[0].AddParagraph(" " + tAccountsPayable[tAccountsPayableRows].Description);
dynRow.Cells[1].AddParagraph(tAccountsPayable[tAccountsPayableRows].PastDue);
dynRow.Cells[1].Format.Alignment = ParagraphAlignment.Right;
dynRow.Cells[2].AddParagraph(tAccountsPayable[tAccountsPayableRows].Value);
dynRow.Cells[2].Format.Alignment = ParagraphAlignment.Right;
tAccountsPayableRows++;
}
}
///
// Static Liabilities
///
{
_PdfVerticalOffset = 85 + (_PdfRowsFSRight * _PdfRowHeight);
Column columnData2 = tStaticLiabilitiesTable.AddColumn("295pt");
Column columnValue2 = tStaticLiabilitiesTable.AddColumn("70pt");
columnValue2.Borders.Right.Visible = false;
Row rowAb = tStaticLiabilitiesTable.AddRow();
rowAb.Borders.Top.Visible = true;
rowAb.Borders.Bottom.Visible = false;
rowAb.Cells[0].AddParagraph(MP.FormFinancialStatement.StaticLiabilites.TopLine);
rowAb.Cells[1].AddParagraph("");
Row row1b = tStaticLiabilitiesTable.AddRow();
row1b.Borders.Bottom.Visible = false;
row1b.Cells[0].AddParagraph(" Intermediate Liabilities (" + MP.FormFinancialStatement.StaticLiabilites.IntLiabilitiesText + ")");
row1b.Cells[1].Format.Alignment = ParagraphAlignment.Right;
row1b.Cells[1].AddParagraph(MP.FormFinancialStatement.StaticLiabilites.IntLiabilitiesValue);
Row row2b = tStaticLiabilitiesTable.AddRow();
row2b.Borders.Bottom.Visible = false;
row2b.Cells[0].AddParagraph(" Long Term Liabilities (" + MP.FormFinancialStatement.StaticLiabilites.LongLiabilitiesText + ")");
row2b.Cells[1].Format.Alignment = ParagraphAlignment.Right;
row2b.Cells[1].AddParagraph(MP.FormFinancialStatement.StaticLiabilites.LongLiabilitiesValue);
Row row3b = tStaticLiabilitiesTable.AddRow();
row3b.Borders.Bottom.Visible = false;
row3b.Cells[0].AddParagraph("Accrued Interest On: (" + MP.FormFinancialStatement.StaticLiabilites.AccruedInterestText + ")");
row3b.Cells[1].AddParagraph("");
Row row4b = tStaticLiabilitiesTable.AddRow();
row4b.Borders.Bottom.Visible = false;
row4b.Cells[0].AddParagraph(" Accounts and Notes Payable (" + MP.FormFinancialStatement.StaticLiabilites.AccountsPayableText + ")");
row4b.Cells[1].Format.Alignment = ParagraphAlignment.Right;
row4b.Cells[1].AddParagraph(MP.FormFinancialStatement.StaticLiabilites.AccountsPayableValue);
Row row5b = tStaticLiabilitiesTable.AddRow();
row5b.Borders.Bottom.Visible = false;
row5b.Cells[0].AddParagraph(" Intermediate Liabilities (" + MP.FormFinancialStatement.StaticLiabilites.OtherIntLiabilitiesText + ")");
row5b.Cells[1].Format.Alignment = ParagraphAlignment.Right;
row5b.Cells[1].AddParagraph(MP.FormFinancialStatement.StaticLiabilites.OtherIntLiabilitiesValue);
Row row6b = tStaticLiabilitiesTable.AddRow();
row6b.Borders.Bottom.Visible = true;
row6b.Cells[0].AddParagraph(" Long Term Liabilities (" + MP.FormFinancialStatement.StaticLiabilites.OtherLongLiabilitiesText + ")");
row6b.Cells[1].Format.Alignment = ParagraphAlignment.Right;
row6b.Cells[1].AddParagraph(MP.FormFinancialStatement.StaticLiabilites.OtherLongLiabilitiesValue);
Row row7b = tStaticLiabilitiesTable.AddRow();
row7b.Borders.Top.Visible = true;
row7b.Borders.Bottom.Visible = false;
row7b.Format.Alignment = ParagraphAlignment.Right;
row7b.Cells[0].AddParagraph("TOTAL CURRENT FARM LIABILITIES => ");
row7b.Cells[1].AddParagraph(MP.FormFinancialStatement.StaticLiabilites.CurrentFarmLiabilities);
}
///
// Intermediate Liabilities
///
{
_PdfVerticalOffset = 85 + (_PdfRowsFSRight * _PdfRowHeight);
Column columnData = tIntermediateLiabilitiesTable.AddColumn("125pt");
Column columnMaturityDate = tIntermediateLiabilitiesTable.AddColumn("50pt");
Column columnInterestRate = tIntermediateLiabilitiesTable.AddColumn("50pt");
Column columnInstallment = tIntermediateLiabilitiesTable.AddColumn("70pt");
Column columnValue = tIntermediateLiabilitiesTable.AddColumn("70pt");
columnValue.Borders.Right.Visible = false;
Row rowA = tIntermediateLiabilitiesTable.AddRow();
rowA.Shading.Color = Color.FromRgbColor((byte)255, Color.Parse("0xa2a2d2"));
rowA.Cells[0].AddParagraph("INTERMEIDATE FARM LIABILITIES (due beyond 12 mos.)");
rowA.Cells[0].MergeRight = 3;
rowA.Cells[4].AddParagraph("");
Row rowB = tIntermediateLiabilitiesTable.AddRow();
rowB.Borders.Bottom.Visible = true;
rowB.Cells[0].AddParagraph("\nCreditor");
rowB.Cells[0].Format.Alignment = ParagraphAlignment.Center;
rowB.Cells[1].AddParagraph("Maturity Date");
rowB.Cells[1].Format.Alignment = ParagraphAlignment.Center;
rowB.Cells[2].AddParagraph("Interest Rate");
rowB.Cells[2].Format.Alignment = ParagraphAlignment.Center;
rowB.Cells[3].AddParagraph("Annual\nInstallment");
rowB.Cells[3].Format.Alignment = ParagraphAlignment.Center;
rowB.Cells[4].AddParagraph("");
foreach (IntermediateLiabilities thisLine in tIntLiabilities)
{
Row dynRow = tIntermediateLiabilitiesTable.AddRow();
dynRow.Borders.Bottom.Visible = false;
dynRow.Borders.Top.Visible = false;
dynRow.Cells[0].AddParagraph(tIntLiabilities[tIntermediateLiabilitiesRows].Creditor);
dynRow.Cells[1].AddParagraph(tIntLiabilities[tIntermediateLiabilitiesRows].MaturityDate);
dynRow.Cells[1].Format.Alignment = ParagraphAlignment.Center;
dynRow.Cells[2].AddParagraph(tIntLiabilities[tIntermediateLiabilitiesRows].InterestRate);
dynRow.Cells[2].Format.Alignment = ParagraphAlignment.Center;
dynRow.Cells[3].AddParagraph(tIntLiabilities[tIntermediateLiabilitiesRows].Installment);
dynRow.Cells[3].Format.Alignment = ParagraphAlignment.Center;
dynRow.Cells[4].AddParagraph(tIntLiabilities[tIntermediateLiabilitiesRows].Value);
dynRow.Cells[4].Format.Alignment = ParagraphAlignment.Right;
tIntermediateLiabilitiesRows++;
}
Row otherRow = tIntermediateLiabilitiesTable.AddRow();
otherRow.Borders.Top.Visible = true;
otherRow.Borders.Bottom.Visible = true;
otherRow.Cells[0].AddParagraph("Other (" + MP.FormFinancialStatement.StaticLiabilites.OtherIntermediateLiabilitiesText + ")");
otherRow.Cells[0].MergeRight = 3;
otherRow.Cells[4].AddParagraph(MP.FormFinancialStatement.StaticLiabilites.OtherIntermediateLiabilitiesValue);
otherRow.Cells[4].Format.Alignment = ParagraphAlignment.Right;
Row totalRow = tIntermediateLiabilitiesTable.AddRow();
totalRow.Borders.Top.Visible = true;
totalRow.Borders.Bottom.Visible = true;
totalRow.Cells[0].AddParagraph("TOTAL INTERMEDIATE FARM LIABILITIES => ");
totalRow.Cells[0].MergeRight = 3;
totalRow.Cells[0].Format.Alignment = ParagraphAlignment.Right;
totalRow.Cells[4].AddParagraph(MP.FormFinancialStatement.StaticLiabilites.TotalIntermediateLiabilities);
totalRow.Cells[4].Format.Alignment = ParagraphAlignment.Right;
}
///
// Long Tem Liabilities
///
{
_PdfVerticalOffset = 85 + (_PdfRowsFSRight * _PdfRowHeight);
Column columnData = tLongTermLiabilitiesTable.AddColumn("125pt");
Column columnMaturityDate = tLongTermLiabilitiesTable.AddColumn("50pt");
Column columnInterestRate = tLongTermLiabilitiesTable.AddColumn("50pt");
Column columnInstallment = tLongTermLiabilitiesTable.AddColumn("70pt");
Column columnValue = tLongTermLiabilitiesTable.AddColumn("70pt");
columnValue.Borders.Right.Visible = false;
Row rowA = tLongTermLiabilitiesTable.AddRow();
rowA.Shading.Color = Color.FromRgbColor((byte)255, Color.Parse("0xa2a2d2"));
rowA.Cells[0].AddParagraph("LONG TERM FARM LIABILITIES (due beyond 12 mos.)");
rowA.Cells[0].MergeRight = 3;
rowA.Cells[4].AddParagraph("");
Row rowB = tLongTermLiabilitiesTable.AddRow();
rowB.Borders.Bottom.Visible = true;
rowB.Cells[0].AddParagraph("\nCreditor");
rowB.Cells[0].Format.Alignment = ParagraphAlignment.Center;
rowB.Cells[1].AddParagraph("Maturity Date");
rowB.Cells[1].Format.Alignment = ParagraphAlignment.Center;
rowB.Cells[2].AddParagraph("Interest Rate");
rowB.Cells[2].Format.Alignment = ParagraphAlignment.Center;
rowB.Cells[3].AddParagraph("Annual\nInstallment");
rowB.Cells[3].Format.Alignment = ParagraphAlignment.Center;
rowB.Cells[4].AddParagraph("");
foreach (LongTermLiabilities thisLine in tLongLiabilities)
{
Row dynRow = tLongTermLiabilitiesTable.AddRow();
dynRow.Borders.Bottom.Visible = false;
dynRow.Borders.Top.Visible = false;
dynRow.Cells[0].AddParagraph(tLongLiabilities[tLongTermLiabilitesRows].Creditor);
dynRow.Cells[1].AddParagraph(tLongLiabilities[tLongTermLiabilitesRows].MaturityDate);
dynRow.Cells[1].Format.Alignment = ParagraphAlignment.Center;
dynRow.Cells[2].AddParagraph(tLongLiabilities[tLongTermLiabilitesRows].InterestRate);
dynRow.Cells[2].Format.Alignment = ParagraphAlignment.Center;
dynRow.Cells[3].AddParagraph(tLongLiabilities[tLongTermLiabilitesRows].Installment);
dynRow.Cells[3].Format.Alignment = ParagraphAlignment.Center;
dynRow.Cells[4].AddParagraph(tLongLiabilities[tLongTermLiabilitesRows].Value);
dynRow.Cells[4].Format.Alignment = ParagraphAlignment.Right;
tLongTermLiabilitesRows++;
}
Row otherRow = tLongTermLiabilitiesTable.AddRow();
otherRow.Borders.Top.Visible = true;
otherRow.Borders.Bottom.Visible = true;
otherRow.Cells[0].AddParagraph("Other (" + MP.FormFinancialStatement.StaticLiabilites.OtherLongTermLiabilitiesText + ")");
otherRow.Cells[0].MergeRight = 3;
otherRow.Cells[4].AddParagraph(MP.FormFinancialStatement.StaticLiabilites.OtherLongTermLiabilitiesValue);
otherRow.Cells[4].Format.Alignment = ParagraphAlignment.Right;
Row totalRow = tLongTermLiabilitiesTable.AddRow();
totalRow.Borders.Top.Visible = true;
totalRow.Borders.Bottom.Visible = true;
totalRow.Cells[0].AddParagraph("TOTAL LONG TERM FARM LIABILITIES => ");
totalRow.Cells[0].MergeRight = 3;
totalRow.Cells[0].Format.Alignment = ParagraphAlignment.Right;
totalRow.Cells[4].AddParagraph(MP.FormFinancialStatement.StaticLiabilites.TotalLongTermLiabilities);
totalRow.Cells[4].Format.Alignment = ParagraphAlignment.Right;
Row farmTotalRow = tLongTermLiabilitiesTable.AddRow();
farmTotalRow.Borders.Top.Visible = true;
farmTotalRow.Borders.Bottom.Visible = true;
farmTotalRow.Cells[0].AddParagraph("TOTAL FARM LIABILITIES => ");
farmTotalRow.Cells[0].MergeRight = 3;
farmTotalRow.Cells[4].AddParagraph(MP.FormFinancialStatement.StaticLiabilites.TotalFarmLiabilities);
farmTotalRow.Cells[4].Format.Alignment = ParagraphAlignment.Right;
}
///
// Render Document
///
{
MigraDoc.Rendering.DocumentRenderer docRenderer = new DocumentRenderer(tDoc);
docRenderer.PrepareDocument();
docRenderer.RenderObject(gfx, 30, 65, "740pt", tTopTable);
docRenderer.RenderObject(gfx, 30, 85, "365pt", tCurrentAssetsTable);
docRenderer.RenderObject(gfx, 405, 85, "365pt", tAccountsPayableTable);
_PdfRowsFSRight += (2 + tAccountsPayableRows); // 2 for the 2 rows that will always be there regardless of entries3
docRenderer.RenderObject(gfx, 405, 85 + (_PdfRowsFSRight * _PdfRowHeight), "365pt", tStaticLiabilitiesTable);
_PdfRowsFSRight += 8;
docRenderer.RenderObject(gfx, 405, 85 + (_PdfRowsFSRight * _PdfRowHeight), "365pt", tIntermediateLiabilitiesTable);
_PdfRowsFSRight += (5 + tIntermediateLiabilitiesRows); // 2 rows, one is a double tall
docRenderer.RenderObject(gfx, 405, 85 + (_PdfRowsFSRight * _PdfRowHeight), "365pt", tLongTermLiabilitiesTable);
_PdfRowsFSRight += (6 + tLongTermLiabilitesRows); // 2 rows, one is a double tall
docRenderer.RenderObject(gfx, 405, 85 + (_PdfRowsFSRight * _PdfRowHeight), "365pt", tLongTermLiabilitiesTable);
_PdfRowsFSRight += (6 + tLongTermLiabilitesRows); // 2 rows, one is a double tall
docRenderer.RenderObject(gfx, 405, 85 + (_PdfRowsFSRight * _PdfRowHeight), "365pt", tLongTermLiabilitiesTable);
_PdfRowsFSRight += (6 + tLongTermLiabilitesRows); // 2 rows, one is a double tall
docRenderer.RenderObject(gfx, 405, 85 + (_PdfRowsFSRight * _PdfRowHeight), "365pt", tLongTermLiabilitiesTable);
_PdfRowsFSRight += (6 + tLongTermLiabilitesRows); // 2 rows, one is a double tall
docRenderer.RenderObject(gfx, 405, 85 + (_PdfRowsFSRight * _PdfRowHeight), "365pt", tLongTermLiabilitiesTable);
_PdfRowsFSRight += (6 + tLongTermLiabilitesRows); // 2 rows, one is a double tall
}
}
For testing purposes, I force the last table to render a handful of times to get to the end of the page. It generates a PDF that looks like this:
http://i.imgur.com/bv374Kd.pngThe kind team that manages the SO account for PDFSharp did point me to this:
http://www.pdfsharp.net/wiki/HelloMigraDoc-sample.ashxBut I'm not sure how I can leverage that with my current code. Do I need to totally restructure everything? What needs to happen for me to get automatic page breaks to work?