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

时间:2014-08-16 17:26:28

标签: c# pdfsharp

我正在尝试使用我的数据库中的所有详细信息生成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);
    }

1 个答案:

答案 0 :(得分:0)

您正在使用PDFsharp。使用PDFsharp,您负责分页符。再次调用AddPage()以启动新页面并开始在页面顶部写入行。

另一个答案中链接的Invoice示例适用于MigraDoc。 MigraDoc自动处理分页符。切换到MigraDoc可能是获取跨越多个页面的表的更简单方法。

在PDFsharp论坛上多次询问(和回答)这样的问题。