Add page number in footer of pdf using iTextsharp

Reference:  https://stackoverflow.com/questions/1032614/itextsharp-creating-a-footer-page-of/1372669#1372669

using System;
using System.Collections.Generic;
using System.Text;
using iTextSharp.text.pdf;
using iTextSharp.text;
namespace PDF_Tests
{
    public class TwoColumnHeaderFooter :PdfPageEventHelper
    {
        // This is the contentbyte object of the writer
        PdfContentByte cb;
        // we will put the final number of pages in a template
        PdfTemplate template;
        // this is the BaseFont we are going to use for the header / footer
        BaseFont bf = null;
        // This keeps track of the creation time
        DateTime PrintTime = DateTime.Now;
        #region Properties
        private string _Title;
        public string Title
        {
            get { return _Title; }
            set { _Title = value; }
        }

        private string _HeaderLeft;
        public string HeaderLeft
        {
            get { return _HeaderLeft; }
            set { _HeaderLeft = value; }
        }
        private string _HeaderRight;
        public string HeaderRight
        {
            get { return _HeaderRight; }
            set { _HeaderRight = value; }
        }
        private Font _HeaderFont;
        public Font HeaderFont
        {
            get { return _HeaderFont; }
            set { _HeaderFont = value; }
        }
        private Font _FooterFont;
        public Font FooterFont
        {
            get { return _FooterFont; }
            set { _FooterFont = value; }
        }
        #endregion
        // we override the onOpenDocument method
        public override void OnOpenDocument(PdfWriter writer, Document document)
        {
            try
            {
                PrintTime = DateTime.Now;
                bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
                cb = writer.DirectContent;
                template = cb.CreateTemplate(50, 50);
            }
            catch (DocumentException de)
            {
            }
            catch (System.IO.IOException ioe)
            {
            }
        }

        public override void OnStartPage(PdfWriter writer, Document document)
        {
            base.OnStartPage(writer, document);
            Rectangle pageSize = document.PageSize;
            if (Title != string.Empty)
            {
                cb.BeginText();
                cb.SetFontAndSize(bf, 15);
                cb.SetRGBColorFill(50, 50, 200);
                cb.SetTextMatrix(pageSize.GetLeft(40), pageSize.GetTop(40));
                cb.ShowText(Title);
                cb.EndText();
            }
            if (HeaderLeft + HeaderRight != string.Empty)
            {
                PdfPTable HeaderTable = new PdfPTable(2);
                HeaderTable.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;
                HeaderTable.TotalWidth = pageSize.Width - 80;
                HeaderTable.SetWidthPercentage(new float[] { 45, 45 }, pageSize);

                PdfPCell HeaderLeftCell = new PdfPCell(new Phrase(8, HeaderLeft, HeaderFont));
                HeaderLeftCell.Padding = 5;
                HeaderLeftCell.PaddingBottom = 8;
                HeaderLeftCell.BorderWidthRight = 0;
                HeaderTable.AddCell(HeaderLeftCell);
                PdfPCell HeaderRightCell = new PdfPCell(new Phrase(8, HeaderRight, HeaderFont));
                HeaderRightCell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;
                HeaderRightCell.Padding = 5;
                HeaderRightCell.PaddingBottom = 8;
                HeaderRightCell.BorderWidthLeft = 0;
                HeaderTable.AddCell(HeaderRightCell);
                cb.SetRGBColorFill(0, 0, 0);
                HeaderTable.WriteSelectedRows(0, -1, pageSize.GetLeft(40), pageSize.GetTop(50), cb);
            }
        }
        public override void OnEndPage(PdfWriter writer, Document document)
        {
            base.OnEndPage(writer, document);
            int pageN = writer.PageNumber;
            String text = "Page " + pageN + " of ";
            float len = bf.GetWidthPoint(text, 8);
            Rectangle pageSize = document.PageSize;
            cb.SetRGBColorFill(100, 100, 100);
            cb.BeginText();
            cb.SetFontAndSize(bf, 8);
            cb.SetTextMatrix(pageSize.GetLeft(40), pageSize.GetBottom(30));
            cb.ShowText(text);
            cb.EndText();
            cb.AddTemplate(template, pageSize.GetLeft(40) + len, pageSize.GetBottom(30));

            cb.BeginText();
            cb.SetFontAndSize(bf, 8);
            cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, 
            "Printed On " + PrintTime.ToString(), 
            pageSize.GetRight(40), 
            pageSize.GetBottom(30), 0);
            cb.EndText();
        }
        public override void OnCloseDocument(PdfWriter writer, Document document)
        {
            base.OnCloseDocument(writer, document);
            template.BeginText();
            template.SetFontAndSize(bf, 8);
            template.SetTextMatrix(0, 0);
            template.ShowText("" + (writer.PageNumber - 1));
            template.EndText();
        }
    }
}





// Our custom Header and Footer is done using Event Handler
TwoColumnHeaderFooter PageEventHandler = new TwoColumnHeaderFooter();
PDFWriter.PageEvent = PageEventHandler;
// Define the page header
PageEventHandler.Title = Title; //set empty title only to not add title and add footer
PageEventHandler.HeaderFont = FontFactory.GetFont(BaseFont.COURIER_BOLD, 10, Font.BOLD);
PageEventHandler.HeaderLeft = "Group";
PageEventHandler.HeaderRight = "1";

Html Editor Plugin – bootstrap-wysiwyg-editor

Reference http://www.c-sharpcorner.com/uploadfile/3d39b4/bootstrap-wysiwyg-editor-in-asp-net-mvc/

 

 

Introduction

This article introduces how to implement a Bootstrap WYSIWYG editor in an ASP.NET MVC application. There are many editors available over the internet but here we will implement the Summer note editor that is a simple WYSIWYG editor using Bootstrap. Summer note is a JavaScript library that helps us to create a WYSIWYG editor. You can download it from either GitHub or the official site.

Using the Code

Summer note uses the open-source libraries jQuery, Bootstrap and Font Awesome. To implement this in an ASP.NET MVC application we will create an MVC project in Visual Studio. First of all we will create bundles required for both JavaScript and CSS files. The following code snippet shows the BundleConfig class.

  1. using System.Web.Optimization;
  2. namespace BootstrapEditor.App_Start
  3. {
  4.     public class BundleConfig
  5.     {
  6.         public static void RegisterBundles(BundleCollection bundles)
  7.         {
  8.             bundles.Add(new StyleBundle(“~/Content/css”).Include(
  9.                      “~/Content/css/bootstrap.css”,
  10.                      “~/Content/css/font-awesome.css”,
  11.                      “~/Content/css/site.css”
  12.                    ));
  13.             bundles.Add(new StyleBundle(“~/Content/summernote”).Include(
  14.                 “~/Content/summernote/summernote.css”
  15.                 ));
  16.             bundles.Add(new ScriptBundle(“~/bundle/base”).Include(
  17.                “~/Scripts/jquery-1.10.2.js”,
  18.                 “~/Scripts/bootstrap.js”
  19.                ));
  20.             bundles.Add(new ScriptBundle(“~/bundle/summernote”).Include(
  21.                “~/Content/summernote/summernote.js”
  22.                ));
  23.         }
  24.     }
  25. }

Thereafter we will create a view model (HomeViewModel) that will be bind to the view. The following code snippet shows the HomeViewModel class.

  1. namespace BootstrapEditor.Models
  2. {
  3.     public class HomeViewModel
  4.     {
  5.         public string Title { getset; }
  6.         public string Content { getset; }
  7.     }
  8. }

Thereafter we will create a controller that handles GET and POST requests for a view. The following code snippet shows HomeController.

  1. using BootstrapEditor.Models;
  2. using System.Web.Mvc;
  3. namespace BootstrapEditor.Controllers
  4. {
  5.     public class HomeController : Controller
  6.     {
  7.         [HttpGet]
  8.         public ActionResult Index()
  9.         {
  10.             HomeViewModel model = new HomeViewModel();
  11.             return View();
  12.         }
  13.         [HttpPost]
  14.         public ActionResult Index(HomeViewModel model)
  15.         {
  16.             return View(model);
  17.         }
  18.     }
  19. }

Now we will create a view that renders the editor on UI. The following code snippet shows the index view.

  1. @model BootstrapEditor.Models.HomeViewModel
  2. @section head{
  3.     @Styles.Render(“~/Content/summernote”)
  4. }
  5. class=“panel panel-primary”>
  6.     
    class=“panel-heading panel-head”>Add Content
  7.     
    class=“panel-body”>
  8.         @using (Html.BeginForm())
  9.         {
  10.             
    class=“form-horizontal”>
  11.                 
    class=“form-group”>
  12.                     @Html.LabelFor(model => model.Title, new { @class = “col-lg-2 control-label” })
  13.                     
    class=“col-lg-9”>
  14.                         @Html.TextBoxFor(model => model.Title, new { @class = “form-control” })
  15.                     
  •                 </div>
  •                 
    class=“form-group”>
  •                     @Html.LabelFor(model => model.Content, new { @class = “col-lg-2 control-label” })
  •                     
    class=“col-lg-9”>
  •                         @Html.TextAreaFor(model => model.Content, new { @class = “form-control”, @row = 5 })
  •                     
  •                 </div>
  •                 
    class=“form-group”>
  •                     
    class=“col-lg-9”>
  •                     
    class=“col-lg-3”>
  •                         class=“btn btn-success” id=“btnSubmit” type=“submit”>
  •                             Submit
  •                         
  •