每页固定行使用dataGridView iTextSharp winForms

时间:2015-06-21 11:42:20

标签: c# winforms itextsharp

当我使用iTextSharp将dataGridView导出为pdf时,我想告诉他每页有10行,其余行要转到下一页,我不知道要解决这个问题,不知道。这是我将datagridview导出为pdf的代码。

   private void BindDataGridView()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Nu", Type.GetType("System.String"));
        dt.Columns.Add("MODEL", Type.GetType("System.String"));
        dt.Columns.Add("ORDER", Type.GetType("System.String"));
        dt.Columns.Add("Qnt", Type.GetType("System.String"));
        dt.Columns.Add("Price", Type.GetType("System.String"));



        for (int i = 0; i < 50; ++i)
        {
            dt.Rows.Add();
            dt.Rows[i]["Nu"] = (i+1).ToString();
            dt.Rows[i]["MODEL"].ToString();
            dt.Rows[i]["ORDER"].ToString();
            dt.Rows[i]["Qnt"].ToString();
            dt.Rows[i]["Price"].ToString();

        }

        this.dataGridView1.DataSource = dt;


    }

     private void button1_Click(object sender, EventArgs e)
    {

        string sylfaenpath = Environment.GetEnvironmentVariable("windir") + "\\fonts\\Arial.ttf";
        BaseFont sylfaen = BaseFont.CreateFont(sylfaenpath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        Font head = new Font(sylfaen, 12f, iTextSharp.text.Font.NORMAL, iTextSharp.text.Color.BLACK);


        var bodyFont = FontFactory.GetFont("Arial", 8);

        PdfPTable pdfTable = new PdfPTable(5);
        pdfTable.WidthPercentage = 100;

        pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;
        pdfTable.DefaultCell.BorderWidth = 1;

        float[] widths13 = new float[] { 1f, 1f, 3f, 1f, 1f };
        pdfTable.SetWidths(widths13);



        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            foreach (DataGridViewCell cell in row.Cells)
            {
                pdfTable.AddCell(new Phrase(cell.Value.ToString(), head));
            }
        }

        var titleFont = FontFactory.GetFont("Arial", 9);


     string folderPath = "C:\\PDFs\\";
        if (!Directory.Exists(folderPath))
        {
            Directory.CreateDirectory(folderPath);
        }
        using (FileStream stream = new FileStream(folderPath + "Priemnica.pdf", FileMode.Create))
        {
            Document pdfDoc = new Document(PageSize.A4, 10, 10, 10, 50);
            PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);

            writer.PageEvent = new PDFFooter();

            pdfDoc.Open();
            pdfDoc.Add(pdfTable);

         //   pdfDoc.Add(footer);
            pdfDoc.Add(lineTable);
          //  pdfDoc.Add(nested222);
            pdfDoc.Close();
            stream.Close();
        }

        System.Diagnostics.Process.Start(folderPath + "Something.pdf");
    }

0 个答案:

没有答案