如何使用PDFSharp在多个页面上显示数据库中的行
本文关键字:显示 数据库 PDFSharp 何使用 | 更新日期: 2023-09-27 18:18:30
我正试图从我的数据库生成一个PDF与所有的细节。这工作得很好……直到更多的数据行插入到数据库中,然后我的行运行到页面的末尾,然后突然消失。我一直在扯我的头发搜索解决方案在互联网上如何有我的行显示在多个页面,但我没有运气。在PDFSharp的论坛上没有对这个话题的支持(至少,不是我能找到的),我真的需要这个工作。任何帮助将非常感激!
下面是我生成PDF的代码:(代码可以工作,直到我有很多行。)
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);
}
您正在使用PDFsharp。使用PDFsharp,您要负责分页符。再次调用AddPage()来开始一个新的页面,并开始在页面的顶部写入行。
另一个答案中链接的Invoice示例是用于MigraDoc的。MigraDoc自动处理分页符。切换到MigraDoc可能是获得跨多个页面的表的更简单的方法。
这样的问题在PDFsharp论坛上被问过(也回答过)很多次。