根据SQL结果生成多个PDF

时间:2018-08-09 18:32:52

标签: c# itext

我正在生成带有sql查询结果的pdf文件。 sql可能返回1个以上的booking_id。现在要做的是,将所有预订ID的结果放入一个pdf中。我想获取每个预订ID并生成其pdf。基本上,每个booking_id 1个pdf,每个zip压缩(这是一个要求)。不确定iTextSharp是否可以在这里提供帮助。

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.IO.Compression;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace ConvertPdf
{
class Program
{

     private static DataTable GetData(SqlCommand cmd)
    {
        DataTable dt = new DataTable();
        String strConnString = ConfigurationManager.AppSettings["dbConn"];
        SqlConnection con = new SqlConnection(strConnString);
        SqlDataAdapter sda = new SqlDataAdapter();
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;
        try
        {
            con.Open();
            sda.SelectCommand = cmd;
            sda.Fill(dt);
            return dt;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
            sda.Dispose();
            con.Dispose();
        }
    }



    static void Main(string[] args)
    {

        //Get the data from database into datatable
        string strQuery = "select * from emp_booking where emp_booking_id in (select e.emp_booking_id from emp_booking e, emp_booking_file ef where ef.booking_date>=getdate() and e.emp_booking_id = ef.emp_booking_id)";
        SqlCommand cmd = new SqlCommand(strQuery);
        DataTable dt = GetData(cmd);
        Transpose(dt);
        int i=1;

        foreach(DataRow r in dt.Rows)
        {
        Document document = new Document();

        PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("M://Bookingsheet/Pdf/Bookingsheet" + i + ".pdf", FileMode.Create));

        document.Open();
        iTextSharp.text.Font font5 = iTextSharp.text.FontFactory.GetFont(FontFactory.HELVETICA, 5);

        PdfPTable table = new PdfPTable(dt.Columns.Count);


        PdfPRow row = null;
        float[] widths = new float[] { 4f, 4f, 4f, 4f, 4f };

        table.SetWidths(widths);

        table.WidthPercentage = 100;
        int iCol = 0;
        string colname = "";
        PdfPCell cell = new PdfPCell(new Phrase("EmpSheet"));

        cell.Colspan = dt.Columns.Count;

       foreach (DataColumn c in dt.Columns)
        {

            table.AddCell(new Phrase(c.ColumnName, font5));
        }


            if (dt.Rows.Count > 0)
            {
                table.AddCell(new Phrase(r[0].ToString(), font5));
                table.AddCell(new Phrase(r[1].ToString(), font5));
                table.AddCell(new Phrase(r[2].ToString(), font5));
                table.AddCell(new Phrase(r[3].ToString(), font5));
                table.AddCell(new Phrase(r[4].ToString(), font5));
            }

        document.Add(table);
        document.Close();
        ZipFile.CreateFromDirectory(@"D://BookingSheet/Pdf", @"M://BookingSheetUpload/Zip/Bookingsheet.zip");
        i++;
    }
}
}
}

1 个答案:

答案 0 :(得分:1)

虽然我不熟悉您的数据库的架构或所需的PDF格式,但我相信按以下方式重构Main方法将导致每个booking_id的PDF离散。

   static void Main(string[] args)
    {
        string strQuery = "select * from emp_booking where emp_booking_id in (select e.emp_booking_id from emp_booking e, emp_booking_file ef where ef.booking_date>=getdate() and e.emp_booking_id = ef.emp_booking_id)";
        SqlCommand cmd = new SqlCommand(strQuery);
        DataTable dt = GetData(cmd);
        Transpose(dt);

        foreach (DataRow r in dt)
        {
            Document document = new Document();
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("<Unique filename for each booking ID>", FileMode.Create));
            document.Open();
            iTextSharp.text.Font font5 = iTextSharp.text.FontFactory.GetFont(FontFactory.HELVETICA, 5);

            PdfPTable table = new PdfPTable(dt.Columns.Count);
            float[] widths = new float[] { 4f, 4f, 4f, 4f, 4f };
            table.SetWidths(widths);
            table.WidthPercentage = 100;

            foreach (DataColumn c in dt.Columns)
            {
                table.AddCell(new Phrase(c.ColumnName, font5));
            }

            table.AddCell(new Phrase(r[0].ToString(), font5));
            table.AddCell(new Phrase(r[1].ToString(), font5));
            table.AddCell(new Phrase(r[2].ToString(), font5));
            table.AddCell(new Phrase(r[3].ToString(), font5));
            table.AddCell(new Phrase(r[4].ToString(), font5));

            document.Add(table);
            document.Close();
            ZipFile.CreateFromDirectory(@"<parent directory of pdf>", @"M://BookingSheetUpload/Zip/<unique zip name>.zip");
        }
    }

这将遍历查询返回的每个DataRow并每行生成一个PDF。请注意,每个PDF和.zip文件都应分配一个唯一的名称。

我也可能建议将其中一些代码移入其他类,或者至少移至Program类的静态方法。同样,我对您的应用程序不熟悉,但是Main方法似乎在这里做了很多繁重的工作。