【问题标题】:Using iTextSharp to print a PDF with multiple tables使用 iTextSharp 打印包含多个表格的 PDF
【发布时间】:2014-06-07 18:25:55
【问题描述】:

我有一份详细的帐单要打印,其中包含各个部门的帐单详细信息。我需要将它们打印在 PDF 上。但是我使用了大量的选择过程,因为我必须显示很多表格,所以我需要可以重用的代码。

【问题讨论】:

    标签: c# asp.net sql-server pdf itextsharp


    【解决方案1】:

    我使用的命名空间:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.IO;
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using iTextSharp.text.html;
    using iTextSharp.text.html.simpleparser;
    

    生成 PDF 的Print 按钮单击事件:

    protected void btnPrintToPDF_Click(object sender, EventArgs e)
        {
        //Default Settings for your PDF File
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ContentType = "application/pdf";
        HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.pdf", "PDFExport"));
        HttpContext.Current.Response.Charset = "utf-8";
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
        HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    
        //Initialize a StringSwriter
        StringWriter stringWrite = new StringWriter();
        DataTable dt = new DataTable();
    
        //Info line in PDF
        stringWrite.WriteLine("Clothing department");
        //Fetch DataTable
        dt = getDataTablebyProcedure("SelectFromClothingDepartment");
        //Append it to String Writer
        stringWrite = getStringWriterbyDataTable(dt, stringWrite);
    
        //Info line in PDF
        stringWrite.WriteLine("Food Department");
        //Fetch DataTable
        dt = getDataTablebyProcedure("SelectFromFoodDepartment");
        //Append it to Stwing Writer
        stringWrite = (getStringWriterbyDataTable(dt, stringWrite));
    
        //As many procedures as you want to go here
    
        //Finally Write the writer into a reader
        StringReader sr = new StringReader(stringWrite.ToString());
        //Generate the pdf
        Document pdfDoc = new Document(new Rectangle(288f, 144f), 10f, 10f, 10f, 0f);
        pdfDoc.SetPageSize(PageSize.A4.Rotate());
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);
        pdfDoc.Open();
        htmlparser.Parse(sr);
        pdfDoc.Close();
        HttpContext.Current.Response.Write(pdfDoc);
        HttpContext.Current.Response.End();
        }
    

    返回Select 过程的数据表的过程

     protected DataTable getDataTablebyProcedure(string ProcedureName)
        {
        SqlDataAdapter da= new SqlDataAdapter();
        DataTable dt=new DataTable();
        try
            {//open a connection to your DB
            da.Fill(dt);
            }
        catch (Exception ex)
            {//handle Exception
            }
        finally
            {//Close Connection
            }
        return dt;
        }
    

    将数据表附加到您的StringWriter的函数

    protected static StringWriter getStringWriterbyDataTable(DataTable dt, StringWriter stringWrite1 )
        {
        //System.IO.StringWriter stringWrite1 = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite1 = new HtmlTextWriter(stringWrite1);
        DataGrid myDataGrid1 = new DataGrid();
        myDataGrid1.DataSource = dt;
        myDataGrid1.DataBind();
        myDataGrid1.RenderControl(htmlWrite1);
        return stringWrite1;
        }
    

    嗯,这对我来说非常有效,所以我希望有一天它对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2012-06-15
      • 2016-11-07
      • 2011-08-31
      • 1970-01-01
      • 2015-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多