使用PDFsharp

时间:2016-09-09 18:32:24

标签: c# pdf gridview pdfsharp

我有一个用于确认的页面,我需要生成一个基本上与之前页面值相同的PDF文件,我放了一个徽标和一个标题,我需要传递来自会话的gridview值,我试图弄清楚如何使用PDFsharp做到这一点,但无法找到方法。

这是我目前的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;

        }

这些是需要在“Texto”下进行的值,它不需要是一个很好的网格,只是为了显示值是正确的。

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();
        }

1 个答案:

答案 0 :(得分:1)

PDFsharp为您提供所需的一切 - 在较低的层次上:DrawString,DrawRectangle,DrawLine,MeasureString,...

MigraDoc为您提供所需的一切 - 更高层次:AddTable,AddParagraph,......

我会使用MigraDoc http://pdfsharp.net/wiki/HelloMigraDoc-sample.ashx