Search code examples
c#pdfgridviewpdfsharp

Grid view to PDF with PDFsharp


I have a page that is for confirmation, I need to generate a PDF file basically with the same values as the page before, I put a logo and a header,under that I need to pass gridview values that come from a session, I'm trying to figure out how to do this with PDFsharp but cant find a way.

This is my current PDF:

 public void Gera_Pdf()
        {
            PdfDocument pdf = new PdfDocument();
            pdf.Info.Title = "My First PDF";
            PdfPage pdfPage = pdf.AddPage();
            string texto = "                                                                              Dados do Aceite                                                                                                                                                                   " + 
                                                                                                                               "Empresa:" + lblEmpresas.Text + " /Representante:" + lblRepresentante.Text + " /CNPJ:" +lblCnpj.Text;
            XFont font = new XFont("Times New Roman", 10, XFontStyle.Bold);
            XGraphics gfx = XGraphics.FromPdfPage(pdfPage);
            XTextFormatter tf = new XTextFormatter(gfx);
            DrawImage(gfx, "D:/PDF Sharp/Images/Klabin.png", 50, 50, 50, 50);
            XRect rect = new XRect(60, 100, 650, 30);
            gfx.DrawRectangle(XBrushes.White, rect);
            //tf.Alignment = ParagraphAlignment.Left;
            tf.DrawString(texto, font, XBrushes.Black, rect, XStringFormats.TopLeft);

            string pdfFilename = "D:/PDF Sharp/PDF/PdfTeste.pdf";
            pdf.Save(pdfFilename);
            byte[] pdfData;
            using (var ms = new MemoryStream())
            {
                pdf.Save(ms);
                pdfData = ms.ToArray();
            }
            Session["pdfBytes"] = pdfData;

        }

And these are the values that need to go under the "Texto", it doesn't need to be a nice grid, just to show the values is alright.

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);
                }
            }
            grdSimulacao.DataSource = dt;
            grdSimulacao.DataBind();
        }

Solution

  • PDFsharp gives you all you need - on a low level: DrawString, DrawRectangle, DrawLine, MeasureString, ...

    MigraDoc gives you all you need - on a higher level: AddTable, AddParagraph, ...

    I would use MigraDoc.
    http://pdfsharp.net/wiki/HelloMigraDoc-sample.ashx