C# 如何使用PDFSharp在多个页面上显示数据库中的行?

C# 如何使用PDFSharp在多个页面上显示数据库中的行?,c#,pdfsharp,C#,Pdfsharp,我正在尝试生成一个包含数据库中所有详细信息的PDF。这是非常好的工作。。。在数据库中插入更多的数据行之前,我的行一直运行到页面的末尾,然后突然消失。我一直在互联网上苦苦寻找一个解决方案,如何让我的行显示在多个页面上,但我没有运气。在PDFSharp的论坛中没有关于这个主题的支持(至少,不是我能找到的),我真的需要它来工作。任何帮助都将不胜感激 下面是我生成PDF的代码:(代码有效,直到我有很多行。) public void PrintPDFDisplayingLogInformation(Dat

我正在尝试生成一个包含数据库中所有详细信息的PDF。这是非常好的工作。。。在数据库中插入更多的数据行之前,我的行一直运行到页面的末尾,然后突然消失。我一直在互联网上苦苦寻找一个解决方案,如何让我的行显示在多个页面上,但我没有运气。在PDFSharp的论坛中没有关于这个主题的支持(至少,不是我能找到的),我真的需要它来工作。任何帮助都将不胜感激

下面是我生成PDF的代码:(代码有效,直到我有很多行。)

public void PrintPDFDisplayingLogInformation(DataGridView dataPayments)
{
数据集ds=新数据集();
int i=0;
int-yPoint=0;
字符串姓氏=null;
字符串名称=null;
字符串lastPaymentDate=null;
字符串成员资格=空;
字符串gymId=null;
字符串lastPaidAmount=null;
字符串=空;
字符串月份=DateTime.Now.ToString(“MM”);
字符串year=DateTime.Now.year.ToString();
ds=paymentService.RetrieveFullPaymentLogForPDF();
PdfDocument pdf=新PdfDocument();
pdf.Info.Title=“本月付款”;
PdfPage PdfPage=pdf.AddPage();
XGraphics graph=XGraphics.FromPdfPage(pdfPage);
XFont标题=新的XFont(“Verdana”,20,XFontStyle.Bold);
XFont副标题=新XFont(“Verdana”,12,XFontStyle.Bold);
XFont fontBold=新XFont(“Verdana”,8,XFontStyle.Bold);
XFont font=新的XFont(“Verdana”,8,XFontStyle.Regular);
XFont smallFont=新XFont(“Verdana”,6,XFontStyle.Bold);
//绘制文档的标题。
graph.DrawString(“完整支付日志”+DateTime.Now.ToString(“yyyy/MM/dd”),标题XBrushes.Black,
新的XRect(60,60,pdfPage.Width.cm,pdfPage.Height.cm),XStringFormats.TopLeft;
//绘制个人详细信息的子标题。
图.抽绳(“上次付款”,副标题,XBrushes.黑色,
新的XRect(60100,pdfPage.Width.cm,pdfPage.Height.cm),XStringFormats.TopLeft;
图.抽绳(“姓氏”,副标题,XBrushes.黑色,
新的XRect(170100,pdfPage.Width.cm,pdfPage.Height.cm),XStringFormats.TopLeft;
图.抽绳(“名称”,副标题,XBrushes.黑色,
新的XRect(250100,pdfPage.Width.cm,pdfPage.Height.cm),XStringFormats.TopLeft;
图形。拉丝(“类型”,副标题,XBrushes。黑色,
新的XRect(315100,pdfPage.Width.cm,pdfPage.Height.cm),XStringFormats.左上角;
图.抽绳(“健身房ID”,副标题,XBrushes.黑色,
新的XRect(370100,pdfPage.Width.cm,pdfPage.Height.cm),XStringFormats.TopLeft;
图.抽绳(“已付”,副标题,XBrushes.黑色,
新的XRect(435100,pdfPage.Width.cm,pdfPage.Height.cm),XStringFormats.TopLeft;
图.抽绳(“欠款”,副标题,XBrushes.黑色,
新的XRect(4801000,pdfPage.Width.cm,pdfPage.Height.cm),XStringFormats.TopLeft;
//从数据库中绘制每一行。
yPoint=yPoint+120;

对于(i=0;i您使用的是PDFsharp。使用PDFsharp您负责分页符。再次调用AddPage()以启动新页面并开始在页面顶部写入行

另一个答案中链接的发票示例是针对MigraDoc的。MigraDoc自动处理分页符。切换到MigraDoc可能是获得跨多个页面的表的更简单方法


类似的问题在PDFsharp论坛上被问过(并回答过)很多次。

你可能有链接吗?正如我所说,我正在努力在他们的论坛上找到答案。我不知道要搜索什么。你可以从这里开始阅读:(查看概述和MigraDoc示例)。论坛的链接可以在这里找到:
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);
    }