码迷,mamicode.com
首页 > 其他好文 > 详细

Export pdf file using ITextSharp

时间:2016-05-26 18:50:34      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:

最近项目中需要到处PDF文件,最后上网搜索了一下,发现ITextSharp比较好用,所以做了一个例子:

技术分享
public string ExportPDF()
        {
            //ITextSharp Usage
            //Steps:1. Add content to cell;2. Add cell to table;3. Add table to document;4. Add document to rectangle;
            string sAbsolutePath = ControllerContext.HttpContext.Server.MapPath(this.path);
            string FileName = string.Format("Notice_{0}.pdf", DateTime.Now.ToString("yyyyMMddHHmmss"));
            BaseFont bf = BaseFont.CreateFont("C:/WINDOWS/Fonts/Arial.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            Rectangle rec = new Rectangle(1000, 500);
            Document document = new Document(rec);
            document.SetMargins(10f, 10f, 10f, 10f);
            PdfWriter pdfwriter = PdfWriter.GetInstance(document,
                    new System.IO.FileStream(sAbsolutePath + "\\" + FileName, System.IO.FileMode.Create)
                );

            document.Open();
            Font font = new Font(bf);

            try
            {
                PdfPTable pdftable = new PdfPTable(5);
                pdftable.HorizontalAlignment = 1;

                //Set cell width
                float[] cellwidth = { 3f, 5f, 5f, 3.5f, 5f };
                pdftable.SetWidths(cellwidth);

                //Header           
                Font PDFFontA = FontFactory.GetFont("Arial", 20, Font.BOLD);
                Font PDFFontB = FontFactory.GetFont("Arial", 15, Font.BOLD);
                Font PDFFontC = FontFactory.GetFont("Arial", 10);

                //Image Object
                Image jpeg = Image.GetInstance(HttpContext.Server.MapPath("../images/buddy.jpg"));
                PdfPCell cell = new PdfPCell(jpeg);
                cell.FixedHeight = 40f;
                cell.Colspan = 3;
                cell.Rowspan = 4;
                cell.Border = 0;
                pdftable.AddCell(cell);

                cell = new PdfPCell(new Phrase(("Did You Know?"), PDFFontB));
                cell.Colspan = 3;
                cell.Rowspan = 2;
                cell.Border = 0;
                cell.BackgroundColor = new BaseColor(247, 238, 214);//RGB Color Value
                cell.HorizontalAlignment = 1;
                pdftable.AddCell(cell);

                cell = new PdfPCell(new Phrase("\nTitle: Hello World!\nDate: " + DateTime.Now.ToString("MM/dd/yyyy") + "\n\n", PDFFontC));
                cell.Colspan = 4;
                cell.Rowspan = 2;
                cell.Border = 0;
                cell.BackgroundColor = new BaseColor(247, 238, 214);//RGB Color Value
                cell.HorizontalAlignment = 1;
                pdftable.AddCell(cell);

                cell = new PdfPCell(new Phrase("Notice", PDFFontA));
                cell.BackgroundColor = new BaseColor(230, 0, 0);//RGB Color Value
                cell.Colspan = 10;
                cell.Padding = 5f;
                cell.HorizontalAlignment = 1;
                pdftable.AddCell(cell);

                string[] AssHeader = { "DateTime", "Name", "Description", "Icon", "Notes"};
                for (int i = 0; i < AssHeader.Length; i++)
                {
                    cell = new PdfPCell(new Phrase(AssHeader[i], PDFFontB));
                    cell.HorizontalAlignment = 1;
                    cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                    cell.BackgroundColor = new BaseColor(230, 0, 0);
                    cell.PaddingBottom = 4f;
                    cell.PaddingTop = 4f;
                    pdftable.AddCell(cell);
                }
                for (int i = 0; i < 100; i++)
                {
                    cell = new PdfPCell(new Phrase(new DateTime().ToShortDateString(), PDFFontC));
                    cell.HorizontalAlignment = 1;
                    cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                    cell.PaddingBottom = 4f;
                    cell.PaddingTop = 4f;
                    pdftable.AddCell(cell);

                    cell = new PdfPCell(new Phrase("Jack Chean", PDFFontC));
                    cell.HorizontalAlignment = 1;
                    cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                    cell.PaddingBottom = 4f;
                    cell.PaddingTop = 4f;
                    pdftable.AddCell(cell);

                    cell = new PdfPCell(new Phrase("Just for my testing!!", PDFFontC));
                    cell.HorizontalAlignment = 1;
                    cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                    cell.PaddingBottom = 4f;
                    cell.PaddingTop = 4f;
                    pdftable.AddCell(cell);

                    cell = new PdfPCell(new Phrase("ICON", PDFFontC));
                    cell.HorizontalAlignment = 1;
                    cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                    cell.PaddingBottom = 4f;
                    cell.PaddingTop = 4f;
                    pdftable.AddCell(cell);

                    cell = new PdfPCell(new Phrase("For Tom!", PDFFontC));
                    cell.HorizontalAlignment = 1;
                    cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                    cell.PaddingBottom = 4f;
                    cell.PaddingTop = 4f;
                    pdftable.AddCell(cell);
                }
                //Very important
                //You can display the header in each page through this properity:HeaderRows
                pdftable.HeaderRows = 3;

                document.Add(pdftable);
                //Pager
                float tableheight = pdftable.CalculateHeights();
                if (tableheight < 500 && tableheight > 350)
                {
                    document.NewPage();
                }

                document.NewPage();
                document.Close();
            }
            catch (Exception ex)
            {
                document.Add(new Paragraph(ex.Message.ToString(), font));
            }
            return FileName;
        }
View Code

最后的显示效果:

技术分享

参考文章:http://blog.csdn.net/adgjlxxx/article/details/43307227

Export pdf file using ITextSharp

标签:

原文地址:http://www.cnblogs.com/sccd/p/5526628.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!