asp.net 產生PDF方法

來源:互聯網
上載者:User

標籤:blog   http   os   io   檔案   資料   for   ar   

今天轉部落格園看到有人發表了一篇產生PFd的文章,準備自己也留一份準備以後用到的時候方便調用;

首先去itextsharp網站下載控制項(https://sourceforge.net/projects/itextsharp)

將下載後的控制項引用到自己的項目裡面,主要的bll檔案為:itextsharp.dll檔案

1、根據DataTable產生PDF檔案

添加Itextsharp引用

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

應用代碼

 /// <summary>        /// 轉換資料表為PDF文檔        /// </summary>        /// <param name="Data">資料表資料</param>        /// <param name="PDFFile">目標PDF檔案全路徑</param>        /// <param name="FontPath">字型所在路徑</param>        /// <param name="FontSize">字型大小</param>        /// <returns>返回調用是否成功</returns>        public static bool ConvertDataTableToPDF(DataTable datatable, string PDFFilePath, string FontPath, float FontSize)        {            //初始化一個目的文件類            Document document = new Document();            //調用PDF的寫入方法流            //注意FileMode-Create表示如果目標檔案不存在,則建立,如果已存在,則覆蓋。            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(PDFFilePath, FileMode.Create));            //開啟目的文件對象            document.Open();            //建立PDF文檔中的字型            BaseFont baseFont = BaseFont.CreateFont(                FontPath,                BaseFont.IDENTITY_H,                BaseFont.NOT_EMBEDDED);            //根據字型路徑和字型大小屬性建立字型            Font font = new Font(baseFont, FontSize);            //根據資料表內容建立一個PDF格式的表            PdfPTable table = new PdfPTable(datatable.Columns.Count);            //遍曆原table的內容            for (int i = 0; i < datatable.Rows.Count; i++)            {                for (int j = 0; j < datatable.Columns.Count; j++)                {                    table.AddCell(new Phrase(datatable.Rows[i][j].ToString(), font));                }            }            //在目的文件中添加轉化後的表資料            document.Add(table);            //關閉目標檔案            document.Close();            //關閉寫入流            writer.Close();            return true;        }       /// <summary>        /// 產生Dataset        /// </summary>        /// <returns></returns>        private DataSet GetSet()        {            DataSet ds = new DataSet();            string sql = "select * from T_AjaxXML";            string Config = ConfigurationManager.ConnectionStrings["Config"].ConnectionString;            using (SqlConnection cnn = new SqlConnection(Config))            {                using (SqlCommand cmm = new SqlCommand(sql, cnn))                {                    SqlDataAdapter dapter = new SqlDataAdapter(cmm);                    dapter.Fill(ds);                }            }            return ds;        }調用方法: ConvertDataTableToPDF(GetSet().Tables[0], Server.MapPath("~/Table.pdf"), "C:\\Windows\\Fonts\\simsun.ttc,1", 14);

2、根據常值內容產生PDF檔案

 private void CreateTxt(string txt, string filepath)        {            //建立文檔對象            Document document = new Document();            //執行個體化產生的文檔            PdfWriter.GetInstance(document, new FileStream(filepath, FileMode.Create));            //開啟文檔            document.Open();            //在文檔中添加常值內容            document.Add(new Paragraph(txt));            //關閉文檔對象            document.Close();        }

3、產生頁首頁尾(不過iTextSharp5.5這個版本沒有了這個方法,麻煩親們幫忙找一個)

 private void CreatePDFheader(string filepath, string headertxt, string footertxt)        {            //建立文檔對象            Document document = new Document();            // 建立文檔寫入執行個體            PdfWriter.GetInstance(document, new FileStream(filepath, FileMode.Create));            PdfPageEventHelper hh = new PdfPageEventHelper();                       // 添加頁尾            HeaderFooter footer = new HeaderFooter(new Phrase(footertxt), true);            footer.Border = Rectangle.NO_BORDER;            document.Footer = footer;            //開啟文檔內容對象            document.Open();            // 添加頁首            HeaderFooter header = new HeaderFooter(new Phrase(headertxt), false);            document.Header = header;            //設計各頁的內容            document.Add(new Paragraph("This is First Page"));            //新添加一個頁            document.NewPage();            //第2頁中添加文本            document.Add(new Paragraph("This is Second Page"));            // 重設頁面數量            document.ResetPageCount();            //關閉文檔對象            document.Close();        }

  

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.