Export pdf file using ITextSharp

Posted Joye Zhou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Export pdf file using ITextSharp相关的知识,希望对你有一定的参考价值。

最近项目中需要导出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的主要内容,如果未能解决你的问题,请参考以下文章

phpexcel将选定的工作表导出为pdf

Allegro16.6导出位号图

测试套装因“SyntaxError: Unexpected token 'export”' react typescript using jest 失败

[Slide] 解决Slidev export导出pdf报错

在 UNO LibreOffice 中使用 writer_pdf_Export 过滤器时出现问题

WYSIWYG for Matlab export_fig