2016-09-09 71 views
0

我有一个页面用于确认,我需要生成一个基本上与页面具有相同值的PDF文件,我在此之前放置了一个徽标和一个标题,然后我需要传递gridview值来自一个会话,我试图找出如何用PDFsharp做到这一点,但无法找到一种方法。使用PDFsharp格式查看PDF到PDF

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

PDFsharp给你所有你需要 - 在较低的水平:的DrawString,DrawRectangle的,DrawLine的,MeasureString,...

MigraDoc给你所有你需要 - 上了一个台阶:AddTable,AddParagraph, ...

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