【问题标题】:GridviewRow to PDF with iTextSharp使用 iTextSharp 将 GridviewRow 转换为 PDF
【发布时间】:2016-09-14 16:31:55
【问题描述】:

我有一个确认页面,在这个页面我需要放一个和之前页面一模一样的网格,这个就是网格。

要创建此网格,我使用此代码,值来自会话。

public void Carrega_valores()
        {
            GridViewRow[] valoresNovos = new GridViewRow[300];
            valoresNovos = (GridViewRow[])Session["vlColunas"];
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("Belnr"));
            dt.Columns.Add(new DataColumn("Dtemissao"));
            dt.Columns.Add(new DataColumn("Dtvenc"));
            dt.Columns.Add(new DataColumn("DIASANTEC"));
            dt.Columns.Add(new DataColumn("Valor"));
            dt.Columns.Add(new DataColumn("VLENCARGOS"));
            dt.Columns.Add(new DataColumn("VLFINAL"));
            foreach (GridViewRow grdCount in valoresNovos)
            {
                if (grdCount != null)
                {
                    DataRow dr = dt.NewRow();
                    dr[0] = grdCount.Cells[1].Text;
                    dr[1] = grdCount.Cells[2].Text;
                    dr[2] = grdCount.Cells[3].Text;
                    if(grdCount.Cells[7].Text != " ")
                    {
                        dr[3] = grdCount.Cells[7].Text;
                    }
                    else
                    {
                        dr[3] = "";
                    }

                    dr[4] = grdCount.Cells[5].Text;
                    dr[5] = grdCount.Cells[8].Text;
                    dr[6] = grdCount.Cells[5].Text;
                    dt.Rows.Add(dr);
                }
            }
            Session["DataSetValores"] = dt;
            grdSimulacao.DataSource = dt;
            grdSimulacao.DataBind();
        }

我需要放入 PDF,我使用此代码,但它不起作用。它只创建带有图像和文本的 pdf,但表格不显示。

public void Gera_Pdf()
        {
            using (var fileStream = new System.IO.FileStream("output.pdf", System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None))
            {
                PdfPTable pdfTable = new PdfPTable(7);

                GridViewRow[] valoresNovos = new GridViewRow[300];
                valoresNovos = (GridViewRow[])Session["vlColunas"];
                foreach (GridViewRow row in valoresNovos)

                {
                        pdfTable.AddCell(row.Cells.ToString());
                }

                var document = new iTextSharp.text.Document();
                var pdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(document, fileStream);
                document.Open();
                document.Add(pdfTable);
                iTextSharp.text.FontFactory.RegisterDirectory("C:\\WINDOWS\\Fonts");
                var font = iTextSharp.text.FontFactory.GetFont("Calibri", 14);
                var font2 = iTextSharp.text.FontFactory.GetFont("Calibri", 10);
                var image = iTextSharp.text.Image.GetInstance("Klabin.png");
                image.ScaleToFit(70, 70);
                image.SetAbsolutePosition(25, 780);
                var paragraph = new iTextSharp.text.Paragraph("DADOS DO ACEITE", font);
                paragraph.Alignment = iTextSharp.text.Element.ALIGN_CENTER;
                document.Add(paragraph);
                paragraph = new iTextSharp.text.Paragraph("Empresa:" + lblEmpresas.Text + " /Representante:" + lblRepresentante.Text + " /CNPJ:" + lblCnpj.Text, font2);
                paragraph.Alignment = iTextSharp.text.Element.ALIGN_CENTER;
                document.Add(paragraph);
                var contentByte = pdfWriter.DirectContent;
                contentByte.AddImage(image);

                document.Close();
                byte[] bytes = System.IO.File.ReadAllBytes("output.pdf");
                System.IO.File.WriteAllBytes("myfile.pdf", bytes);
                Session["pdfBytes"] = bytes;
                System.Diagnostics.Process.Start("output.pdf");
            }
        }

有人知道我做错了什么吗?

【问题讨论】:

    标签: c# pdf datagridview itext


    【解决方案1】:

    我设法解决了,我做到了:

    public void Gera_Pdf()
            {
                using (
                    var fileStream = new System.IO.FileStream("output.pdf", System.IO.FileMode.Create,
                        System.IO.FileAccess.Write, System.IO.FileShare.None))
                {
                    var font3 = iTextSharp.text.FontFactory.GetFont("Calibri", 10);
                    DataTable dt = (DataTable) Session["DataSetValores"];
                    PdfPTable pdfTable = new PdfPTable(dt.Columns.Count);
                    PdfPRow row = null;
                    float[] widths = new float[] {4f, 4f, 4f, 4f,4f,4f,4f};
    
                    pdfTable.SetWidths(widths);
    
                    pdfTable.WidthPercentage = 100;
                    int iCol = 0;
                    string colname = "";
                    PdfPCell cell = new PdfPCell(new iText.Phrase("Products"));
    
                    cell.Colspan = dt.Columns.Count;
    
    
    
                        pdfTable.AddCell(new iText.Phrase("NF", font3));
                        pdfTable.AddCell(new iText.Phrase("Emissão", font3));
                        pdfTable.AddCell(new iText.Phrase("Vencimento", font3));
                        pdfTable.AddCell(new iText.Phrase("Dias", font3));
                        pdfTable.AddCell(new iText.Phrase("Valor(R$)", font3));
                        pdfTable.AddCell(new iText.Phrase("Encargos(R$)", font3));
                        pdfTable.AddCell(new iText.Phrase("Vlr. Final(R$)", font3));
    
    
                    foreach (DataRow r in dt.Rows)
                    {
                        if (dt.Rows.Count > 0)
                        {
                            pdfTable.AddCell(new iText.Phrase(r[0].ToString(), font3));
                            pdfTable.AddCell(new iText.Phrase(r[1].ToString(), font3));
                            pdfTable.AddCell(new iText.Phrase(r[2].ToString(), font3));
                            pdfTable.AddCell(new iText.Phrase(r[3].ToString(), font3));
                            pdfTable.AddCell(new iText.Phrase(r[4].ToString(), font3));
                            pdfTable.AddCell(new iText.Phrase(r[5].ToString(), font3));
                            pdfTable.AddCell(new iText.Phrase(r[6].ToString(), font3));
                        }
    
                    }
                    var document = new iTextSharp.text.Document();
                        var pdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(document, fileStream);
                        document.Open();
    
                        iTextSharp.text.FontFactory.RegisterDirectory("C:\\WINDOWS\\Fonts");
                        var font = iTextSharp.text.FontFactory.GetFont("Calibri", 14);
                        var font2 = iTextSharp.text.FontFactory.GetFont("Calibri", 10);
                        var image = iTextSharp.text.Image.GetInstance("Klabin.png");
                        image.ScaleToFit(70, 70);
                        image.SetAbsolutePosition(25, 780);
                        var paragraph = new iTextSharp.text.Paragraph("DADOS DO ACEITE", font);
                        paragraph.Alignment = iTextSharp.text.Element.ALIGN_CENTER;
                        document.Add(paragraph);
                        paragraph =
                            new iTextSharp.text.Paragraph(
                                "Empresa:" + lblEmpresas.Text + " /Representante:" + lblRepresentante.Text + " /CNPJ:" +
                                lblCnpj.Text, font2);
                        paragraph.Alignment = iTextSharp.text.Element.ALIGN_CENTER;
                        document.Add(paragraph);
                        var contentByte = pdfWriter.DirectContent;
                        contentByte.AddImage(image);
                    paragraph = new iTextSharp.text.Paragraph("");
                    document.Add(paragraph);
                    paragraph = new iTextSharp.text.Paragraph("");
                    paragraph.SpacingAfter = 40f;
                    document.Add(paragraph);
    
                    document.Add(pdfTable);
                        document.Close();
                        byte[] bytes = System.IO.File.ReadAllBytes("output.pdf");
                        System.IO.File.WriteAllBytes("myfile.pdf", bytes);
                        Session["pdfBytes"] = bytes;
                        System.Diagnostics.Process.Start("output.pdf");
                    }
                }
    

    【讨论】:

      猜你喜欢
      • 2022-03-07
      • 1970-01-01
      • 2011-06-03
      • 1970-01-01
      • 1970-01-01
      • 2014-09-29
      • 2015-12-19
      相关资源
      最近更新 更多