.net 生成pdf表格

只需要建一个类文件就搞定了

 public class CreatePDF
    {
        public static CreatePDF Current { get { return new CreatePDF(); } }
        public void CreatePDFs(RBS.Models.UserConfirmModel Model)
        {
            Document document = new Document();
            string filepath = "/Upload/Pdf/";
            Directory.CreateDirectory(HttpContext.Current.Server.MapPath(filepath));
            PdfWriter.GetInstance(document, new FileStream(HttpContext.Current.Server.MapPath(filepath + "001.pdf"), FileMode.Create));
            document.Open();
            BaseFont bftitle = BaseFont.CreateFont(@"C:\\Windows\Fonts\SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            Font fonttitle = new Font(bftitle, 20, Font.UNDERLINE);
            BaseFont bf1 = BaseFont.CreateFont(@"C:\\Windows\Fonts\SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            Font font1 = new Font(bf1, 12);
            Font fonttitle10 = new Font(bf1, 12);
            Font fonttitle12 = new Font(bf1, 11);
            PdfPCell cellmode = new PdfPCell();

            cellmode.Padding = 0;
            Paragraph Title = new Paragraph("信息总表", fonttitle);
            Title.SetAlignment("center");
            document.Add(Title);
            PdfPTable topTable = new PdfPTable(1);
            PdfPCell topCell = new PdfPCell();
            topCell.PaddingTop = 10F;
            topCell.Colspan = 1;
            topCell.Border = 0;
            topTable.AddCell(topCell);
            document.Add(topTable);
            PdfPTable table = new PdfPTable(3);
            CreateCell(2, false, "广州XXXXXX公司", fonttitle10, "left", table);
            CreateCell(1, false, "字第190898765号", fonttitle10, "right", table);
            CreateCell(3, 5, 0, "兹批准  广州市XXXXXXX分公司 去购买下列物品", fonttitle10, "center", table);
            CreateCell(1, true, "物品名称", fonttitle10, "center", table);
            CreateCell(1, true, "数量", fonttitle10, "center", table);
            CreateCell(1, true, "备注", fonttitle10, "center", table);
            string[] arrNames = { "物品名称1", " 物品名称2", " ", " " };
            string[] arrCounts = { "10吨", "800公斤", "", "" };
            for (int i = 0; i < arrNames.Length; i++)
            {
                CreateCell(1, 5, 1, arrNames[i], fonttitle10, "center", table);
                CreateCell(1, 5, 1, arrCounts[i], fonttitle10, "center", table);
                CreateCell(1, 5, 1, " ", fonttitle10, "center", table);
            }
            CreateCell(3, false, " 此证由填发日起止2013年03月19日有效 逾期作废", fonttitle10, "left", table);
            CreateCell(3, false, " 到本县、市以外购买物品时,必须经出售地政府机关在备注栏内盖章后方能有效", fonttitle10, "left", table);
            CreateCell(1, 5, 0, " \r填发人:XXX", fonttitle10, "left", table);
            CreateCell(2, false, "广东省广州市\r\n2013年3月5日", fonttitle10, "right", table);

            document.Add(table);
            document.Close();
        }


        ///
        /// 生成单元格
        ///
        /// 合并列数
        /// 是否需要边框线
        /// 表格内显示的内容
        /// 内容字体
        /// 对齐方式
        /// 此单元格填充的表
        private void CreateCell(int Colspan, bool Border, string Content, Font font, string alignment, PdfPTable table)
        {
            if (Border)
                CreateCell(Colspan, 0, 1, Content, font, alignment, table);
            else
                CreateCell(Colspan, 0, Content, font, alignment, table);
        }
        ///
        /// 生成单元格
        ///
        /// 合并列数
        /// 间距
        /// 内容
        /// 字体
        /// 对齐方式
        /// 此单元格填充的表
        private void CreateCell(int Colspan, int Padding, string Content, Font font, string alignment, PdfPTable table)
        {
            CreateCell(Colspan, Padding, 0, Content, font, alignment, table);
        }
        ///
        /// 生成单元格
        ///
        /// 合并列数
        /// 间距
        /// 边框线
        /// 内容
        /// 字体
        /// 对齐方式
        /// 此单元格填充的表
        private void CreateCell(int Colspan, int Padding, int Border, string Content, Font font, string alignment, PdfPTable table)
        {
            CreateCell(Colspan, Padding, Border, 0, 0, Content, font, alignment, table);
        }
        ///
        /// 生成单元格
        ///
        /// 合并列数
        /// 间距
        /// 边框线
        /// 水平对齐方式
        /// 垂直对齐方式
        /// 内容
        /// 字体
        /// 对齐方式
        /// 此单元格填充的表
        private void CreateCell(int Colspan, int Padding, int Border, int HorizontalAlignment, int VerticalAlignment, string Content, Font font, string alignment, PdfPTable table)
        {
            PdfPCell cell = new PdfPCell();
            cell.Colspan = Colspan;
            cell.Padding = Padding;
            if (HorizontalAlignment > 0)
                cell.HorizontalAlignment = HorizontalAlignment;
            if (VerticalAlignment > 0)
                cell.VerticalAlignment = VerticalAlignment;
            if (Border == 0)
                cell.Border = Border;
            Paragraph table_t = new Paragraph(Content, font);
            table_t.SetAlignment(alignment);
            cell.AddElement(table_t);
            table.AddCell(cell);
        }

    }

 

posted @ 2013-12-24 11:34  程序新青年  阅读(513)  评论(0编辑  收藏  举报
============================================================================== 青春匆匆,很多人都有自己的座右铭,鞭策自己前进,当没看到座右铭的时候又忘了自己要干什么,就这样天天立志,志天天立,最终还是那个初出茅庐的小菜鸟。从现在开始,慢慢去改掉懒惰的习惯。慢慢去加强学习,直到慢慢成功。==============================================================================