利用 iTextSharp 將資料轉成pdf檔下載

摘要:利用 iTextSharp 將資料轉成pdf檔下載

using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using System.IO;

public partial class convertPDF : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        MaintainScrollPositionOnPostBack = true;
        WritePDF();
    }
    protected void WritePDF()
    {
        Document doc = new Document(PageSize.A4, 50, 50, 80, 50);//輸出尺寸設定 / 邊界設定
        MemoryStream Memory = new MemoryStream();//存放資料的記憶體資料流
       
       // 覆寫方式
        PdfWriter PdfWriter = PdfWriter.GetInstance(doc, Memory);
       // PdfWriter Pdfwrite = PdfWriter.GetInstance(doc, new FileStream(path + "/pdfexample4.pdf", FileMode.Create));

        //字型設定
        BaseFont bfChinese = BaseFont.CreateFont(@"C:\WINDOWS\Fonts\KAIU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        Font ChFont = new Font(bfChinese, 12);
       // Font ChFont_blue = new Font(bfChinese, 40, Font.NORMAL, new BaseColor(51, 0, 153));
       // Font ChFont_msg = new Font(bfChinese, 12, Font.ITALIC, BaseColor.RED);

        doc.Open();
        PdfPTable Memoirstable = new PdfPTable(4);
        Memoirstable.TotalWidth = 500f;
        Memoirstable.LockedWidth = true;
        PdfPCell MemoirsCT = new PdfPCell(new Phrase("自傳", ChFont));
        MemoirsCT.Colspan = 4;
        Memoirstable.AddCell(MemoirsCT);

      for(int i=1 ; i<=10 ; i++)
      {
            PdfPCell MemoirsC = new PdfPCell(new Phrase("測試文字"+i.ToString(), ChFont));
            MemoirsC.Colspan = 2;
            Memoirstable.AddCell(MemoirsC);
        }

        iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(new Uri(Server.MapPath("~/images/tana.jpg")));
        iTextSharp.text.Image jpg2 = iTextSharp.text.Image.GetInstance(new Uri(Server.MapPath("~/images/Taipei.jpg")));


        Memoirstable.AddCell( new PdfPCell(new Phrase("TOEIC成績上傳", ChFont)));
        Memoirstable.AddCell(jpg2);
        Memoirstable.AddCell("row3");
        Memoirstable.AddCell("row4");

        PdfPCell jpgcell = new PdfPCell(jpg,true);

        Memoirstable.AddCell(jpgcell);
        Memoirstable.AddCell("row5");
        Memoirstable.AddCell("row6");
        Memoirstable.AddCell("row7");
        Memoirstable.AddCell("row8");
        Memoirstable.AddCell("row9");
       // doc2.Add(TOEICSCOREURLtable);
        doc.Add(Memoirstable);

        //=========================輸出/釋放資源=========================
        doc.Close();
        string Strfilename = "員工資料" + System.DateTime.Now.ToString();
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=UTF-8>");
        Strfilename = HttpUtility.UrlEncode(Strfilename + ".pdf", System.Text.Encoding.GetEncoding("UTF-8"));
        HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", Strfilename));
        HttpContext.Current.Response.ContentType = "application/octet-streem";

        Response.OutputStream.Write(Memory.GetBuffer(), 0, Memory.GetBuffer().Length);
        Response.OutputStream.Flush();
        Response.OutputStream.Close();
        Response.Flush();
        Response.End();
        //==================================================================
    }

}