I am trying to generate a PDF with all the details from my database. This is working wonderfully... until more rows of data is inserted in the database, then my rows run up to the end of the page and all of a sudden disappear. I have been pulling my hair out searching for a solution on the internet on how to have my rows display over multiple pages but I have had no luck. There is NO support on this topic in PDFSharp's forums(Well at least, not what I could find) and I really need this to work. ANY HELP WOULD BE GREATLY APPRECIATED!
Here is my code for generating the PDF: (Code works, up until I have a lot of rows.)
public void PrintPDFDisplayingLogInformation(DataGridView dataPayments)
{
DataSet ds = new DataSet();
int i = 0;
int yPoint = 0;
string surname = null;
string name = null;
string lastPaymentDate = null;
string membership = null;
string gymId = null;
string lastPaidAmount = null;
string arrears = null;
string month = DateTime.Now.ToString("MM");
string year = DateTime.Now.Year.ToString();
ds = paymentService.RetrieveFullPaymentLogForPDF();
PdfDocument pdf = new PdfDocument();
pdf.Info.Title = "Payments This Month";
PdfPage pdfPage = pdf.AddPage();
XGraphics graph = XGraphics.FromPdfPage(pdfPage);
XFont heading = new XFont("Verdana", 20, XFontStyle.Bold);
XFont subHeading = new XFont("Verdana", 12, XFontStyle.Bold);
XFont fontBold = new XFont("Verdana", 8, XFontStyle.Bold);
XFont font = new XFont("Verdana", 8, XFontStyle.Regular);
XFont smallFont = new XFont("Verdana", 6, XFontStyle.Bold);
//Draw Header Of Document.
graph.DrawString("Full Payment Log " + DateTime.Now.ToString("yyyy/MM/dd"), heading, XBrushes.Black,
new XRect(60, 60, pdfPage.Width.Centimeter, pdfPage.Height.Centimeter), XStringFormats.TopLeft);
//Draw Sub-Heading For Personal Details.
graph.DrawString("Last Payment", subHeading, XBrushes.Black,
new XRect(60, 100, pdfPage.Width.Centimeter, pdfPage.Height.Centimeter), XStringFormats.TopLeft);
graph.DrawString("Surname", subHeading, XBrushes.Black,
new XRect(170, 100, pdfPage.Width.Centimeter, pdfPage.Height.Centimeter), XStringFormats.TopLeft);
graph.DrawString("Name", subHeading, XBrushes.Black,
new XRect(250, 100, pdfPage.Width.Centimeter, pdfPage.Height.Centimeter), XStringFormats.TopLeft);
graph.DrawString("Type", subHeading, XBrushes.Black,
new XRect(315, 100, pdfPage.Width.Centimeter, pdfPage.Height.Centimeter), XStringFormats.TopLeft);
graph.DrawString("Gym ID", subHeading, XBrushes.Black,
new XRect(370, 100, pdfPage.Width.Centimeter, pdfPage.Height.Centimeter), XStringFormats.TopLeft);
graph.DrawString("Paid", subHeading, XBrushes.Black,
new XRect(435, 100, pdfPage.Width.Centimeter, pdfPage.Height.Centimeter), XStringFormats.TopLeft);
graph.DrawString("Arrears", subHeading, XBrushes.Black,
new XRect(480, 100, pdfPage.Width.Centimeter, pdfPage.Height.Centimeter), XStringFormats.TopLeft);
//Draw Each Row From Database.
yPoint = yPoint + 120;
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
surname = ds.Tables[0].Rows[i].ItemArray[0].ToString();
name = ds.Tables[0].Rows[i].ItemArray[1].ToString();
membership = ds.Tables[0].Rows[i].ItemArray[2].ToString();
lastPaidAmount = ds.Tables[0].Rows[i].ItemArray[3].ToString();
arrears = ds.Tables[0].Rows[i].ItemArray[4].ToString();
gymId = ds.Tables[0].Rows[i].ItemArray[5].ToString();
lastPaymentDate = ds.Tables[0].Rows[i].ItemArray[6].ToString();
//Check Whether Date Is Current Month.
if (lastPaymentDate.Contains(year + "/" + month))
{
graph.DrawString(lastPaymentDate, font, XBrushes.Black, new XRect(60, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
}
else
{
graph.DrawString(lastPaymentDate, font, XBrushes.Red, new XRect(60, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
}
graph.DrawString(surname, font, XBrushes.Black, new XRect(170, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
graph.DrawString(name, font, XBrushes.Black, new XRect(250, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
graph.DrawString(membership, font, XBrushes.Black, new XRect(315, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
graph.DrawString(gymId, font, XBrushes.Black, new XRect(370, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
//Check Whether Member Has Paid For Current Month.
if (Convert.ToInt32(lastPaidAmount) != 0)
{
graph.DrawString(lastPaidAmount, font, XBrushes.Green, new XRect(435, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
}
else
{
graph.DrawString(lastPaidAmount, font, XBrushes.Black, new XRect(435, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
}
//Check Whether Member Is In Arrears.
if (Convert.ToInt32(arrears) != 0)
{
graph.DrawString(arrears, font, XBrushes.Red, new XRect(480, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
}
else
{
graph.DrawString(arrears, font, XBrushes.Black, new XRect(480, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
}
yPoint = yPoint + 15;
}
//Draw Page Footer.
graph.DrawString("PDF Generated With ++++++++",
smallFont, XBrushes.Black, new XRect(300, 800, pdfPage.Width.Centimeter, pdfPage.Height.Centimeter), XStringFormats.Center);
graph.DrawString("Copyright © ++++++++++ (Pty) Ltd || All Right Reserved",
smallFont, XBrushes.Black, new XRect(300, 807, pdfPage.Width.Centimeter, pdfPage.Height.Centimeter), XStringFormats.Center);
graph.DrawString("Website: ",
smallFont, XBrushes.Black, new XRect(300, 814, pdfPage.Width.Centimeter, pdfPage.Height.Centimeter), XStringFormats.Center);
graph.DrawString("Email: ",
smallFont, XBrushes.Black, new XRect(300, 821, pdfPage.Width.Centimeter, pdfPage.Height.Centimeter), XStringFormats.Center);
string pdfFilename = "Payment_Log_" + DateTime.Now.ToString("yyyy-MM-dd") + ".pdf";
string strPath = Environment.GetFolderPath(
System.Environment.SpecialFolder.DesktopDirectory) + "\\" + pdfFilename;
pdf.Save(strPath);
Process.Start(strPath);
}
You are using PDFsharp. With PDFsharp you are responsible for the page breaks. Call AddPage() again to start a new page and start writing rows at the top of the page.
The Invoice sample linked in the other answer is for MigraDoc. MigraDoc handles page breaks automatically. Switching to MigraDoc is probably the easier way to get a table that spans several pages.
Questions like that were asked (and answered) many times on the PDFsharp forum.