zoukankan      html  css  js  c++  java
  • C# 利用ITextSharp导出PDF文件

    最近项目中需要导出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("
    Title: Hello World!
    Date: " + DateTime.Now.ToString("MM/dd/yyyy") + "
    
    ", 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

  • 相关阅读:
    STDMETHOD_,STDMETHOD,__declspec(novtable)和__declspec(selectany)
    __stdcall 与 __cdecl
    winows 进程通信的实例详解
    Windows 下多线程编程技术
    MFC/VC++ UI界面美化技术
    VC++中 wstring和string的互相转换实现
    VS2010项目转化为VS2008项目
    VC++ 响应回车键的2种方法
    高效 告别996,开启java高效编程之门 2-4实战:单一条件参数化
    高效 告别996,开启java高效编程之门 2-3实战:硬编码业务逻辑
  • 原文地址:https://www.cnblogs.com/sccd/p/5526628.html
Copyright © 2011-2022 走看看