using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        string temp="";

   //取得选择的资料

        for (int i = 0; i <= GridView1.Rows.Count - 1;i++ )
            {
                CheckBox chkb = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
                if (chkb.Checked)
                {
                    temp += chkb.Text + ",";
                }
            }
        if (temp != "")
        {
            temp = temp.Substring(0, temp.Length - 1);
            Response.ContentType = "application/pdf";
            Response.AddHeader("Content-disposition", "attachment; filename=tokens.pdf");

            string[] temp1 = temp.Split(',');

    //产生临时套版,存在时先删掉

            if (File.Exists(Request.MapPath("~/temp.pdf")))
            {
                File.Delete(Request.MapPath("~/temp.pdf"));
            }
            string MasterPdfFile = Request.MapPath("~/temp.pdf");
            PdfCopyFields mypdfcopy = new PdfCopyFields(new FileStream(MasterPdfFile, FileMode.CreateNew));
            int i = 0;
            foreach (string temp2 in temp1)
            {
                SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["cns_mcl"].ConnectionString);
                SqlCommand sqlcmd = new SqlCommand("", conn);
                conn.Open();
                sqlcmd.CommandText = "select post_no,pos_title,pos_text from post where post_no=@post_no";
                sqlcmd.Parameters.AddWithValue("@post_no", temp2);

                SqlDataReader reader = sqlcmd.ExecuteReader();

      //套用使用的样版pdf

                PdfReader pdfReader = new PdfReader(Request.MapPath("~/fund_bill.pdf"));
                string tmpfilepath = Request.MapPath("~/temp/temp" + i.ToString() + ".pdf");
                FileStream tmppgoutputfile = new FileStream(tmpfilepath, FileMode.CreateNew);
                PdfStamper pdfStamper = new PdfStamper(pdfReader, tmppgoutputfile);
                //PdfStamper pdfStamper = new PdfStamper(pdfReader, Response.OutputStream);
                //PdfStamper pdfStamper = new PdfStamper(pdfReader, ms);
                pdfStamper.Writer.CloseStream = false;
                pdfStamper.FormFlattening = false;
                AcroFields pdfForm = pdfStamper.AcroFields;
                BaseFont.AddToResourceSearch("iTextAsian.dll");
                string fontPath = Environment.GetFolderPath(Environment.SpecialFolder.System) + @"".."Fonts"kaiu.ttf";

                BaseFont bfChinese = BaseFont.CreateFont(
                    fontPath,
                    BaseFont.IDENTITY_H,
                    BaseFont.NOT_EMBEDDED
                );
                //BaseFont bfChinese = BaseFont.CreateFont("MSung-Light", "UniCNS-UCS2-H", BaseFont.EMBEDDED);
                //            string fontPath = Environment.GetFolderPath(Environment.SpecialFolder.System) +
                //@"".."Fonts"comic.TTF";
                //            BaseFont bfChinese = BaseFont.CreateFont(
                //                 fontPath,
                //                 BaseFont.IDENTITY_H, //横式中文
                //                 BaseFont.NOT_EMBEDDED
                //             );
                //BaseFont bfChinese = BaseFont.CreateFont("MSung-Light", "UniCNS-UCS2-H", BaseFont.EMBEDDED);
                Font fontChinese = new Font(bfChinese, 16f, Font.NORMAL);
                if (reader.Read())
                {
                    pdfForm.SetFieldProperty("comp_name_1", "textfont", bfChinese, null);
                    pdfForm.SetField("comp_name_1", reader["post_no"].ToString());
                    pdfForm.SetFieldProperty("comp_name_2", "textfont", bfChinese, null);
                    pdfForm.SetField("comp_name_2", reader["pos_title"].ToString());
                    pdfForm.SetFieldProperty("comp_name_3", "textfont", bfChinese, null);
                    pdfForm.SetField("comp_name_3", reader["pos_text"].ToString());
                }
                reader.Dispose();
                conn.Close();
                pdfStamper.Close();
                tmppgoutputfile.Close();
                PdfReader nwpgreader = new PdfReader(tmpfilepath);
                PdfTemplate page = pdfStamper.GetImportedPage(nwpgreader, 1);
                mypdfcopy.AddDocument(nwpgreader);
                i++;
            }
            mypdfcopy.Close();
            PdfReader respreader = new PdfReader(MasterPdfFile);
            PdfStamper respOutStamper = new PdfStamper(respreader, Response.OutputStream);
            respOutStamper.Close();
            if (Directory.Exists(Request.MapPath("~/temp")))
            {
                Directory.Delete(Request.MapPath("~/temp"), true);
                Directory.CreateDirectory(Request.MapPath("~/temp"));
            }

        }
    }
}
posted on 2009-06-10 13:47  chenshichang  阅读(1009)  评论(0)    收藏  举报